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 d192fc570215a53f691896191f9bb005c9119134
parent 7487d45e43c9b8d7d323a6d6befd9a5525e2aeaa
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Sun,  4 Dec 2022 03:57:53 -0800

Solution to day 3, part 1.

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

diff --git a/src/day_3.jl b/src/day_3.jl @@ -0,0 +1,41 @@ +#!/usr/bin/env julia +# https://adventofcode.com/2022/day/3 +using AdventOfCode + +example = split(""" + vJrwpWtwJgWrhcsFMMfFFhFp + jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL + PmmdzqPrVvPwwTWBwg + wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn + ttgJtRGJQctTZtZT + CrZsJsPPZsGzwwsLwLmpwMDw""", + "\n") +input = readlines("data/day_3.txt") + +# Return a tuple of arrays for a rucksack, consisting of the priority values of +# all of the (unique) items in the compartment. This abuses ASCII/UTF-8 +# encoding of [a-zA-Z] +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]) +end + +# This is written to assume there's only one item in both compartments +function get_compartment_intersection_priority(rucksack) + compartments = get_compartments(rucksack) + intersect(compartments[1], compartments[2])[1] +end + +function part_1(input) + sum(map(get_compartment_intersection_priority, input)) +end +@assert part_1(example) == 157 +@info part_1(input) + +function part_2(input) + nothing +end +@info part_2(input)