-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.scala
63 lines (49 loc) · 1.46 KB
/
14.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
val input = 360781
val inputString = input.toString
class Recipe(var next: Recipe, val value: Int) {
override def toString: String = s"R($value)"
}
object Recipe {
def apply(a0: => Recipe, b: Int) = new Recipe(a0, b)
}
var first: Recipe = Recipe(null, 3)
var second: Recipe = Recipe(first, 7)
first.next = second
var last = second
val start = first
lazy val scoreboard: Stream[Recipe] = start #:: scoreboard.map(_.next)
// Super lazy and hacky approach by pregenerating a lot of recipes and
// hoping the subsequence from part B will appear in the sequence
// A proper solution would be to create a lazily evaluated stream
// Unfortunately there's a lot of reference reconnecting which doesn't
// work that easily in immutable code
for (_ <- 1 to 50 * input) {
val sum = first.value + second.value
val digit = sum % 10
val tensDigit = (sum / 10) % 10
if (tensDigit > 0) {
val secondNew = Recipe(start, tensDigit)
last.next = secondNew
last = secondNew
}
val firstNew = Recipe(start, digit)
last.next = firstNew
last = firstNew
for (_ <- 0 until first.value + 1) {
first = first.next
}
for (_ <- 0 until second.value + 1) {
second = second.next
}
}
scoreboard.slice(input, input + 10).foreach(r => print(r.value))
println
val resultB = scoreboard
.zipWithIndex
.sliding(inputString.length, 1)
.dropWhile(window => window.map(_._1.value).mkString("") != inputString)
.take(1)
.toStream
.head
.head
println(resultB._2)