commit 734acf76c6759956e6478d8ed5f089cdb03278a6
parent 3ae71a1a33ea04c65ce374ba6f50842a30c15ece
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Mon, 12 Dec 2022 15:31:07 -0800
Solution to day 11, part 2.
Diffstat:
M | src/day_11.jl | | | 46 | +++++++++++++++++++++++++++++++--------------- |
1 file changed, 31 insertions(+), 15 deletions(-)
diff --git a/src/day_11.jl b/src/day_11.jl
@@ -39,7 +39,7 @@ module MonkeyBusiness
mutable struct Monkey
id::Int
items::Vector{Int}
- operation::AbstractString
+ operation::Tuple{<:AbstractString,<:AbstractString}
divisible::Int
throw_id::Tuple{Int,Int}
considerations::Int
@@ -52,7 +52,7 @@ module MonkeyBusiness
monkeyregexes = [
r"^Monkey (\d+):$",
r"^ Starting items: ([0-9, ]*)$",
- r"^ Operation: new = (.*)$",
+ r"^ Operation: new = old ([+*]) (old|\d+)$",
r"^ Test: divisible by (\d+)$",
r"^ If true: throw to monkey (\d+)$",
r"^ If false: throw to monkey (\d+)$"
@@ -63,7 +63,7 @@ module MonkeyBusiness
Monkey(
parse(Int, monkeymatches[1].captures[1]),
parse.(Int, split(monkeymatches[2].captures[1], ", ")),
- monkeymatches[3].captures[1],
+ Tuple(monkeymatches[3].captures),
parse(Int, monkeymatches[4].captures[1]),
(
parse(Int, monkeymatches[5].captures[1]),
@@ -75,12 +75,19 @@ module MonkeyBusiness
"""
Apply the operation to see how the worry level changes.
-
- This is a hacky/brittle approach! But it works for the little code puzzle.
"""
- function monkeyinspection(monkey::Monkey, worry::Int)
- operation = replace(monkey.operation, "old" => worry)
- eval(Meta.parse(operation))
+ function monkeyinspection!(monkey::Monkey, worry::Int)
+ if monkey.operation[2] == "old"
+ operand = worry
+ else
+ operand = parse(Int, monkey.operation[2])
+ end
+
+ if monkey.operation[1] == "*"
+ worry * operand
+ else
+ worry + operand
+ end
end
"""
@@ -94,21 +101,23 @@ module MonkeyBusiness
Process one monkey's turn.
"""
function monkeyturn!(monkey::Monkey,
- monkeys::Dict{Int, Monkey})
+ monkeys::Dict{Int, Monkey},
+ part::Int)
while !isempty(monkey.items)
- item = popfirst!(monkey.items)
- item = monkeyinspection(monkey, item)
- item ÷= 3
+ item = mod(monkeyinspection!(monkey, popfirst!(monkey.items)),
+ mapreduce(x->x.divisible, *, values(monkeys)))
+ part == 1 && (item ÷= 3)
push!(monkeys[monkeytest(monkey, item)].items, item)
monkey.considerations += 1
end
end
- function monkeyrounds!(monkeys::Dict{Int, Monkey}, numrounds::Int)
+ function monkeyrounds!(monkeys::Dict{Int, Monkey}, numrounds::Int,
+ part::Int = 1)
monkeyids = sort(collect(keys(monkeys)))
for _ = 1:numrounds
for monkeyid = monkeyids
- monkeyturn!(monkeys[monkeyid], monkeys)
+ monkeyturn!(monkeys[monkeyid], monkeys, part)
end
end
end
@@ -150,6 +159,13 @@ end
@info part_1(input)
function part_2(input)
- nothing
+ monkeys = MonkeyBusiness.makemonkeys(input)
+ MonkeyBusiness.monkeyrounds!(monkeys, 10_000, 2)
+ reduce(
+ *,
+ partialsort(map(x->x.considerations, values(monkeys)),
+ 1:2, rev = true)
+ )
end
+@assert part_2(example) == 2713310158
@info part_2(input)