day 10 finally

master
Shoofle 10 months ago
parent 5ed8eb6433
commit b2cabd079b
  1. 70
      day10/src/Main.kt

@ -1,3 +1,71 @@
import Instruction.Type.*
import java.io.File
const val limit = 300;
data class Instruction(val type: Type, val value: Int) {
enum class Type {
NOOP,
ADDX
}
}
fun main() {
println("Hello World!")
val instructions = MutableList<Instruction>(0) { Instruction(NOOP, 0) };
val cyclesOfInterest = setOf(20, 60, 100, 140, 180, 220)
for (line in File("c.input").readLines()) {
if (line.isEmpty()) continue;
val s = line.split(" ");
if (s[0] == "noop") {
instructions.add(Instruction(NOOP, 0));
}
if (s[0] == "addx") {
instructions.add(Instruction(ADDX, s[1].toInt()));
}
}
for (i in instructions) {
println("$i")
}
var signalSum = 0;
var cycle = 0;
var pc = 0;
var x = 1;
var command = instructions[cycle];
var currentTimer = 0;
while (cycle < limit) {
// At the start of the first cycle, the noop instruction begins execution.
if (cycle % 40 == 0) print("\n")
if (currentTimer == 0) {
command = instructions[pc % instructions.count()]
when (command.type) {
NOOP -> currentTimer = 1
ADDX -> currentTimer = 2
}
}
// During the first cycle, X is 1.
val spriteLocations = setOf(x-1, x, x+1)
if (cycle % 40 in spriteLocations) print("#")
else print(".")
//println("during cycle ${cycle+1}, we're executing $command the register is $x")
signalSum += (cycle+1) * x
// After the first cycle, the noop instruction finishes execution, doing nothing.
currentTimer -= 1
if (currentTimer == 0) {
// if the timer is at zero, we execute the existing command.
when (command.type) {
NOOP -> {}
ADDX -> x += command.value
}
pc++
}
cycle += 1
}
println("after all is said and done,, the sum is $signalSum")
}
Loading…
Cancel
Save