-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day10.scala
26 lines (20 loc) · 839 Bytes
/
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
package AdventOfCode2015
import collection.mutable.ArrayBuffer
object Day10:
def step(initial: ArrayBuffer[Char]): ArrayBuffer[Char] =
val buffer = ArrayBuffer[Char]()
initial.appended('x').foldLeft(0 -> initial.head) { case ((count, previous), next) =>
if previous == next then
(count + 1, previous)
else
buffer.append((count + '0').toChar).append(previous)
(1, next)
}
buffer
def generate(input: String, count: Int): Int = Iterator.iterate(ArrayBuffer.from(input))(step).drop(count).next().size
def part1(input: String): Int = generate(input, 40)
def part2(input: String): Int = generate(input, 50)
def main(args: Array[String]): Unit =
val data = io.Source.fromResource("AdventOfCode2015/Day10.txt").mkString.trim
println(part1(data))
println(part2(data))