Day 1 + 2
This commit is contained in:
69
day02/computer.nim
Normal file
69
day02/computer.nim
Normal file
@@ -0,0 +1,69 @@
|
||||
import sequtils, strutils
|
||||
|
||||
type
|
||||
Program* = seq[int]
|
||||
Input* = object
|
||||
noun*: int
|
||||
verb*: int
|
||||
Computer* = ref object
|
||||
source: Program # Original program code without modifications
|
||||
program*: Program # The computers program in memory
|
||||
ic*: int # The instruction counter
|
||||
|
||||
StopExecution = object of Exception
|
||||
## This will be raised when the ending opcode is reached
|
||||
|
||||
proc set(cp: Computer, address, value: int): void =
|
||||
cp.program[address] = value
|
||||
|
||||
proc get(cp: Computer, address: int): int =
|
||||
cp.program[address]
|
||||
|
||||
proc add(cp: Computer, param1, param2, param3: int): void =
|
||||
cp.set(param3, (cp.get(param1) + cp.get(param2)))
|
||||
|
||||
proc mul(cp: Computer, param1, param2, param3: int): void =
|
||||
cp.set(param3, (cp.get(param1) * cp.get(param2)))
|
||||
|
||||
proc newComputer*(sourceFile: string): Computer =
|
||||
var file = readFile("input.txt");
|
||||
file.stripLineEnd
|
||||
let source = map(split(file, ','), parseInt)
|
||||
result = Computer(source: source, ic: 0)
|
||||
|
||||
proc reset*(cp: Computer, input: Input): void =
|
||||
## Reset instruction pointer, program code and input values
|
||||
cp.program = cp.source
|
||||
cp.ic = 0
|
||||
cp.set(1, input.noun)
|
||||
cp.set(2, input.verb)
|
||||
|
||||
proc process(cp: Computer): void =
|
||||
let opCode = cp.get(cp.ic)
|
||||
if opCode == 99: # End Execution
|
||||
raise newException(StopExecution, "Program ended")
|
||||
if opCode == 1: # Addition
|
||||
cp.add(cp.get(cp.ic+1), cp.get(cp.ic+2), cp.get(cp.ic+3))
|
||||
if opCode == 2: # Multiplication
|
||||
cp.mul(cp.get(cp.ic+1), cp.get(cp.ic+2), cp.get(cp.ic+3))
|
||||
cp.ic += 4
|
||||
|
||||
proc run*(cp: Computer): int =
|
||||
## Runs the program loaded in memory until complete
|
||||
try:
|
||||
while true:
|
||||
cp.process
|
||||
except StopExecution:
|
||||
return cp.get(0)
|
||||
return -1 # Something went wrong
|
||||
|
||||
proc findInputFor*(cp: Computer, target: int): Input =
|
||||
## Finds fitting inputs to produce the desired output
|
||||
## Stops at the first match
|
||||
for noun in 0 .. 99:
|
||||
for verb in 0 .. 99:
|
||||
let input = Input(noun: noun, verb: verb)
|
||||
cp.reset(input)
|
||||
var output = cp.run
|
||||
if output == target:
|
||||
return input
|
||||
1
day02/input.txt
Normal file
1
day02/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,6,19,1,5,19,23,1,23,6,27,1,5,27,31,1,31,6,35,1,9,35,39,2,10,39,43,1,43,6,47,2,6,47,51,1,5,51,55,1,55,13,59,1,59,10,63,2,10,63,67,1,9,67,71,2,6,71,75,1,5,75,79,2,79,13,83,1,83,5,87,1,87,9,91,1,5,91,95,1,5,95,99,1,99,13,103,1,10,103,107,1,107,9,111,1,6,111,115,2,115,13,119,1,10,119,123,2,123,6,127,1,5,127,131,1,5,131,135,1,135,6,139,2,139,10,143,2,143,9,147,1,147,6,151,1,151,13,155,2,155,9,159,1,6,159,163,1,5,163,167,1,5,167,171,1,10,171,175,1,13,175,179,1,179,2,183,1,9,183,0,99,2,14,0,0
|
||||
19
day02/main.nim
Normal file
19
day02/main.nim
Normal file
@@ -0,0 +1,19 @@
|
||||
import computer
|
||||
|
||||
proc solvePart1(): void =
|
||||
var
|
||||
cp = newComputer("input.txt")
|
||||
input = Input(noun: 12, verb: 2)
|
||||
cp.reset(input)
|
||||
let result = cp.run
|
||||
echo "Result for Part 1: ", result
|
||||
|
||||
proc solvePart2(): void =
|
||||
var
|
||||
target = 19690720
|
||||
cp = newComputer("input.txt")
|
||||
inputs = cp.findInputFor(target)
|
||||
echo "result for Part 2: ", 100 * inputs.noun + inputs.verb
|
||||
|
||||
solvePart1()
|
||||
solvePart2()
|
||||
Reference in New Issue
Block a user