Skip to content

Commit e8e7622

Browse files
committed
Solution 2017-23, part 2 (Coprocessor Conflagration)
1 parent 5eb799a commit e8e7622

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

src/main/kotlin/de/ronny_h/aoc/year2017/day23/CoprocessorConflagration.kt

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import de.ronny_h.aoc.AdventOfCode
44
import de.ronny_h.aoc.year2017.day18.Program
55
import kotlinx.coroutines.runBlocking
66

7-
fun main() = CoprocessorConflagration().run(4225, 0)
7+
fun main() = CoprocessorConflagration().run(4225, 905)
88

99
class CoprocessorConflagration : AdventOfCode<Long>(2017, 23) {
1010
override fun part1(input: List<String>): Long = runBlocking {
@@ -13,57 +13,45 @@ class CoprocessorConflagration : AdventOfCode<Long>(2017, 23) {
1313
return@runBlocking program.getNumberOfMultiplies()
1414
}
1515

16-
override fun part2(input: List<String>): Long = runBlocking {
17-
val program = Program(input, presetRegisters = mapOf("a" to 1))
18-
program.run()
19-
return@runBlocking program.getRegisterValue("h")
20-
}
16+
override fun part2(input: List<String>): Long = partTwoAsKotlinOptimized().toLong()
2117
}
2218

19+
// This is an intermediate step of simplifying the program given in my puzzle input.
20+
// It keeps the original variable names but uses a higher level of language features
21+
// that make it easier to see what the program actually does.
22+
// It does not terminate in reasonable time and is not tested. The optimized function
23+
// below is my final solution.
2324
private fun partTwoAsKotlin(): Int {
24-
val a = 1
25-
var d = 0
26-
var e = 0
27-
var f = 0
28-
var g = 0
25+
var f: Int
2926
var h = 0
3027

31-
var b = 67
32-
var c = b
33-
if (a != 0) {
34-
b *= 100
35-
b += 100000
36-
c = b
37-
c += 17000
38-
}
39-
while (true) {
28+
for (b in 106700..106700 + 17000 step 17) { // 1000 iterations
4029
f = 1
41-
d = 2
42-
do {
43-
e = 2
44-
do {
45-
g = d
46-
g *= e
47-
g -= b
48-
if (g == 0) {
30+
for (d in 2..b) {
31+
for (e in 2..b) {
32+
if (b == d * e) {
4933
f = 0
5034
}
51-
e++
52-
g = e
53-
g -= b
54-
} while (g != 0)
55-
d++
56-
g = d
57-
g -= b
58-
} while (g != 0)
35+
}
36+
}
5937
if (f == 0) {
38+
// f is 0 if b can be written as d * e (with d,e in {2..b})
39+
// -> h counts non-prime numbers between 106700 and 123700 in steps of 17
6040
h++
6141
}
62-
g = b
63-
g -= c
64-
if (g == 0) {
65-
return h
42+
}
43+
return h
44+
}
45+
46+
private fun partTwoAsKotlinOptimized(): Int {
47+
var nonPrimesCount = 0
48+
for (number in 106700..106700 + 17000 step 17) {
49+
for (divisor in 2..number / 2) {
50+
if (number % divisor == 0) {
51+
nonPrimesCount++
52+
break
53+
}
6654
}
67-
b += 17
6855
}
56+
return nonPrimesCount
6957
}

src/test/kotlin/de/ronny_h/aoc/year2017/day23/CoprocessorConflagrationTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CoprocessorConflagrationTest : StringSpec({
3232
CoprocessorConflagration().part1(input) shouldBe 1
3333
}
3434

35-
"part 2: at the end of the test program h should be 7" {
36-
CoprocessorConflagration().part2(input) shouldBe 7
35+
"part 2: the number of non-prime numbers between 106700 and 123700 in steps of 17" {
36+
CoprocessorConflagration().part2(input) shouldBe 905
3737
}
3838
})

0 commit comments

Comments
 (0)