-
-
Save tong92/f92838e08b5652d3d40201bdb3ec4669 to your computer and use it in GitHub Desktop.
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
import scala.io.Source | |
def matchHalf(xs: String): String = | |
val half = xs.take(xs.length / 2) | |
val otherHalf = xs.takeRight(xs.length / 2) | |
half.filter(otherHalf.contains) | |
def stringToPriority(s: String): Int = | |
val c = s.charAt(0) | |
c.isLower match | |
case true => c - 'a' + 1 | |
case _ => c - 'A' + 27 | |
def ans1(xs: List[String]): Int = xs.map(matchHalf andThen stringToPriority).sum | |
def matchBag(a: String, b: String): String = a.filter(b.contains) | |
def ans2(xs: List[String]): Int = | |
xs.grouped(3).map(bags => stringToPriority(matchBag(matchBag(bags(0), bags(1)), bags(2)))).sum | |
def solve = | |
val input = Source.fromFile("day3.txt").getLines.toList | |
println(ans1(input)) | |
println(ans2(input)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment