-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.go
199 lines (178 loc) · 4.27 KB
/
day9.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
)
type RopeEnd struct {
x, y int
}
func newRopeEnd() RopeEnd {
var r = RopeEnd{
x: 0,
y: 0,
}
return r
}
func (r *RopeEnd) MoveRope(direction rune) {
// this function moves the rope end in ONE direction.
// Meaning, given the direction, it will increase or
// decrease the respective offset by one ONLY. This will
// NOT consider diagonal, for that you should call this
// function twice.
switch direction {
case 'U':
// going up
r.y++
case 'D':
// going down
r.y--
case 'L':
// going left
r.x--
case 'R':
// going right
r.x++
}
}
func DetermineTailDirection(h, t *RopeEnd) error {
// This takes the head and tail's current position
// and updates the tail's position.
// first, are we occupying the same space?
if h.x == t.x && h.y == t.y {
return errors.New("Same")
}
// are we adjacent?
// To determine if we are adjacent, we need to see if the xOffsets
// AND the yOffsets are only 1 away!
hfx := float64(h.x)
hfy := float64(h.y)
tfx := float64(t.x)
tfy := float64(t.y)
if math.Abs(hfx-tfx) == 1 && math.Abs(hfy-tfy) <= 1 {
return errors.New("Adjacent")
}
if math.Abs(hfy-tfy) == 1 && math.Abs(hfx-tfx) <= 1 {
return errors.New("Adjacent")
}
// Find out if we even have to go diagonal.
// Are we in the same column OR row?
if h.x == t.x || h.y == t.y {
// we do not need to go diagonal
if h.x > t.x+1 {
// move 1 column right
t.MoveRope('R')
} else if h.x < t.x-1 {
// move 1 column left
t.MoveRope('L')
} else if h.y > t.y+1 {
// move 1 row up
t.MoveRope('U')
} else if h.y < t.y-1 {
// move 1 row down
t.MoveRope('D')
} else {
// this is redundant but whatever
return errors.New("Same")
}
} else {
// now we are moving diagonal. Check if up and right
if h.x > t.x+1 {
// we need to move right and...
t.MoveRope('R')
if h.y > t.y {
t.MoveRope('U')
} else if h.y < t.y {
t.MoveRope('D')
} else {
return errors.New("Weird logic? Moved right but not up or down")
}
} else if h.x < t.x-1 {
// we need to move left and...
t.MoveRope('L')
if h.y > t.y {
t.MoveRope('U')
} else if h.y < t.y {
t.MoveRope('D')
} else {
return errors.New("Weird logic? Moved left but not up or down")
}
} else if h.y > t.y+1 {
// we need to move up and...
t.MoveRope('U')
if h.x > t.x {
t.MoveRope('R')
} else if h.x < t.x {
t.MoveRope('L')
} else {
return errors.New("Weird logic? Moved up but not right or left")
}
} else if h.y < t.y-1 {
// we need to move down and...
t.MoveRope('D')
if h.x > t.x {
t.MoveRope('R')
} else if h.x < t.x {
t.MoveRope('L')
} else {
return errors.New("Weird logic? Moved down but not right or left")
}
} else {
return errors.New(fmt.Sprintf("Don't know what happened! h - x:%d y:%d, t - x:%d y:%d", h.x, h.y, t.x, t.y))
}
}
return nil // we moved, no errors
}
func main() {
readFile, err := os.Open("input")
if err != nil {
fmt.Println(err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var lines []string
for fileScanner.Scan() {
lines = append(lines, fileScanner.Text())
}
readFile.Close()
h := newRopeEnd()
t := newRopeEnd()
posCounter := make(map[string]int)
stepCount := 0
// count the first location
pos := fmt.Sprintf("%s~%s", strconv.Itoa(t.x), strconv.Itoa(t.y))
posCounter[pos]++
for _, v := range lines {
s := strings.Split(v, " ")
dir := s[0]
amt, err := strconv.Atoi(s[1])
if err != nil {
panic("Could not read integer!")
}
fmt.Printf("Direction: %s, Amount: %s\n", s[0], s[1])
for i := 0; i < amt; i++ {
r := []rune(dir)
h.MoveRope(r[0])
res := DetermineTailDirection(&h, &t)
if res != nil {
fmt.Printf("Step: %d) h - x:%d y:%d, t - x:%d y:%d - %s\n", stepCount, h.x, h.y, t.x, t.y, res)
} else {
fmt.Printf("Step: %d) h - x:%d y:%d, t - x:%d y:%d\n", stepCount, h.x, h.y, t.x, t.y)
}
// Count the new position
pos = fmt.Sprintf("%s~%s", strconv.Itoa(t.x), strconv.Itoa(t.y))
posCounter[pos]++
stepCount++
}
}
// var counter int = 0
// for range posCounter {
// counter++
// }
// fmt.Printf("posCounter: +%v\n", posCounter)
fmt.Println("Possible positions:", len(posCounter))
}