advent_of_code_2022

My (attempted) solutions to the 2022 Advent of Code
git clone https://git.eamoncaddigan.net/advent_of_code_2022.git
Log | Files | Refs | README

commit f84dd861d09545ffe1da81ca7ceb57fbf9802f30
parent cf1d38d09caf7e3ba5a892b66df4de627607fd0a
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Sun,  4 Dec 2022 04:51:12 -0800

Reworked solution with cool Julia stuff I learned.

I'm still updating my solutions with cool snippets from other folks'
Julia solutions (but only after finding a solution, natch). This has
been updated to include the \div operator, Iterators.partition, and a
slick (but technically unnecessary) use of reduce.

Diffstat:
Msrc/day_3.jl | 15+++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/day_3.jl b/src/day_3.jl @@ -19,8 +19,8 @@ function get_compartments(rucksack) rucksack = Vector{UInt8}(rucksack) rucksack = map(x->(x>Int('Z')) ? x-Int('a')+1 : x-Int('A')+27, rucksack) - (rucksack[1:Integer(length(rucksack)/2)], - rucksack[Integer(length(rucksack)/2+1):end]) + (rucksack[1:end÷2], + rucksack[end÷2+1:end]) end # This is written to assume there's only one item in both compartments @@ -41,14 +41,17 @@ end # operator `...`. # This function takes a group of rucksacks (in string format) and finds the -# value of the "badge" +# value of the "badge". Also, you can splat the input to intersect (and I did +# that for my original solution), but I saw somebody else call reduce and that +# feels worth cribbing (even if it's no more efficient). function get_rucksacks_badge(rucksacks) - intersect(map(x->union(x...), map(get_compartments, rucksacks))...)[1] + reduce(intersect, + map(x->union(get_compartments(x)...),rucksacks))[1] end function part_2(input) - sum([get_rucksacks_badge(input[i:i+2]) - for i = 1:3:length(input)]) + sum(map(get_rucksacks_badge, + Iterators.partition(input, 3))) end @assert part_2(example) == 70 @info part_2(input)