Skip to content

Commit fb1ba37

Browse files
committed
[Day 24] Small improvements
1 parent 73e97b4 commit fb1ba37

File tree

2 files changed

+4
-27
lines changed

2 files changed

+4
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This Julia package contains my solutions for [Advent of Code 2024](https://adven
3232
| 20 | [:white_check_mark:](https://adventofcode.com/2024/day/20) | 227.453 ms | 224.97 MiB| [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day20.jl) |
3333
| 22 | [:white_check_mark:](https://adventofcode.com/2024/day/22) | 241.724 ms | 78.80 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day22.jl) |
3434
| 23 | [:white_check_mark:](https://adventofcode.com/2024/day/23) | 4.197 ms | 3.82 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day23.jl) |
35-
| 24 | [:white_check_mark:](https://adventofcode.com/2024/day/24) | 9.476 s | 4.84 GiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day24.jl) |
35+
| 24 | [:white_check_mark:](https://adventofcode.com/2024/day/24) | 8.618 s | 4.84 GiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day24.jl) |
3636
<!-- | 21 | [:white_check_mark:](https://adventofcode.com/2024/day/21) | 9.675 ms | 7.19 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day21.jl) | -->
3737
<!-- | 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) | -->
3838

src/day24.jl

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ using AdventOfCode2024
44

55
function day24(input::String = readInput(joinpath(@__DIR__, "..", "data", "day24.txt")))
66
x, y, rules = parse_input(input)
7-
p1, _ = run(x, y, rules, nothing)
7+
p1, ruleorder = run(x, y, rules, nothing)
88

99
swapped_outputs = []
10-
_, wrong_adders = check_adders(rules, Int[])
10+
wrong_adders = [i for i 0:44 if !check_adders_at_indices(rules, [i]; ruleorder)[1]]
1111

1212
sequences = find_sequences(wrong_adders)
1313
for sequence sequences
@@ -91,7 +91,7 @@ function parse_input(input::AbstractString)
9191
return x, y, rule
9292
end
9393

94-
function run(x::Int, y::Int, rules::Vector{NTuple{4, String}}, ruleorder::Nothing)
94+
function run(x::Int, y::Int, rules::Vector{NTuple{4, String}}, _::Nothing)
9595
state = Dict{String,Bool}()
9696
unused_rules = Set(1:length(rules))
9797
prev_length = length(unused_rules) + 1
@@ -196,8 +196,6 @@ function run(x::Int, y::Int, rules::Vector{NTuple{4, String}}, ruleorder::Vector
196196
return result, ruleorder
197197
end
198198

199-
200-
201199
function needed_rules(rules::Vector{NTuple{4, String}}, outinds::Vector{Int})
202200
ruleinds = Set{Int}()
203201
reg = r"[xy]\d{2}"
@@ -216,27 +214,6 @@ function needed_rules(rules::Vector{NTuple{4, String}}, outinds::Vector{Int})
216214
return ruleinds
217215
end
218216

219-
function check_adders(rules::Vector{NTuple{4, String}}, ignore_indices::Vector{Int}; ruleorder::Union{Vector{Int},Nothing} = nothing)
220-
wrong = Int[]
221-
xi = (0, 0, 1, 1, 0, 1, 0, 1)
222-
yi = (0, 1, 0, 1, 0, 0, 1, 1)
223-
cprev = (0, 0, 0, 0, 1, 1, 1, 1)
224-
for index setdiff(0:44, ignore_indices)
225-
iter = index == 0 ? (1:4) : (1:8)
226-
for i iter
227-
x = (xi[i] << index) + (cprev[i] << (index - 1))
228-
y = (yi[i] << index) + (cprev[i] << (index - 1))
229-
result, ruleorder = run(x, y, rules, ruleorder)
230-
result == -1 && return false, wrong
231-
if result != x + y
232-
push!(wrong, index)
233-
break
234-
end
235-
end
236-
end
237-
return true, wrong
238-
end
239-
240217
function check_adders_at_indices(rules::Vector{NTuple{4, String}}, indices::Vector{Int}; ruleorder::Union{Vector{Int},Nothing} = nothing)
241218
xi = (0, 0, 1, 1, 0, 1, 0, 1)
242219
yi = (0, 1, 0, 1, 0, 0, 1, 1)

0 commit comments

Comments
 (0)