-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (71 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
_ "embed"
"fmt"
"golang.org/x/exp/maps"
)
type Point struct {
X int
Y int
}
//go:embed input.txt
var input string
func main() {
visitedBySanta := part1(input)
fmt.Printf("Part 1: %d\n", visitedBySanta)
visitedBySantaAndRobot := part2(input)
fmt.Printf("Part 2: %d\n", visitedBySantaAndRobot)
}
func part2(input string) int {
santa := Point{0, 0}
robot := Point{0, 0}
visitedHouses := make(map[Point]struct{})
visitedHouses[santa] = struct{}{}
for i := 0; i < len(input); i++ {
if i%2 == 0 {
switch input[i] {
case '^':
santa.Y++
case 'v':
santa.Y--
case '>':
santa.X++
case '<':
santa.X--
}
visitedHouses[santa] = struct{}{}
} else {
switch input[i] {
case '^':
robot.Y++
case 'v':
robot.Y--
case '>':
robot.X++
case '<':
robot.X--
}
visitedHouses[robot] = struct{}{}
}
}
return len(maps.Keys(visitedHouses))
}
func part1(input string) int {
position := Point{0, 0}
visitedHouses := make(map[Point]struct{})
visitedHouses[position] = struct{}{}
for i := 0; i < len(input); i++ {
switch input[i] {
case '^':
position.Y++
case 'v':
position.Y--
case '>':
position.X++
case '<':
position.X--
}
visitedHouses[position] = struct{}{}
}
return len(maps.Keys(visitedHouses))
}