This is a solution to Advent of Code 2022 day 3, written in Raku.
https://adventofcode.com/2022/day/3
Part One
Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item types?
my @rucksacks = '3-input.txt'.IO.lines.map({
my @contents = .comb;
my Int $half = +@contents div 2;
my @i = [(&)] @contents[^$half, $half..*];
given @i.head.key {
when / <[a..z]>/ { $_.ord - 'a'.ord + 1 }
when / <[A..Z]> / { $_.ord - 'A'.ord + 27 }
}
});
say [+] @rucksacks;
7597
Part Two
Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those item types?
my @groups = '3-input.txt'.IO.lines.map({ .comb }).map(-> @a, @b, @c, {
my $i = (@a (&) @b (&) @c).head.key;
given $i {
when / <[a..z]>/ { $_.ord - 'a'.ord + 1 }
when / <[A..Z]> / { $_.ord - 'A'.ord + 27 }
}
});
say [+] @groups;
2607