Skip to content

Commit a0aa086

Browse files
committed
[Day 15] Add solution
1 parent d359ce9 commit a0aa086

File tree

3 files changed

+40
-56
lines changed

3 files changed

+40
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This Julia package contains my solutions for [Advent of Code 2024](https://adven
2424
| 12 | [:white_check_mark:](https://adventofcode.com/2024/day/12) | 12.506 ms | 10.00 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day12.jl) |
2525
| 13 | [:white_check_mark:](https://adventofcode.com/2024/day/13) | 3.423 ms | 1.15 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day13.jl) |
2626
| 14 | [:white_check_mark:](https://adventofcode.com/2024/day/14) | 75.842 ms | 275.06 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day14.jl) |
27+
| 15 | [:white_check_mark:](https://adventofcode.com/2024/day/15) | 1.944 ms | 3.16 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day15.jl) |
2728
| 16 | [:white_check_mark:](https://adventofcode.com/2024/day/16) | 167.184 ms | 105.84 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day16.jl) |
2829
| 17 | [:white_check_mark:](https://adventofcode.com/2024/day/17) | 6.921 ms | 706.30 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day17.jl) |
2930
| 18 | [:white_check_mark:](https://adventofcode.com/2024/day/18) |2.964 ms | 5.64 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day18.jl) |
@@ -34,7 +35,6 @@ This Julia package contains my solutions for [Advent of Code 2024](https://adven
3435
<!-- | 23 | [:white_check_mark:](https://adventofcode.com/2024/day/23) | 2.979 s | 9.69 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day23.jl) | -->
3536
<!-- | 24 | [:white_check_mark:](https://adventofcode.com/2024/day/24) | 43.214 ms | 49.77 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day24.jl) | -->
3637
<!-- | 25 | [:white_check_mark:](https://adventofcode.com/2024/day/25) | 69.476 ms | 62.03 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day25.jl) | -->
37-
<!-- | 15 | [:white_check_mark:](https://adventofcode.com/2024/day/15) | 2.647 ms | 1.49 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day15.jl) | -->
3838

3939

4040
The benchmarks have been measured on this machine:

src/day15.jl

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using AdventOfCode2024
66
function day15(input::String = readInput(joinpath(@__DIR__, "..", "data", "day15.txt")))
77
world, instructions = parse_input(input)
88
world_p1 = copy(world)
9-
execute_instructions_p1!(world_p1, instructions)
9+
execute_instructions!(world_p1, instructions)
1010
p1 = total_gps(world_p1)
1111
world_p2 = fill('.', (size(world) .* (1, 2))...)
1212
for ind eachindex(IndexCartesian(), world)
@@ -20,37 +20,16 @@ function day15(input::String = readInput(joinpath(@__DIR__, "..", "data", "day15
2020
world_p2[ind[1],2*ind[2]-1] = '@'
2121
end
2222
end
23-
execute_instructions_p2!(world_p2, instructions)
24-
return world_p2
23+
execute_instructions!(world_p2, instructions)
24+
p2 = total_gps(world_p2)
25+
return [p1, p2]
2526
end
2627

2728
function parse_input(input)
2829
wo, ins = split(input, "\n\n")
2930
return map(x -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(wo))))), replace(ins, '\n' => "")
3031
end
3132

32-
function execute_instructions_p1!(world, instructions)
33-
current = findall(x -> x == '@', world)[1]
34-
world[current] = '.'
35-
for instruction instructions
36-
next = current + _to_dir(instruction)
37-
if world[next] == '.'
38-
current = next
39-
elseif world[next] == 'O'
40-
while world[next] ('.', '#')
41-
next += _to_dir(instruction)
42-
end
43-
world[next] == '#' && continue
44-
while next != current
45-
pnext = next - _to_dir(instruction)
46-
world[next], world[pnext] = world[pnext], world[next]
47-
next = pnext
48-
end
49-
current = current + _to_dir(instruction)
50-
end
51-
end
52-
end
53-
5433
function _to_dir(c::Char)
5534
c == 'v' && return CartesianIndex(1, 0)
5635
c == '^' && return CartesianIndex(-1, 0)
@@ -60,12 +39,43 @@ end
6039

6140
function total_gps(world::Matrix{Char})
6241
total = 0
63-
for index findall(x -> x == 'O', world)
42+
for index findall(x -> x ('O', '['), world)
6443
total += 100 * (index[1] - 1) + index[2] - 1
6544
end
6645
return total
6746
end
6847

48+
function execute_instructions!(world, instructions)
49+
current = findall(x -> x == '@', world)[1]
50+
world[current] = '.'
51+
for (k, instruction) instructions |> enumerate
52+
dir = _to_dir(instruction)
53+
next = current + dir
54+
if world[next] == '.'
55+
current = next
56+
elseif world[next] ('[',']', 'O')
57+
if instruction ('>', '<') || world[next] == 'O'
58+
while world[next] ('.', '#')
59+
next += dir
60+
end
61+
world[next] == '#' && continue
62+
while next != current
63+
pnext = next - dir
64+
world[next], world[pnext] = world[pnext], world[next]
65+
next = pnext
66+
end
67+
current = current + dir
68+
else
69+
move_positions = Set{CartesianIndex{2}}()
70+
if locate_box_complex!(Set([next]), move_positions, world, dir)
71+
move!(move_positions, world, dir)
72+
current = current + dir
73+
end
74+
end
75+
end
76+
end
77+
end
78+
6979
function locate_box_complex!(tipping_positions::Set{CartesianIndex{2}}, move_positions::Set{CartesianIndex{2}}, world::Matrix{Char}, dir::CartesianIndex{2})
7080
new_tipping_positions = Set{CartesianIndex{2}}()
7181
for pos tipping_positions
@@ -104,34 +114,6 @@ function move!(move_positions::Set{CartesianIndex{2}}, world::Matrix{Char}, dir:
104114
end
105115
end
106116

107-
function execute_instructions_p2!(world, instructions)
108-
current = findall(x -> x == '@', world)[1]
109-
world[current] = '.'
110-
for instruction instructions
111-
dir = _to_dir(instruction)
112-
next = current + dir
113-
if world[next] == '.'
114-
current = next
115-
elseif world[next] ('[',']')
116-
if instruction ('>', '<')
117-
while world[next] ('.', '#')
118-
next += dir
119-
end
120-
world[next] == '#' && continue
121-
while next != current
122-
pnext = next - dir
123-
world[next], world[pnext] = world[pnext], world[next]
124-
next = pnext
125-
end
126-
current = current + dir
127-
else
128-
move_positions = Set{CartesianIndex{2}}()
129-
if locate_box_complex!(Set([next]), move_positions, world, dir)
130-
move!(move_positions, world, dir)
131-
end
132-
end
133-
end
134-
end
135-
end
117+
136118

137119
end # module

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ end
207207
"<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>" *
208208
"^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>" *
209209
"v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^\n"
210+
@test AdventOfCode2024.Day15.day15(sample) == [10092, 9021]
211+
@test AdventOfCode2024.Day15.day15() == [1492518, 1512860]
210212
end
211213

212214
@testset "Day 16" begin

0 commit comments

Comments
 (0)