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 6779cfa5c75ac163437cb9a67907825067a42d04
parent 734acf76c6759956e6478d8ed5f089cdb03278a6
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Tue, 13 Dec 2022 21:48:26 -0800

Solution to day 13, part 1.

Diffstat:
Asrc/day_13.jl | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+), 0 deletions(-)

diff --git a/src/day_13.jl b/src/day_13.jl @@ -0,0 +1,84 @@ +# https://adventofcode.com/2022/day/13 +using AdventOfCode + +example = readlines(IOBuffer(""" + [1,1,3,1,1] + [1,1,5,1,1] + + [[1],[2,3,4]] + [[1],4] + + [9] + [[8,7,6]] + + [[4,4],4,4] + [[4,4],4,4,4] + + [7,7,7,7] + [7,7,7] + + [] + [3] + + [[[]]] + [[]] + + [1,[2,[3,[4,[5,6,7]]]],8,9] + [1,[2,[3,[4,[5,6,0]]]],8,9]""")) +input = readlines("data/day_13.txt") + +""" +Compare packets using AoC day 13 logic. +""" +function comparesides(left::Int, right::Int) + if left < right + "right" + elseif left > right + "wrong" + else + "continue" + end +end + +function comparesides(left::Vector{<:Any}, right::Vector{<:Any}) + for i = 1:min(length(left), length(right)) + i_result = comparesides(left[i], right[i]) + if i_result != "continue" + return i_result + end + end + + if length(left) < length(right) + "right" + elseif length(left) > length(right) + "wrong" + else + "continue" + end +end + +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. +""" +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]) + ) +end + +function part_1(input) + sum(findall(map(x->comparesides(x[1], x[2]), parse_input(input)) .== "right")) +end +@assert part_1(example) == 13 +@info part_1(input) + +function part_2(input) + nothing +end +@info part_2(input)