Day 2
This commit is contained in:
@@ -8,4 +8,8 @@ fun lines(resourceFile: String): Stream<String> {
|
|||||||
return Files.lines(Paths.get(Utils::class.java.getResource(resourceFile).toURI()))
|
return Files.lines(Paths.get(Utils::class.java.getResource(resourceFile).toURI()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun line(resourceFile: String): String {
|
||||||
|
return lines(resourceFile).findFirst().get()
|
||||||
|
}
|
||||||
|
|
||||||
class Utils
|
class Utils
|
||||||
|
|||||||
75
src/main/kotlin/com/basdado/adventofcode/day2/Day2.kt
Normal file
75
src/main/kotlin/com/basdado/adventofcode/day2/Day2.kt
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/main/resources/day/2/input.txt
Normal file
1
src/main/resources/day/2/input.txt
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user