Skip to content

Instantly share code, notes, and snippets.

@tong92
Last active February 10, 2023 12:24
Show Gist options
  • Save tong92/f92838e08b5652d3d40201bdb3ec4669 to your computer and use it in GitHub Desktop.
Save tong92/f92838e08b5652d3d40201bdb3ec4669 to your computer and use it in GitHub Desktop.
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