-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09p1.go
97 lines (84 loc) · 1.42 KB
/
09p1.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"strconv"
"aoc2022/utils"
)
type position struct {
x int
y int
}
func D09P1() {
lines := utils.ReadLines("./inputs/09.txt")
head := position{0, 0}
tail := position{0, 0}
tailVisited := make(map[position]bool)
for _, line := range lines {
moves, _ := strconv.Atoi(line[2:])
for moves > 0 {
switch line[0] {
case 'U':
head.y++
case 'D':
head.y--
case 'L':
head.x--
case 'R':
head.x++
}
moves--
tail = moveTail(tail, head)
tailVisited[tail] = true
}
}
fmt.Printf("Tail visited %d unique positions\n", len(tailVisited))
}
func moveTail(tail position, head position) (newTail position) {
newTail = tail
delta := position{head.x - tail.x, head.y - tail.y}
switch delta {
case
position{-1, 2},
position{-2, 1},
position{-2, 2},
position{0, 2},
position{1, 2},
position{2, 1},
position{2, 2}:
newTail.y++
}
switch delta {
case
position{-1, -2},
position{-2, -1},
position{-2, -2},
position{0, -2},
position{1, -2},
position{2, -1},
position{2, -2}:
newTail.y--
}
switch delta {
case
position{-1, -2},
position{-1, 2},
position{-2, -0},
position{-2, -1},
position{-2, -2},
position{-2, 1},
position{-2, 2}:
newTail.x--
}
switch delta {
case
position{1, -2},
position{2, -1},
position{2, -2},
position{2, 0},
position{2, 1},
position{2, 2},
position{1, 2}:
newTail.x++
}
return
}