Skip to content

Commit f880c77

Browse files
committed
Added solution for part 1
1 parent 8258f30 commit f880c77

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Day-08/kotlin/NyCodeGHG/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt

Day-08/kotlin/NyCodeGHG/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Kotlin Solution
2+
I recommend to use [kscript](https://github.com/holgerbrandl/kscript) by [@holgerbrandl](https://github.com/holgerbrandl) to execute the script file.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.nio.file.Files
2+
import java.nio.file.Paths
3+
4+
val instructions = Files.readAllLines(Paths.get("input.txt"))
5+
.map { it.split(" ") }
6+
.map { Instruction(it[0], it[1].toInt()) }
7+
8+
var accumulator = 0
9+
10+
var currentInstruction = 0
11+
12+
while (currentInstruction < instructions.size) {
13+
val instruction = instructions[currentInstruction]
14+
if (instruction.executions > 0) {
15+
println("accumulator is at $accumulator")
16+
break
17+
}
18+
currentInstruction += instruction.execute()
19+
}
20+
21+
fun Instruction.execute(): Int {
22+
this.executions++
23+
return when (type) {
24+
"acc" -> {
25+
accumulator += this.value
26+
1
27+
}
28+
"jmp" -> {
29+
this.value
30+
}
31+
"nop" -> {
32+
1
33+
}
34+
else -> {
35+
error("Invalid instruction type: $type")
36+
}
37+
}
38+
}
39+
40+
data class Instruction(val type: String, val value: Int, var executions: Int = 0)

0 commit comments

Comments
 (0)