-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.scala
66 lines (54 loc) · 1.51 KB
/
day10.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Advent of Code 2020, Day 10
// (c) blu3r4y
import scala.io.Source
object Day10 {
val PATH = "/home/data/day10.txt"
def main(args: Array[String]) = {
val jolts = load(PATH)
System.out.println(part1(jolts))
System.out.println(part2(jolts))
}
def load(path: String): List[Int] = {
var jolts = Source.fromFile(path).getLines.toList.map(_.toInt)
jolts = 0 :: jolts.max + 3 :: jolts
jolts.sorted
}
def part1(jolts: Seq[Int]): Int = {
val diffs = diff(jolts)
diffs.count(_ == 1) * diffs.count(_ == 3)
}
def part2(jolts: Seq[Int]): Long = {
val diffs = diff(jolts)
val lengths = consecutive(diffs)
val perms = lengths.map(permutations)
perms.map(_.toLong).product
}
def consecutive(seq: Seq[Int]): Seq[Int] = {
for (vals <- split(seq) if vals.head == 1)
yield vals.length
}
def permutations(n: Int): Int = {
if (n < 2) 1 else tribonacci(n + 2)
}
def tribonacci(n: Int): Int = {
n match {
case 0 => 0
case 1 => 0
case 2 => 1
case _ => tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)
}
}
def diff(seq: Seq[Int]): Seq[Int] = {
(seq.drop(1) zip seq)
.map(t => t._1 - t._2)
}
def split(seq: Seq[Int]): List[List[Int]] = {
// itertools.groupby in Scala (https://stackoverflow.com/a/4763086/927377)
seq.foldRight(List[List[Int]]()) { (e, acc) =>
acc match {
case (`e` :: xs) :: fs => (e :: e :: xs) :: fs
case _ => List(e) :: acc
}
}
}
}