-
-
Save waynejo/695308246a91b2e4373b32b1831e26cf 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 java.io.FileInputStream | |
import scala.io.StdIn | |
@main def solve10() = | |
def solve1(numbers: Vector[Int]): Int = { | |
val jolts = 0 +: numbers.sorted :+ (numbers.max + 3) | |
val gabs = jolts.sliding(2).foldLeft(Map[Int, Int]()) { (acc, x) => | |
val gab = x(1) - x(0) | |
acc.updated(gab, acc.getOrElse(gab, 0) + 1) | |
} | |
gabs(1) * gabs(3) | |
} | |
def solve2(numbers: Vector[Int]): BigInt = { | |
val jolts = 0 +: numbers.sorted :+ (numbers.max + 3) | |
val numberOfWaysToArrange = jolts.indices.foldLeft(Array.fill(jolts.size)(BigInt(0))) { (acc, idx) => | |
if 0 == idx then | |
acc(idx) = 1 | |
else | |
(1 to 3).foreach(delta => | |
if 0 <= idx - delta && 3 >= jolts(idx) - jolts(idx - delta) then | |
acc(idx) += acc(idx - delta) | |
) | |
acc | |
} | |
numberOfWaysToArrange.last | |
} | |
val in = new FileInputStream("example10-1.in") | |
System.setIn(in) | |
val inputs = Iterator.continually(StdIn.readLine()).takeWhile(_ != null).toVector | |
val numbers = inputs.map { line => line.toInt } | |
val answer1 = solve1(numbers) | |
println(answer1) | |
val answer2 = solve2(numbers) | |
println(answer2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment