adding 2015 & 2025 advents
This commit is contained in:
10
2015/03/golang/.vscode/settings.json
vendored
Normal file
10
2015/03/golang/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"[go]": {
|
||||
"editor.insertSpaces": false,
|
||||
"editor.formatOnSave": true,
|
||||
"editor.defaultFormatter": "golang.go",
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.organizeImports": "explicit"
|
||||
}
|
||||
}
|
||||
}
|
||||
3
2015/03/golang/go.mod
Normal file
3
2015/03/golang/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module aoc/day-3
|
||||
|
||||
go 1.24.4
|
||||
87
2015/03/golang/main.go
Normal file
87
2015/03/golang/main.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Point struct {
|
||||
x, y int
|
||||
}
|
||||
|
||||
func main() {
|
||||
input := readFile("../input.txt")
|
||||
fmt.Println(input)
|
||||
fmt.Println(part1(input))
|
||||
fmt.Println(part2(input))
|
||||
}
|
||||
|
||||
func part1(input string) int {
|
||||
data := strings.Split(input, "")
|
||||
locations := make(map[Point]int)
|
||||
current := Point{x: 0, y: 0}
|
||||
locations[current] = 1
|
||||
for _, v := range data {
|
||||
current = updateLocation(current, v)
|
||||
l, exists := locations[current]
|
||||
if exists {
|
||||
locations[current] = l + 1
|
||||
} else {
|
||||
locations[current] = 1
|
||||
}
|
||||
}
|
||||
return len(locations)
|
||||
}
|
||||
|
||||
func part2(input string) int {
|
||||
data := strings.Split(input, "")
|
||||
locations := make(map[Point]int)
|
||||
santa := Point{x: 0, y: 0}
|
||||
robot := Point{x: 0, y: 0}
|
||||
locations[santa] = 1
|
||||
locations[robot] = 1
|
||||
for i, v := range data {
|
||||
if i%2 == 0 {
|
||||
santa = updateLocation(santa, v)
|
||||
l, exists := locations[santa]
|
||||
if exists {
|
||||
locations[santa] = l + 1
|
||||
} else {
|
||||
locations[santa] = 1
|
||||
}
|
||||
} else {
|
||||
robot = updateLocation(robot, v)
|
||||
l, exists := locations[robot]
|
||||
if exists {
|
||||
locations[robot] = l + 1
|
||||
} else {
|
||||
locations[robot] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return len(locations)
|
||||
}
|
||||
|
||||
func updateLocation(point Point, direction string) Point {
|
||||
switch direction {
|
||||
case "^":
|
||||
return Point{point.x, point.y + 1}
|
||||
case "v":
|
||||
return Point{point.x, point.y - 1}
|
||||
case ">":
|
||||
return Point{point.x + 1, point.y}
|
||||
case "<":
|
||||
return Point{point.x - 1, point.y}
|
||||
default:
|
||||
return point
|
||||
}
|
||||
}
|
||||
|
||||
func readFile(filename string) string {
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(content)
|
||||
}
|
||||
Reference in New Issue
Block a user