Day 1 + 2
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.exe
|
||||
100
day01/input.txt
Normal file
100
day01/input.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
51360
|
||||
95527
|
||||
72603
|
||||
128601
|
||||
68444
|
||||
138867
|
||||
67294
|
||||
134343
|
||||
62785
|
||||
53088
|
||||
134635
|
||||
137884
|
||||
97654
|
||||
103704
|
||||
138879
|
||||
87561
|
||||
83922
|
||||
68414
|
||||
84876
|
||||
105143
|
||||
76599
|
||||
98924
|
||||
57080
|
||||
63590
|
||||
50126
|
||||
111872
|
||||
55754
|
||||
64410
|
||||
78488
|
||||
56557
|
||||
105446
|
||||
127182
|
||||
59451
|
||||
87249
|
||||
61652
|
||||
131698
|
||||
148820
|
||||
95742
|
||||
68223
|
||||
121744
|
||||
65678
|
||||
99745
|
||||
64089
|
||||
75610
|
||||
106085
|
||||
100364
|
||||
116959
|
||||
122862
|
||||
56580
|
||||
109631
|
||||
82895
|
||||
79666
|
||||
133474
|
||||
50579
|
||||
83473
|
||||
140028
|
||||
125999
|
||||
68225
|
||||
131345
|
||||
90797
|
||||
84914
|
||||
81915
|
||||
65369
|
||||
71230
|
||||
50379
|
||||
106385
|
||||
118503
|
||||
119640
|
||||
138540
|
||||
70678
|
||||
95881
|
||||
100282
|
||||
123060
|
||||
147368
|
||||
93030
|
||||
82553
|
||||
131271
|
||||
147675
|
||||
111126
|
||||
115183
|
||||
82956
|
||||
145698
|
||||
99261
|
||||
52768
|
||||
99207
|
||||
123551
|
||||
64738
|
||||
117275
|
||||
98136
|
||||
111592
|
||||
78576
|
||||
118613
|
||||
130351
|
||||
68567
|
||||
72356
|
||||
85608
|
||||
129414
|
||||
66521
|
||||
76924
|
||||
130449
|
||||
24
day01/main.nim
Normal file
24
day01/main.nim
Normal file
@@ -0,0 +1,24 @@
|
||||
import math, strutils
|
||||
|
||||
proc fuelCalc(mass: int): int =
|
||||
floor(mass / 3).toInt - 2
|
||||
|
||||
|
||||
proc calcForFuel(fuelMass: int): int =
|
||||
let required = fuelCalc(fuelMass)
|
||||
if required <= 0:
|
||||
return 0
|
||||
else:
|
||||
return required + calcForFuel(required)
|
||||
|
||||
proc calc(mass: int): int =
|
||||
var massFuel = fuelCalc(mass)
|
||||
var fuelFuel = calcForFuel(massFuel)
|
||||
return massFuel + fuelFuel
|
||||
|
||||
var result = 0;
|
||||
var file = open("input.txt", fmRead);
|
||||
for line in file.lines:
|
||||
result += calc(line.parseInt)
|
||||
|
||||
echo result
|
||||
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