Skip to content

Instantly share code, notes, and snippets.

@Boltovnya
Last active December 10, 2020 19:11
Show Gist options
  • Save Boltovnya/a3fa3447f632bd4a71af4df69312702f to your computer and use it in GitHub Desktop.
Save Boltovnya/a3fa3447f632bd4a71af4df69312702f to your computer and use it in GitHub Desktop.
def part1(joltages: IndexedSeq[Int]): Int = {
def findJolts(it: Int = 0, jo: IndexedSeq[Int] = joltages, on: IndexedSeq[Int] = IndexedSeq(), tr: IndexedSeq[Int] = IndexedSeq()): (IndexedSeq[Int], IndexedSeq[Int]) =
@tailrec
def iter(i: Int, j: IndexedSeq[Int], o: IndexedSeq[Int], t: IndexedSeq[Int]): (IndexedSeq[Int], IndexedSeq[Int]) =
val tup = (j.sorted filter((x: Int) => (x - i == 1) | (x - i == 3))).head match {
case x if i + 1 == x => (x, o :+ x, t)
case x if i + 3 == x => (x, o, t:+ x)
if (tup._2.size + tup._3.size == j.length) return (tup._2, tup._3)
iter(tup._1, j.filter(_ != tup._1), tup._2, tup._3)
iter(it, jo, on, tr)
}
val x = findJolts()
x._1.length * x._2.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment