Day 24 part 2
This commit is contained in:
parent
82b1705bb8
commit
891bc6c4c8
|
@ -1,13 +1,12 @@
|
||||||
import ./common, std/[strutils, sugar, sequtils, options, tables, sets, strformat, algorithm, pegs]
|
import ./common, std/[strutils, sequtils, tables, strformat, algorithm]
|
||||||
|
|
||||||
proc parseInstructionArg(s: string): int =
|
proc parseInstructionArg(s: string): int =
|
||||||
let ss = s.split(' ')
|
let ss = s.split(' ')
|
||||||
if ss.len > 2 and not (ss[2][0] in 'w'..'z'): ss[2].parseInt
|
if ss.len > 2 and not (ss[2][0] in 'w'..'z'): ss[2].parseInt
|
||||||
else: 0
|
else: 0
|
||||||
|
|
||||||
proc p1(input: Lines): uint64 =
|
proc process(input: Lines): Table[int, (int, int)] =
|
||||||
let instructionArgs = input.map(parseInstructionArg)
|
let instructionArgs = input.map(parseInstructionArg)
|
||||||
var reversedDigits = initTable[int, int]()
|
|
||||||
var cantComputeYet: seq[(int, int)] = @[]
|
var cantComputeYet: seq[(int, int)] = @[]
|
||||||
for i in 0..13:
|
for i in 0..13:
|
||||||
let o = i * 18
|
let o = i * 18
|
||||||
|
@ -15,12 +14,20 @@ proc p1(input: Lines): uint64 =
|
||||||
let (previousI, previousAddend) = cantComputeYet.pop
|
let (previousI, previousAddend) = cantComputeYet.pop
|
||||||
let diff = previousAddend + instructionArgs[o+5]
|
let diff = previousAddend + instructionArgs[o+5]
|
||||||
if diff < 0:
|
if diff < 0:
|
||||||
reversedDigits[previousI] = 9
|
result[previousI] = (9, -diff + 1)
|
||||||
reversedDigits[i] = 9 + diff
|
result[i] = (9 + diff, 1)
|
||||||
else:
|
else:
|
||||||
reversedDigits[previousI] = 9 - diff
|
result[previousI] = (9 - diff, 1)
|
||||||
reversedDigits[i] = 9
|
result[i] = (9, 1 + diff)
|
||||||
else: cantComputeYet.add (i, instructionArgs[o+15])
|
else: cantComputeYet.add (i, instructionArgs[o+15])
|
||||||
reversedDigits.keys.toSeq.sorted.mapIt($reversedDigits[it]).join().parseBiggestUInt()
|
|
||||||
|
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()
|
||||||
|
|
||||||
time(&"Day 24 Part 1"): echo 24.loadInput.p1
|
time(&"Day 24 Part 1"): echo 24.loadInput.p1
|
||||||
|
time(&"Day 24 Part 2"): echo 24.loadInput.p2
|
||||||
|
|
Loading…
Reference in a new issue