-
Notifications
You must be signed in to change notification settings - Fork 1
/
solutions.go
173 lines (147 loc) · 2.88 KB
/
solutions.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package day09
import (
"fmt"
"strconv"
"strings"
)
type Puzzle struct{}
type Move struct {
direction string
steps int
}
func parseInput(raw string) []Move {
moves := []Move{}
lines := strings.Split(strings.TrimSpace(raw), "\n")
for _, line := range lines {
parts := strings.Split(line, " ")
steps, err := strconv.Atoi(parts[1])
if err != nil {
panic(err)
}
moves = append(moves, Move{
direction: parts[0],
steps: steps,
})
}
return moves
}
// []Pos(L,C) -> [ (0,0), (0,1), (0,2), (1,2), (2,2), (2,1), (2,0), (2,-1) ]
type Pos struct {
line int
col int
}
func (p *Pos) moveHead(mov Move) {
switch mov.direction {
case "R":
p.col++
case "L":
p.col--
case "U":
p.line--
case "D":
p.line++
}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
// . . . .
// . . T .
// . . . .
// . H . .
func (t *Pos) moveTail(h Pos) {
// T H
// 1,2 :: 3,1 -> 1,1 ou -1,-1
lineDiff := t.line - h.line
colDiff := t.col - h.col
if abs(colDiff) <= 1 && abs(lineDiff) <= 1 {
return
}
if lineDiff == 0 {
if colDiff > 0 {
t.col--
} else {
t.col++
}
} else if colDiff == 0 {
if lineDiff > 0 {
t.line--
} else {
t.line++
}
} else {
if colDiff > 0 && lineDiff > 0 {
t.line--
t.col--
} else if colDiff > 0 && lineDiff < 0 {
t.line++
t.col--
} else if colDiff < 0 && lineDiff > 0 {
t.line--
t.col++
} else if colDiff < 0 && lineDiff < 0 {
t.line++
t.col++
} else {
panic("ahhhhhh")
}
}
}
type TailTrail map[Pos]struct{}
func (t TailTrail) updateTailTrail(tail Pos) {
if _, ok := t[tail]; !ok {
t[tail] = struct{}{}
}
}
func simulator(moves []Move) int {
head := Pos{0, 0}
tail := Pos{0, 0}
tailTrail := TailTrail{Pos{0, 0}: struct{}{}}
// tailTrail[Pos{0, 0}] = struct{}{} // isto é o mesmo que está acima
for _, mov := range moves {
for step := 0; step < mov.steps; step++ {
head.moveHead(mov)
tail.moveTail(head)
tailTrail.updateTailTrail(tail)
}
}
return len(tailTrail)
}
func simulatorPart2(moves []Move) int {
knots := []Pos{}
for i := 0; i < 10; i++ {
knots = append(knots, Pos{0, 0})
}
tailTrail := TailTrail{Pos{0, 0}: struct{}{}}
for _, mov := range moves {
for step := 0; step < mov.steps; step++ {
knots[0].moveHead(mov)
for i := range knots[1:] {
knots[i+1].moveTail(knots[i]) // move next, based on previous
}
tailTrail.updateTailTrail(knots[9])
}
}
return len(tailTrail)
}
func (*Puzzle) Part1(input string) string {
// simularMovimentos()
// headPos.move(mov)
// tailPos.move(headPos)
// [][]bool
// contarPosicoesCauda()
moves := parseInput(input)
solution := simulator(moves)
return fmt.Sprint(solution)
}
func (*Puzzle) Part2(input string) string {
moves := parseInput(input)
solution := simulatorPart2(moves)
return fmt.Sprint(solution)
}
func (*Puzzle) Notes() string {
return "map/dict/set coord visitadas"
}