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 76957adbbe41092c3a00cb022c3ca865e6433bfd
parent 25de54a3a55bb34d9111fb6915cef329d0896296
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Wed, 14 Dec 2022 08:45:01 -0800

Post-solution code cleanup.

I was being lazy about parsing parts 1 and 2 separately so I rewrote the
parsing and solving to be more DRY. Along the way I learned that `|>`
is higher precedence than `==`, which surprised me.

Diffstat:
Msrc/day_13.jl | 25++++++++-----------------
1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/src/day_13.jl b/src/day_13.jl @@ -63,33 +63,24 @@ comparesides(left::Int, right::Vector{<:Any}) = comparesides([left], right) comparesides(left::Vector{<:Any}, right::Int) = comparesides(left, [right]) """ -Read the input and return a list of pairs of lists. +Read the input and return a list of packets. """ function parse_input(input) - map( - (x, y) -> (x, y), - map(eval ∘ Meta.parse, input[1:3:end]), - map(eval ∘ Meta.parse, input[2:3:end]) - ) + map(eval ∘ Meta.parse, Iterators.filter(x->x!="", input)) end function part_1(input) - sum(findall(map(x->comparesides(x[1], x[2]), parse_input(input)) .== "right")) + packetorder = map(x->comparesides(x[1], x[2]), + Iterators.partition(parse_input(input), 2)) + (packetorder .== "right") |> findall |> sum end @assert part_1(example) == 13 @info part_1(input) -# Redoing my parsing function for part 2 -function parse_input2(input) - [ - map(eval ∘ Meta.parse, Iterators.filter(x -> x != "", input)); - [ [[2]], [[6]] ] - ] -end - function part_2(input) - packets = parse_input2(input) - sort!(packets, lt = (x,y)->comparesides(x, y) == "right") + packets = parse_input(input) + append!(packets, [ [[2]], [[6]] ]) + sort!(packets, lt = (x, y)->comparesides(x, y) == "right") findfirst(==([[2]]), packets) * findfirst(==([[6]]), packets) end @assert part_2(example) == 140