88 lines
1.6 KiB
Go
88 lines
1.6 KiB
Go
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)
|
|
}
|