Created
December 3, 2022 20:40
-
-
Save cyberzac/dda360711845fe097dcb0d0025c062e1 to your computer and use it in GitHub Desktop.
AdventOfCode 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
object Day3 extends Day : | |
def toPrio(c:Char):Int = c match { | |
case c if c > 'Z' => c - '`' | |
case c => c - '@' + 26 | |
} | |
def part1(input:Iterator[String]) = input | |
.map(r => r.splitAt(r.length/2)) | |
.map { case (f, s) => f.toSet.intersect(s.toSet).head } | |
.map(toPrio) | |
.sum | |
def part2(input:Iterator[String]) = input | |
.sliding(3,3) | |
.map(_.foldLeft(Set.empty[Char]) { | |
case (common, sack) if common.isEmpty => sack.toSet | |
case (common, sack) => common.intersect(sack.toSet) | |
}.head | |
) | |
.map(toPrio) | |
.sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment