advent-of-code/2021/nim/day24.nim

34 lines
1.1 KiB
Nim
Raw Normal View History

2021-12-25 00:18:13 -06:00
import ./common, std/[strutils, sequtils, tables, strformat, algorithm]
2021-12-24 23:25:24 -06:00
2021-12-25 00:14:20 -06:00
proc parseInstructionArg(s: string): int =
2021-12-24 23:25:24 -06:00
let ss = s.split(' ')
2021-12-25 00:14:20 -06:00
if ss.len > 2 and not (ss[2][0] in 'w'..'z'): ss[2].parseInt
else: 0
2021-12-24 23:25:24 -06:00
2021-12-25 00:18:13 -06:00
proc process(input: Lines): Table[int, (int, int)] =
2021-12-25 00:14:20 -06:00
let instructionArgs = input.map(parseInstructionArg)
var cantComputeYet: seq[(int, int)] = @[]
for i in 0..13:
let o = i * 18
if instructionArgs[o+4] == 26:
let (previousI, previousAddend) = cantComputeYet.pop
let diff = previousAddend + instructionArgs[o+5]
if diff < 0:
2021-12-25 00:18:13 -06:00
result[previousI] = (9, -diff + 1)
result[i] = (9 + diff, 1)
2021-12-25 00:14:20 -06:00
else:
2021-12-25 00:18:13 -06:00
result[previousI] = (9 - diff, 1)
result[i] = (9, 1 + diff)
2021-12-25 00:14:20 -06:00
else: cantComputeYet.add (i, instructionArgs[o+15])
2021-12-25 00:18:13 -06:00
proc p1(input: Lines): uint64 =
let processed = input.process
processed.keys.toSeq.sorted.mapIt($processed[it][0]).join().parseBiggestUInt()
proc p2(input: Lines): uint64 =
let processed = input.process
processed.keys.toSeq.sorted.mapIt($processed[it][1]).join().parseBiggestUInt()
2021-12-24 23:25:24 -06:00
time(&"Day 24 Part 1"): echo 24.loadInput.p1
2021-12-25 00:18:13 -06:00
time(&"Day 24 Part 2"): echo 24.loadInput.p2