@@ -6,7 +6,7 @@ using AdventOfCode2024
6
6
function day15 (input:: String = readInput (joinpath (@__DIR__ , " .." , " data" , " day15.txt" )))
7
7
world, instructions = parse_input (input)
8
8
world_p1 = copy (world)
9
- execute_instructions_p1 ! (world_p1, instructions)
9
+ execute_instructions ! (world_p1, instructions)
10
10
p1 = total_gps (world_p1)
11
11
world_p2 = fill (' .' , (size (world) .* (1 , 2 )). .. )
12
12
for ind ∈ eachindex (IndexCartesian (), world)
@@ -20,37 +20,16 @@ function day15(input::String = readInput(joinpath(@__DIR__, "..", "data", "day15
20
20
world_p2[ind[1 ],2 * ind[2 ]- 1 ] = ' @'
21
21
end
22
22
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]
25
26
end
26
27
27
28
function parse_input (input)
28
29
wo, ins = split (input, " \n\n " )
29
30
return map (x -> x[1 ], reduce (vcat, permutedims .(map (x -> split (x, " " ), split (wo))))), replace (ins, ' \n ' => " " )
30
31
end
31
32
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
-
54
33
function _to_dir (c:: Char )
55
34
c == ' v' && return CartesianIndex (1 , 0 )
56
35
c == ' ^' && return CartesianIndex (- 1 , 0 )
60
39
61
40
function total_gps (world:: Matrix{Char} )
62
41
total = 0
63
- for index ∈ findall (x -> x == ' O' , world)
42
+ for index ∈ findall (x -> x ∈ ( ' O' , ' [ ' ) , world)
64
43
total += 100 * (index[1 ] - 1 ) + index[2 ] - 1
65
44
end
66
45
return total
67
46
end
68
47
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
+
69
79
function locate_box_complex! (tipping_positions:: Set{CartesianIndex{2}} , move_positions:: Set{CartesianIndex{2}} , world:: Matrix{Char} , dir:: CartesianIndex{2} )
70
80
new_tipping_positions = Set {CartesianIndex{2}} ()
71
81
for pos ∈ tipping_positions
@@ -104,34 +114,6 @@ function move!(move_positions::Set{CartesianIndex{2}}, world::Matrix{Char}, dir:
104
114
end
105
115
end
106
116
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
+
136
118
137
119
end # module
0 commit comments