From dba4273fc04f828f180466b80e98ba94c1a0f0db Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Mon, 2 Dec 2019 19:41:37 +0100 Subject: [PATCH] Day 2 --- .../kotlin/com/basdado/adventofcode/Utils.kt | 4 + .../com/basdado/adventofcode/day2/Day2.kt | 75 +++++++++++++++++++ src/main/resources/day/2/input.txt | 1 + 3 files changed, 80 insertions(+) create mode 100644 src/main/kotlin/com/basdado/adventofcode/day2/Day2.kt create mode 100644 src/main/resources/day/2/input.txt diff --git a/src/main/kotlin/com/basdado/adventofcode/Utils.kt b/src/main/kotlin/com/basdado/adventofcode/Utils.kt index da0916f..2af4d51 100644 --- a/src/main/kotlin/com/basdado/adventofcode/Utils.kt +++ b/src/main/kotlin/com/basdado/adventofcode/Utils.kt @@ -8,4 +8,8 @@ fun lines(resourceFile: String): Stream { return Files.lines(Paths.get(Utils::class.java.getResource(resourceFile).toURI())) } +fun line(resourceFile: String): String { + return lines(resourceFile).findFirst().get() +} + class Utils diff --git a/src/main/kotlin/com/basdado/adventofcode/day2/Day2.kt b/src/main/kotlin/com/basdado/adventofcode/day2/Day2.kt new file mode 100644 index 0000000..5c2dcf6 --- /dev/null +++ b/src/main/kotlin/com/basdado/adventofcode/day2/Day2.kt @@ -0,0 +1,75 @@ +package com.basdado.adventofcode.day2 + +import com.basdado.adventofcode.line +import com.basdado.adventofcode.lines +import java.lang.Exception + +const val DAY2_INPUT_PATH = "/day/2/input.txt" + +fun main() { + Day2.puzzle1() + Day2.puzzle2() +} + +object Day2 { + + val opcodes = mapOf( + Pair(1, ::add), + Pair(2, ::multiply) + ) + + private fun add(program: IntArray, pos1: Int, pos2: Int, pos3: Int) { + program[pos3] = program[pos1] + program[pos2] + } + + private fun multiply(program: IntArray, pos1: Int, pos2: Int, pos3: Int) { + program[pos3] = program[pos1] * program[pos2] + } + + fun puzzle1() { + val input = parseInput(line(DAY2_INPUT_PATH)) +// val input = parseInput("1,9,10,3,2,3,11,0,99,30,40,50") + input[1] = 12 + input[2] = 2 +// println(input.joinToString()) + execute(input) +// println(input.joinToString()) + println(input[0]) + } + + fun puzzle2() { + val input = parseInput(line(DAY2_INPUT_PATH)) + + for (noun in 0..100) { + for (verb in 0..100) { + val program = input.copyOf() + program[1] = noun + program[2] = verb + try { + execute(program) + } catch (e: Exception) { + // Program is buggy with these inputs...whatever + } + if (program[0] == 19690720) { + // Success! + println("noun: $noun, verb: $verb, result: ${100 * noun + verb}") + } + } + } + } + + fun parseInput(input: String): IntArray { + return input.split(",").map { it.toInt() }.toIntArray(); + } + + fun execute(program: IntArray) { + var pos = 0 + while(program[pos] != 99) { + + val op = opcodes[program[pos]] ?: error("Unknown opcode at position $pos") + op(program, program[pos+1], program[pos+2], program[pos+3]) + pos += 4 + } + + } +} \ No newline at end of file diff --git a/src/main/resources/day/2/input.txt b/src/main/resources/day/2/input.txt new file mode 100644 index 0000000..f143dc1 --- /dev/null +++ b/src/main/resources/day/2/input.txt @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,2,23,6,27,2,6,27,31,2,13,31,35,1,10,35,39,2,39,13,43,1,43,13,47,1,6,47,51,1,10,51,55,2,55,6,59,1,5,59,63,2,9,63,67,1,6,67,71,2,9,71,75,1,6,75,79,2,79,13,83,1,83,10,87,1,13,87,91,1,91,10,95,2,9,95,99,1,5,99,103,2,10,103,107,1,107,2,111,1,111,5,0,99,2,14,0,0 \ No newline at end of file