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

27 lines
975 B
Nim
Raw Normal View History

2021-12-24 23:25:24 -06:00
import ./common, std/[strutils, sugar, sequtils, options, tables, sets, strformat, algorithm, pegs]
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
proc p1(input: Lines): uint64 =
2021-12-25 00:14:20 -06:00
let instructionArgs = input.map(parseInstructionArg)
var reversedDigits = initTable[int, int]()
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:
reversedDigits[previousI] = 9
reversedDigits[i] = 9 + diff
else:
reversedDigits[previousI] = 9 - diff
reversedDigits[i] = 9
else: cantComputeYet.add (i, instructionArgs[o+15])
reversedDigits.keys.toSeq.sorted.mapIt($reversedDigits[it]).join().parseBiggestUInt()
2021-12-24 23:25:24 -06:00
time(&"Day 24 Part 1"): echo 24.loadInput.p1