From b2cabd079b7c2e11c74c0fbad848e0eb1de17f17 Mon Sep 17 00:00:00 2001 From: shoofle Date: Sat, 16 Dec 2023 18:41:39 -0500 Subject: [PATCH] day 10 finally --- day10/src/Main.kt | 70 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/day10/src/Main.kt b/day10/src/Main.kt index a7a5e00..89d1938 100644 --- a/day10/src/Main.kt +++ b/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(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") } \ No newline at end of file