Skip to content

Commit 9b7d1b3

Browse files
committed
feat(2024): add day 15 part 1 and start of part 2
1 parent f26560a commit 9b7d1b3

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

go/2024/puzzles/day15/main.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/believer/aoc-2024/utils/files"
8+
"github.com/believer/aoc-2024/utils/grid"
9+
)
10+
11+
func main() {
12+
fmt.Println("Part 1: ", part1("input.txt"))
13+
fmt.Println("Part 2: ", part2("input.txt"))
14+
}
15+
16+
func part1(name string) int {
17+
p := files.ReadParagraphs(name)
18+
warehouse := grid.New(p[0])
19+
moves := []grid.Point{}
20+
gps := 0
21+
22+
// Get all moves from list
23+
for _, moveLine := range p[1] {
24+
for _, m := range strings.Split(moveLine, "") {
25+
switch m {
26+
case "<":
27+
moves = append(moves, grid.LEFT)
28+
case "^":
29+
moves = append(moves, grid.UP)
30+
case ">":
31+
moves = append(moves, grid.RIGHT)
32+
case "v":
33+
moves = append(moves, grid.DOWN)
34+
}
35+
}
36+
}
37+
38+
// Find robot start position
39+
robot := warehouse.Find('@')
40+
41+
for _, m := range moves {
42+
next := robot.Add(m)
43+
44+
// Move into wall, do nothing
45+
if warehouse.Get(next) == '#' {
46+
continue
47+
}
48+
49+
// Moving a box. Find last location that's not a
50+
// box and move our box there
51+
if warehouse.Get(next) == 'O' {
52+
tmp := next.Add(m)
53+
54+
for {
55+
if warehouse.Get(tmp) != 'O' {
56+
break
57+
}
58+
59+
tmp = tmp.Add(m)
60+
}
61+
62+
// Don't move boxes into wall
63+
if warehouse.Get(tmp) == '#' {
64+
continue
65+
}
66+
67+
warehouse.Update(tmp, 'O')
68+
}
69+
70+
// Update robot position
71+
warehouse.Update(next, '@')
72+
warehouse.Update(robot, '.')
73+
robot = next
74+
}
75+
76+
for _, box := range warehouse.FindAll('O') {
77+
gps += 100*box.Y + box.X
78+
}
79+
80+
return gps
81+
}
82+
83+
func part2(name string) int {
84+
p := files.ReadParagraphs(name)
85+
warehouse := grid.New(p[0])
86+
doubleWarehouse := grid.FromSize(warehouse.Width*2, warehouse.Height)
87+
gps := 0
88+
89+
// Scale warehouse
90+
for y := range warehouse.Height {
91+
for x := range warehouse.Width {
92+
wp := grid.Point{X: x, Y: y}
93+
p := grid.Point{X: x * 2, Y: y}
94+
p2 := grid.Point{X: x*2 + 1, Y: y}
95+
96+
switch warehouse.Get(wp) {
97+
case '.':
98+
doubleWarehouse.Update(p, '.')
99+
doubleWarehouse.Update(p2, '.')
100+
case '#':
101+
doubleWarehouse.Update(p, '#')
102+
doubleWarehouse.Update(p2, '#')
103+
case 'O':
104+
doubleWarehouse.Update(p, '[')
105+
doubleWarehouse.Update(p2, ']')
106+
case '@':
107+
doubleWarehouse.Update(p, '@')
108+
doubleWarehouse.Update(p2, '.')
109+
}
110+
}
111+
}
112+
113+
doubleWarehouse.Debug()
114+
115+
return gps
116+
}

go/2024/puzzles/day15/main_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPart1(t *testing.T) {
10+
t.Run("Part 1", func(t *testing.T) {
11+
expected := 10092
12+
actual := part1("test-input.txt")
13+
assert.Equal(t, expected, actual)
14+
})
15+
}
16+
17+
func TestPart1Small(t *testing.T) {
18+
t.Run("Part 1", func(t *testing.T) {
19+
expected := 2028
20+
actual := part1("test-input-small.txt")
21+
assert.Equal(t, expected, actual)
22+
})
23+
}
24+
25+
func TestPart2(t *testing.T) {
26+
t.Run("Part 2", func(t *testing.T) {
27+
expected := 0
28+
actual := part2("test-input.txt")
29+
assert.Equal(t, expected, actual)
30+
})
31+
}
32+
33+
func BenchmarkPart1(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
part1("input.txt")
36+
}
37+
}
38+
39+
func BenchmarkPart2(b *testing.B) {
40+
for i := 0; i < b.N; i++ {
41+
part2("input.txt")
42+
}
43+
}
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<<

go/2024/puzzles/day15/test-input.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+
#..O@..O.#
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^<<^

0 commit comments

Comments
 (0)