Day 7 (re-using a slightly modified/extended version of the Day5 code)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.basdado.adventofcode.day5
|
||||
|
||||
import com.basdado.adventofcode.line
|
||||
import java.util.*
|
||||
|
||||
const val DAY5_INPUT_PATH = "/day/5/input.txt"
|
||||
|
||||
@@ -30,18 +31,28 @@ object Day5 {
|
||||
|
||||
fun puzzle1() {
|
||||
|
||||
val program = Program(line(DAY5_INPUT_PATH).split(",").map { it.toInt() }.toIntArray(), puzzle1OpCodes,
|
||||
1)
|
||||
val program = loadProgram(DAY5_INPUT_PATH, puzzle1OpCodes, intArrayOf(1))
|
||||
|
||||
program.execute()
|
||||
|
||||
println(program.getOutputs().last())
|
||||
}
|
||||
|
||||
fun puzzle2() {
|
||||
|
||||
val program = Program(line(DAY5_INPUT_PATH).split(",").map { it.toInt() }.toIntArray(), puzzle2OpCodes,
|
||||
5)
|
||||
val program = loadProgram(DAY5_INPUT_PATH, puzzle2OpCodes, intArrayOf(5))
|
||||
|
||||
program.execute()
|
||||
|
||||
println(program.getOutputs().last())
|
||||
}
|
||||
|
||||
fun loadProgram(inputPath: String, opCodes: Map<Int, OpCode>, input: IntArray): Program {
|
||||
return Program(loadProgramData(inputPath), opCodes, input)
|
||||
}
|
||||
|
||||
fun loadProgramData(inputPath: String): IntArray {
|
||||
return line(inputPath).split(",").map { it.toInt() }.toIntArray()
|
||||
}
|
||||
|
||||
class Instruction(
|
||||
@@ -62,9 +73,13 @@ object Day5 {
|
||||
}
|
||||
}
|
||||
|
||||
class Program(val data: IntArray, val opCodes: Map<Int, OpCode>, val input: Int,
|
||||
class Program(val data: IntArray, val opCodes: Map<Int, OpCode>, var inputs: IntArray,
|
||||
var pointer: Int = 0) {
|
||||
|
||||
private var inputPointer: Int = 0
|
||||
private val outputs = mutableListOf<Int>()
|
||||
var done = false
|
||||
|
||||
fun getValue(instruction: Instruction, i: Int): Int {
|
||||
return if (instruction.paramModeImmediate[i]) {
|
||||
data[instruction.pos + i + 1]
|
||||
@@ -78,20 +93,43 @@ object Day5 {
|
||||
data[data[instruction.pos + i + 1]] = x
|
||||
}
|
||||
|
||||
fun getNextInput(): Int {
|
||||
return inputs[inputPointer++]
|
||||
}
|
||||
|
||||
fun pushInput(input: Int) {
|
||||
inputs += input
|
||||
}
|
||||
fun getOutputs(): List<Int> {
|
||||
return outputs
|
||||
}
|
||||
|
||||
fun execute() {
|
||||
while (data[pointer] != 99) {
|
||||
val output = executeUntilOutput()
|
||||
}
|
||||
done = true
|
||||
}
|
||||
|
||||
fun executeUntilOutput(): Int? {
|
||||
|
||||
while (data[pointer] != 99) {
|
||||
|
||||
val instruction = Instruction.parse(data[pointer], pointer, opCodes)
|
||||
val operationResult = instruction.opcode.operation.invoke(instruction, this)
|
||||
|
||||
if (operationResult.output != null) {
|
||||
println(operationResult.output)
|
||||
}
|
||||
|
||||
if (operationResult.moveToNextInstruction) {
|
||||
pointer += instruction.opcode.paramCount + 1
|
||||
}
|
||||
|
||||
if (operationResult.output != null) {
|
||||
outputs.add(operationResult.output)
|
||||
return operationResult.output
|
||||
}
|
||||
}
|
||||
done = true
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +159,7 @@ object Day5 {
|
||||
}
|
||||
|
||||
private fun saveInput(instruction: Instruction, program: Program): OperationResult {
|
||||
program.setValue(instruction, 0, program.input)
|
||||
program.setValue(instruction, 0, program.getNextInput())
|
||||
return VOID
|
||||
}
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ object Day6 {
|
||||
val youToRoot = pathToRoot("YOU", orbits).reversed()
|
||||
val sanToRoot = pathToRoot("SAN", orbits).reversed()
|
||||
|
||||
println(youToRoot)
|
||||
println(sanToRoot)
|
||||
// println(youToRoot)
|
||||
// println(sanToRoot)
|
||||
|
||||
val overlapLength = youToRoot.indexOfLast { sanToRoot.contains(it) } + 1
|
||||
println((youToRoot.size - 1 - overlapLength) + (sanToRoot.size - 1 - overlapLength))
|
||||
|
||||
125
src/main/kotlin/com/basdado/adventofcode/day7/Day7.kt
Normal file
125
src/main/kotlin/com/basdado/adventofcode/day7/Day7.kt
Normal file
@@ -0,0 +1,125 @@
|
||||
package com.basdado.adventofcode.day7
|
||||
|
||||
import com.basdado.adventofcode.day5.Day5
|
||||
|
||||
const val DAY7_INPUT_PATH = "/day/7/input.txt"
|
||||
|
||||
fun main() {
|
||||
Day7.puzzle1()
|
||||
Day7.puzzle2()
|
||||
}
|
||||
|
||||
object Day7 {
|
||||
|
||||
fun puzzle1() {
|
||||
|
||||
val programData = Day5.loadProgramData(DAY7_INPUT_PATH)
|
||||
var maxOutput = Int.MIN_VALUE
|
||||
var maxOutputPhases = ""
|
||||
|
||||
for (A in 0..4) {
|
||||
|
||||
val programA = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(A, 0))
|
||||
programA.execute()
|
||||
val outputA = programA.getOutputs().last()
|
||||
|
||||
for (B in 0..4) {
|
||||
|
||||
if (B == A) continue
|
||||
|
||||
val programB = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(B, outputA))
|
||||
programB.execute()
|
||||
val outputB = programB.getOutputs().last()
|
||||
|
||||
for (C in 0..4) {
|
||||
|
||||
if (C == A || C == B) continue
|
||||
|
||||
val programC = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(C, outputB))
|
||||
programC.execute()
|
||||
val outputC = programC.getOutputs().last()
|
||||
|
||||
for (D in 0..4) {
|
||||
|
||||
if (D == A || D == B || D == C) continue
|
||||
|
||||
val programD = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(D, outputC))
|
||||
programD.execute()
|
||||
val outputD = programD.getOutputs().last()
|
||||
|
||||
for (E in 0..4) {
|
||||
|
||||
if (E == A || E == B || E == C || E == D) continue
|
||||
|
||||
val programE = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(E, outputD))
|
||||
programE.execute()
|
||||
val outputE = programE.getOutputs().last()
|
||||
// Beautiful
|
||||
if (outputE > maxOutput) {
|
||||
maxOutput = outputE
|
||||
maxOutputPhases = "" + A + B + C + D + E
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println("Max output $maxOutput using phases $maxOutputPhases")
|
||||
|
||||
}
|
||||
|
||||
fun puzzle2() {
|
||||
|
||||
println(permute(listOf(5, 6, 7, 8, 9))
|
||||
.map { Pair(it, executeFeedbackLoop(it.toIntArray())) }
|
||||
.filter { it.second != null }
|
||||
.maxBy { it.second!! })
|
||||
|
||||
}
|
||||
|
||||
fun <T> permute(list:List <T>):List<List<T>>{
|
||||
|
||||
if(list.size==1) return listOf(list)
|
||||
val perms= mutableListOf<List<T>>()
|
||||
val sub= list[0]
|
||||
for(perm in permute(list.drop(1)))
|
||||
for (i in 0..perm.size){
|
||||
val newPerm=perm.toMutableList()
|
||||
newPerm.add(i,sub)
|
||||
perms.add(newPerm)
|
||||
}
|
||||
return perms
|
||||
}
|
||||
|
||||
fun executeFeedbackLoop(phaseSettings: IntArray): Int? {
|
||||
|
||||
val programData = Day5.loadProgramData(DAY7_INPUT_PATH)
|
||||
|
||||
val ampA = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(phaseSettings[0]))
|
||||
val ampB = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(phaseSettings[1]))
|
||||
val ampC = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(phaseSettings[2]))
|
||||
val ampD = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(phaseSettings[3]))
|
||||
val ampE = Day5.Program(programData.clone(), Day5.puzzle2OpCodes, intArrayOf(phaseSettings[4]))
|
||||
|
||||
|
||||
var ampAInput = 0
|
||||
var anyAmpRunning = true
|
||||
while (anyAmpRunning) {
|
||||
ampA.pushInput(ampAInput)
|
||||
val ampBInput = ampA.executeUntilOutput() ?: ampA.getOutputs().last()
|
||||
ampB.pushInput(ampBInput)
|
||||
val ampCInput = ampB.executeUntilOutput() ?: ampA.getOutputs().last()
|
||||
ampC.pushInput(ampCInput)
|
||||
val ampDInput = ampC.executeUntilOutput() ?: ampA.getOutputs().last()
|
||||
ampD.pushInput(ampDInput)
|
||||
val ampEInput = ampD.executeUntilOutput() ?: ampA.getOutputs().last()
|
||||
ampE.pushInput(ampEInput)
|
||||
ampAInput = ampE.executeUntilOutput() ?: ampA.getOutputs().last()
|
||||
|
||||
anyAmpRunning = !ampA.done || !ampB.done || !ampC.done || !ampD.done || !ampE.done
|
||||
}
|
||||
return ampE.getOutputs().lastOrNull()
|
||||
}
|
||||
}
|
||||
1
src/main/resources/day/7/ex1.txt
Normal file
1
src/main/resources/day/7/ex1.txt
Normal file
@@ -0,0 +1 @@
|
||||
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0
|
||||
1
src/main/resources/day/7/ex2.txt
Normal file
1
src/main/resources/day/7/ex2.txt
Normal file
@@ -0,0 +1 @@
|
||||
3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0
|
||||
1
src/main/resources/day/7/input.txt
Normal file
1
src/main/resources/day/7/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
3,8,1001,8,10,8,105,1,0,0,21,42,67,88,101,114,195,276,357,438,99999,3,9,101,3,9,9,1002,9,4,9,1001,9,5,9,102,4,9,9,4,9,99,3,9,1001,9,3,9,1002,9,2,9,101,2,9,9,102,2,9,9,1001,9,5,9,4,9,99,3,9,102,4,9,9,1001,9,3,9,102,4,9,9,101,4,9,9,4,9,99,3,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,101,4,9,9,1002,9,5,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,99
|
||||
Reference in New Issue
Block a user