Skip to content

Commit a57957a

Browse files
committed
chore(angch/2024-15): Snapshot work in progress
1 parent 453f8cb commit a57957a

File tree

4 files changed

+129
-33
lines changed

4 files changed

+129
-33
lines changed

angch/2024-15/main.go

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var dirmap = map[byte][2]int{
1818

1919
type Board struct {
2020
board map[[2]int]byte
21+
obs map[[2]int]bool
22+
boxes [][2]int
2123
robot [2]int
2224
maxx int
2325
maxy int
@@ -30,22 +32,22 @@ func dumpboard(b Board) (int, int) {
3032
gps := 0
3133
for y := 0; y < b.maxy; y++ {
3234
for x := 0; x < b.maxx; x++ {
33-
if [2]int{x, y} == b.robot {
35+
coord := [2]int{x, y}
36+
37+
if coord == b.robot {
3438
if display {
3539
fmt.Print("@")
3640
}
3741
continue
3842
}
39-
c := b.board[[2]int{x, y}]
43+
c := b.board[coord]
4044
if c == 'O' {
4145
gps += y*100 + x
4246
}
4347
if display {
4448
switch c {
4549
case '#', 'O':
46-
4750
fmt.Print(string(c))
48-
4951
default:
5052
fmt.Print(".")
5153
}
@@ -59,6 +61,49 @@ func dumpboard(b Board) (int, int) {
5961
return gps, 0
6062
}
6163

64+
func dumpboard2(b Board) (int, int) {
65+
_ = dirmap
66+
display := true
67+
gps := 0
68+
69+
boxMap := map[[2]int]byte{}
70+
for _, v := range b.boxes {
71+
boxMap[v] = '['
72+
boxMap[[2]int{v[0] + 1, v[1]}] = ']'
73+
}
74+
75+
for y := 0; y < b.maxy; y++ {
76+
for x := 0; x < b.maxx; x++ {
77+
coord := [2]int{x, y}
78+
coordhalf := [2]int{x / 2, y}
79+
coordp1 := [2]int{x - 1, y}
80+
81+
if coord == b.robot || coordp1 == b.robot {
82+
if display {
83+
fmt.Print("@")
84+
}
85+
continue
86+
}
87+
88+
if b.obs[coordhalf] {
89+
fmt.Print("#")
90+
continue
91+
}
92+
93+
if boxMap[coord] != byte(0) {
94+
fmt.Print(string(boxMap[coord]))
95+
continue
96+
}
97+
98+
fmt.Print(".")
99+
}
100+
if display {
101+
fmt.Println()
102+
}
103+
}
104+
return gps, 0
105+
}
106+
62107
func day15(file string) (part1, part2 int) {
63108
f, err := os.Open(file)
64109
if err != nil {
@@ -71,20 +116,29 @@ func day15(file string) (part1, part2 int) {
71116
robot := [2]int{0, 0}
72117
y := 0
73118
maxx := 0
119+
obs := map[[2]int]bool{}
120+
boxes := [][2]int{}
74121
for scanner.Scan() {
75122
t := scanner.Text()
76123
if t == "" {
77124
break
78125
}
79126
for x, c := range t {
127+
coord := [2]int{x, y}
80128
if c == '@' {
81-
robot = [2]int{x, y}
129+
robot = coord
82130
continue
83131
}
84132
if c == '.' {
85133
continue
86134
}
87-
board[[2]int{x, y}] = byte(c)
135+
board[coord] = byte(c)
136+
if c == '#' {
137+
obs[[2]int{x, y}] = true
138+
}
139+
if c == 'O' {
140+
boxes = append(boxes, coord)
141+
}
88142
}
89143
maxx = len(t)
90144
y++
@@ -95,13 +149,20 @@ func day15(file string) (part1, part2 int) {
95149
maxx: maxx,
96150
maxy: y,
97151
halfx: false,
152+
obs: obs,
153+
boxes: boxes,
98154
}
99155
b2 := Board{
100-
board: board,
101-
robot: [2]int{robot[0] * 2, robot[1]}, // robot's x is twice
102-
maxx: maxx,
156+
board: map[[2]int]byte{}, // not used
157+
robot: [2]int{robot[0] * 2, robot[1]}, // fullx
158+
maxx: maxx * 2, // fullx
103159
maxy: y,
104160
halfx: true,
161+
obs: obs, // halfx
162+
boxes: boxes, // fullx
163+
}
164+
for k := range b2.boxes {
165+
b2.boxes[k][0] *= 2
105166
}
106167

107168
dumpboard(b)
@@ -147,40 +208,35 @@ func day15(file string) (part1, part2 int) {
147208
}
148209
b:
149210
for m, move := range moves {
150-
_ = m
151211
if true {
152212
fmt.Println(m)
153-
dumpboard(b)
213+
dumpboard2(b2)
154214
}
155-
156215
d := dirmap[byte(move)]
157216

158217
coord1 := [2]int{b.robot[0] + d[0], b.robot[1] + d[1]}
159218
// Empty
160-
bb := b.board[coord1]
161219

162-
switch bb {
163-
case byte(0):
164-
b.robot = coord1
165-
continue a
166-
case '#':
167-
continue a
220+
coord1m := [2]int{coord1[0] - 1, coord1[1]}
221+
coordhalf := [2]int{coord1[0] / 2, coord1[1]}
222+
223+
if b.obs[coordhalf] {
224+
// blocked
225+
fmt.Println("Blocked", string(move))
226+
continue
168227
}
169228

170-
l1 := coord1
171-
r1 := coord1
172-
for b.board[r1] == 'O' {
173-
r1 = [2]int{r1[0] + d[0], r1[1] + d[1]}
229+
intheway := []int{}
230+
for k, v := range b.boxes {
231+
if v == coord1 || v == coord1m {
232+
intheway = append(intheway, k)
233+
}
174234
}
175-
if b.board[r1] == '#' {
176-
// Can't move.
177-
fmt.Println("Can't move O")
178-
continue a
235+
if len(intheway) == 0 {
236+
fmt.Println("no blockj", string(move))
237+
b2.robot = coord1
238+
continue b
179239
}
180-
b.board[r1] = 'O'
181-
delete(b.board, l1)
182-
b.robot = coord1
183-
fmt.Println("Move block O")
184240
}
185241
}
186242
part1, part2 = dumpboard(b)
@@ -199,9 +255,9 @@ func main() {
199255
defer pf.Close()
200256
}
201257
t1 := time.Now()
202-
part1, part2 := day15("test.txt")
258+
part1, part2 := day15("test3.txt")
203259
fmt.Println(part1, part2)
204-
if part1 != 2028 {
260+
if part1 != 2028+1 {
205261
log.Fatal("Test failed ", part1, part2)
206262
}
207263
part1, part2 = day15("test2.txt")

angch/2024-15/test.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
########
2+
#..O.O.#
3+
##@.O..#
4+
#...O..#
5+
#.#.O..#
6+
#...O..#
7+
#......#
8+
########
9+
10+
<^^>>>vv<v>>v<<

angch/2024-15/test2.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
##########
2+
#..O..O.O#
3+
#......O.#
4+
#.OO..O.O#
5+
6+
#O#..O...#
7+
#O..O..O.#
8+
#.OO.O.OO#
9+
#....O...#
10+
##########
11+
12+
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
13+
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
14+
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
15+
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
16+
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
17+
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
18+
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
19+
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
20+
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
21+
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^

angch/2024-15/test3.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#######
2+
#...#.#
3+
#.....#
4+
#..OO@#
5+
#..O..#
6+
#.....#
7+
#######
8+
9+
<vv<<^^<<^^

0 commit comments

Comments
 (0)