Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created February 10, 2023 11:55
Show Gist options
  • Save waynejo/1f426263a407e8a7b0b2e12cbbbd65b3 to your computer and use it in GitHub Desktop.
Save waynejo/1f426263a407e8a7b0b2e12cbbbd65b3 to your computer and use it in GitHub Desktop.
import java.io.FileInputStream
import scala.annotation.tailrec
import scala.io.StdIn
def char2Num(c: Char): Int =
if 'A' <= c && c <= 'Z' then
c - 'A' + 27
else
c - 'a' + 1
def solve3_1(values: Vector[String]): Int =
values.map(line => {
val firstCompartment = line.take(line.length / 2).toSet
val secondCompartment = line.drop(line.length / 2).toSet
val item: Char = firstCompartment.intersect(secondCompartment).head
char2Num(item)
}).sum
def solve3_2(values: Vector[String]): Int =
values.grouped(3).map(lines => {
val badge = lines(0).toSet.intersect(lines(1).toSet).intersect(lines(2).toSet)
char2Num(badge.head)
}).sum
@main def solve3(): Unit =
val in = new FileInputStream("example3-2.in")
System.setIn(in)
val inputs = Iterator.continually(StdIn.readLine())
.takeWhile(line => null != line)
.toVector
println(solve3_1(inputs))
println(solve3_2(inputs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment