Skip to content

Commit f86fff6

Browse files
committed
Solution 2017-17 (Spinlock)
1 parent dfaf60d commit f86fff6

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package de.ronny_h.aoc.year2017.day17
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
5+
fun main() = Spinlock().run(2000, 10242889)
6+
7+
class Spinlock : AdventOfCode<Int>(2017, 17) {
8+
override fun part1(input: List<String>): Int = insertIntoCircularBuffer(input.single().toInt())
9+
override fun part2(input: List<String>): Int = simulateInsertIntoCircularBuffer(input.single().toInt())
10+
11+
private fun insertIntoCircularBuffer(steps: Int): Int {
12+
val circularBuffer = mutableListOf<Int>()
13+
var index = 0
14+
repeat(2017) { i ->
15+
circularBuffer.add(index, i)
16+
index = (index + steps) % circularBuffer.size + 1
17+
}
18+
return circularBuffer[index]
19+
}
20+
21+
private fun simulateInsertIntoCircularBuffer(steps: Int): Int {
22+
var bufferSize = 0
23+
var index = 0
24+
var atIndex1 = -1
25+
repeat(50_000_000) { i ->
26+
if (index == 1) {
27+
atIndex1 = i
28+
}
29+
bufferSize++
30+
index = (index + steps) % bufferSize + 1
31+
}
32+
return atIndex1
33+
}
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package de.ronny_h.aoc.year2017.day17
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.matchers.shouldBe
5+
6+
class SpinlockTest : StringSpec({
7+
8+
"part 1: the value after the last value written (2017)" {
9+
Spinlock().part1(listOf("3")) shouldBe 638
10+
}
11+
12+
"part 2: the value on index 1 (the one after value 0) the moment 50000000 is inserted" {
13+
Spinlock().part2(listOf("3")) shouldBe 1222153
14+
}
15+
})

0 commit comments

Comments
 (0)