From 3a5bc1757d077c75ecce78800372895d668f7d42 Mon Sep 17 00:00:00 2001 From: j-james Date: Wed, 9 Dec 2020 03:39:49 -0800 Subject: Implement current 2019 AoC solutions in Nim --- 2019/eight.nim | 30 ++++++++++++++++++++++++++++ 2019/five.nim | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2019/four.nim | 37 ++++++++++++++++++++++++++++++++++ 2019/one.nim | 16 +++++++++++++++ 2019/two.nim | 37 ++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 2019/eight.nim create mode 100644 2019/five.nim create mode 100644 2019/four.nim create mode 100644 2019/one.nim create mode 100644 2019/two.nim (limited to '2019') diff --git a/2019/eight.nim b/2019/eight.nim new file mode 100644 index 0000000..ecf291e --- /dev/null +++ b/2019/eight.nim @@ -0,0 +1,30 @@ +# Day Eight: Space Image Format +import os, strutils, sequtils + +let + input: string = paramStr(1) + digits: string = strip(readFile(input)) + width: int = 25 + height: int = 6 + depth: int = len(digits) div (width * height) + +var min, product: int = width * height +for layer in distribute(@digits, depth): + if count(layer, '0') < min: + min = count(layer, '0') + product = count(layer, '1') * count(layer, '2') +echo product + +for column in 0 ..< height: + for row in 0 ..< width: + for layer in 0 ..< depth: + case digits[(layer * height * width) + (column * width) + row] + of '0': + write(stdout, ' ') + break + of '1': + write(stdout, 'X') + break + else: + discard + write(stdout, '\n') diff --git a/2019/five.nim b/2019/five.nim new file mode 100644 index 0000000..87c359e --- /dev/null +++ b/2019/five.nim @@ -0,0 +1,63 @@ +# Day Five: Sunny with a Chance of Asteroids +import os, strutils, sequtils + +let input: string = paramStr(1) +var program: seq[int] = map(split(strip(readFile(input)), ','), parseInt) + +var i: int = 0 +while i < len(program): + let opcode: int = program[i] mod 100 + + var + mode: int = program[i] div 100 + one: int = i+1 + two: int = i+2 + three: int = i+3 + if len(program)-i > 1 and mode mod 10 == 0: + one = program[one] + if len(program)-i > 2 and mode div 10 mod 10 == 0: + two = program[two] + if len(program)-i > 3 and mode div 100 == 0: + three = program[three] + + case opcode + of 1: # adds + program[three] = program[one] + program[two] + i += 4 + of 2: # multiplies + program[three] = program[one] * program[two] + i += 4 + of 3: # input + write(stdout, "Input: ") + program[one] = parseInt(readLine(stdin)) + i += 2 + of 4: # output + echo program[one] + i += 2 + of 5: # jump-if-true + if program[one] != 0: + i = program[two] + else: + i += 3 + of 6: # jump-if-false + if program[one] == 0: + i = program[two] + else: + i += 3 + of 7: # less than + if program[one] < program[two]: + program[three] = 1 + else: + program[three] = 0 + i += 4 + of 8: # equals + if program[one] == program[two]: + program[three] = 1 + else: + program[three] = 0 + i += 4 + of 99: # terminate + break + else: + echo("Unsupported code ", program[i], " at ", i) + quit(QuitFailure) diff --git a/2019/four.nim b/2019/four.nim new file mode 100644 index 0000000..eb6d933 --- /dev/null +++ b/2019/four.nim @@ -0,0 +1,37 @@ +# Day Four: Secure Container +import os, strutils, sequtils + +let input: string = paramStr(1) +let bounds: seq[int] = map(split(strip(readFile(input)), '-'), parseInt) +var valid, details: int = 0 + +proc criteria(password: int): bool = + var password: string = $password + for digit in 0 .. 4: + if password[digit] > password[digit+1]: + return false + for digit in 0 .. 4: + if password[digit] == password[digit+1]: + return true + return false + +proc extracriteria(password: int): bool = + var password: string = $password + for digit in 0 .. 4: + if password[digit] > password[digit+1]: + return false + for digit in 0 .. 4: + if password[digit] == password[digit+1]: + if digit == 0 or password[digit] != password[digit-1]: + if digit == 4 or password[digit] != password[digit+2]: + return true + return false + +for password in bounds[0] .. bounds[1]: + if criteria(password): inc(valid) + +for password in bounds[0] .. bounds[1]: + if extracriteria(password): inc(details) + +echo valid +echo details diff --git a/2019/one.nim b/2019/one.nim new file mode 100644 index 0000000..63c1219 --- /dev/null +++ b/2019/one.nim @@ -0,0 +1,16 @@ +# Day One: The Tyranny of the Rocket Equation +import os, strutils + +let input: string = paramStr(1) +var sum, fuelsum: int = 0 + +for mass in lines(input): + var fuel = parseInt(mass) div 3 - 2 + sum += fuel + fuelsum += fuel + while fuel > 0: + fuel = fuel div 3 - 2 + fuelsum += max(0, fuel) + +echo sum +echo fuelsum diff --git a/2019/two.nim b/2019/two.nim new file mode 100644 index 0000000..c0bfb9b --- /dev/null +++ b/2019/two.nim @@ -0,0 +1,37 @@ +# Day Two: 1202 Program Alarm +import os, strutils, sequtils + +let input: string = paramStr(1) +var program: seq[int] = map(split(strip(readFile(input)), ','), parseInt) +program[1] = 12 +program[2] = 2 + +proc execute(program: seq[int]): int = + var program: seq[int] = program + var i: int = 0 + while i < len(program): + case program[i] + of 1: + program[program[i+3]] = program[program[i+1]] + program[program[i+2]] + i += 4 + of 2: + program[program[i+3]] = program[program[i+1]] * program[program[i+2]] + i += 4 + of 99: + break + else: + echo("Unsupported code ", program[i], " at ", i) + quit(QuitFailure) + return program[0] + +proc grammar(program: seq[int]): int = + var program: seq[int] = program + for noun in 0 ..< len(program): + for verb in 0 ..< len(program): + program[1] = noun + program[2] = verb + if execute(program) == 19690720: + return 100 * noun + verb + +echo execute(program) +echo grammar(program) -- cgit v1.2.3-70-g09d2