Skip to content

Commit 85a06ef

Browse files
committed
day 10, bigints to avoid overflow
1 parent ce129cb commit 85a06ef

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed
Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1-
16
2-
10
3-
15
4-
5
5-
1
1+
28
2+
33
3+
18
4+
42
5+
31
6+
14
7+
46
8+
20
9+
48
10+
47
11+
24
12+
23
13+
49
14+
45
15+
19
16+
38
17+
39
618
11
19+
1
20+
32
21+
25
22+
35
23+
8
24+
17
725
7
8-
19
9-
6
10-
12
11-
4
26+
9
27+
4
28+
2
29+
34
30+
10
31+
3

adventofcode2020/src/main/kotlin/com/matsemann/adventofcode2020/Day10.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ fun day10_2(lines: List<String>,) {
3030
.plus(numbers.last() + 3)
3131
.sorted()
3232

33-
val counts = mutableMapOf<Int, Int>()
34-
counts[0] = 1
33+
val counts = mutableMapOf<Int, BigInteger>()
34+
counts[0] = BigInteger.ONE
3535

3636
adaptors.forEach {
37-
val sum = (counts[it - 3] ?: 0) + (counts[it - 2] ?: 0) + (counts[it-1] ?: 0)
37+
val sum = (counts[it - 3] ?: BigInteger.ZERO) + (counts[it - 2] ?: BigInteger.ZERO) + (counts[it-1] ?: BigInteger.ZERO)
3838
counts[it] = sum
3939
}
40-
println()
40+
41+
val ways = counts.maxOf { it.value }
42+
println("ways: $ways")
4143

4244

4345
/*

adventofcode2020/src/test/kotlin/com/matsemann/adventofcode2020/Runner.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class Runner {
126126
}
127127
@Test
128128
fun day10_2Runner() {
129-
val lines = getFileLines("inputs/day10_example.txt").toList()
129+
val lines = getFileLines("inputs/day10_1.txt").toList()
130130
day10_2(lines)
131131
}
132132

0 commit comments

Comments
 (0)