Created
December 3, 2022 19:14
-
-
Save smortex/84451e1f576f0e14a3dcc3d2b0ba3449 to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function aoc::letter2number(String $n) >> Integer { | |
$offset = $n ? { | |
/[a-z]/ => 1, | |
/[A-Z]/ => 27, | |
} | |
'abcdefghijklmnopqrstuvwxyz'.index |$char| { $char == $n } + $offset | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plan aoc::day3 ( | |
Boolean $sample = false, | |
) { | |
$file = $sample ? { | |
false => 'aoc/2022/day3.txt', | |
true => 'aoc/2022/day3.sample.txt', | |
} | |
$data = file($file) | |
$result1 = $data.split('\n').map |$line| { | |
$split_at = $line.size / 2 | |
$part1 = $line[0, $split_at].split('') | |
$part2 = $line[$split_at, -1].split('') | |
$n = intersection($part1, $part2)[0] | |
$n.aoc::letter2number | |
}.reduce |$memo, $value| { $memo + $value } | |
out::message($result1) | |
$result2 = $data.split('\n').slice(3).map |$a| { | |
$n = intersection(intersection($a[0].split(''), $a[1].split('')), $a[2].split(''))[0] | |
$n.aoc::letter2number | |
}.reduce |$memo, $value| { $memo + $value } | |
out::message($result2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment