Day 24 part 1 done!

This commit is contained in:
Daniel Flanagan 2021-12-25 00:14:20 -06:00
parent 96cda8f48e
commit a0818ada2a
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -1,73 +1,28 @@
import ./common, std/[strutils, sugar, sequtils, options, tables, sets, strformat, algorithm, pegs] import ./common, std/[strutils, sugar, sequtils, options, tables, sets, strformat, algorithm, pegs]
type proc parseInstructionArg(s: string): int =
Register = int
ALUInstruction = (string, Register, string)
ALUState = array[4, int]
proc parseInstruction(s: string): ALUInstruction =
# echo s
let ss = s.split(' ') let ss = s.split(' ')
let i = if ss.len > 2: ss[2] else: "" if ss.len > 2 and not (ss[2][0] in 'w'..'z'): ss[2].parseInt
(ss[0], ss[1][0].ord - ord('w'), i) else: 0
proc runALUInstructions(instructions: seq[ALUInstruction], input: string): ALUState =
var inputs = input.reversed
let registers = toHashSet(['w', 'x', 'y', 'z'])
var n = 0
for i in instructions:
let (op, reg, arg) = i
var narg = arg
var num = 0
if arg != "":
if registers.contains(arg[0]):
let index = arg[0].ord - 'w'.ord
num = result[index]
narg = &"r{index+1}"
else: num = arg.parseInt
if n mod 18 in [4, 5, 15, 0]:
echo &"{result} -> {op} {narg} to r{reg+1} (Input Left: {inputs.join})"
case op:
of "inp": result[reg] = inputs.pop.ord - '0'.ord
of "add": result[reg] += num
of "mul": result[reg] *= num
of "div": result[reg] = result[reg] div num
of "mod": result[reg] = result[reg] mod num
of "eql": result[reg] = (result[reg] == num).int
inc n
proc p1(input: Lines): uint64 = proc p1(input: Lines): uint64 =
# repeating these 18 instructions: let instructionArgs = input.map(parseInstructionArg)
# w is used to store the input digit var reversedDigits = initTable[int, int]()
# x is zeroed var cantComputeYet: seq[(int, int)] = @[]
# z is added to x for i in 0..13:
# x is modulo'd over 26 let o = i * 18
# z is divided by either 1 or 26 (mostly in the latter half of the inputs) if instructionArgs[o+4] == 26:
# x is added with SOME NUMBER (sometimes negative) let (previousI, previousAddend) = cantComputeYet.pop
# x is ALWAYS eql'd with w and 0 let diff = previousAddend + instructionArgs[o+5]
# this is meant to ensure that x is NOT equal to the input? echo diff
# y is ALWAYS set to 25 echo previousI
# y is multiplied by x, which will be 25 if x was 1 or 0 otherwise if diff < 0:
# so y will be 25 if x was equal to the input and 0 if not reversedDigits[previousI] = 9
# y is incremented reversedDigits[i] = 9 + diff
# 26 now, if x was equal, 1 if not else:
# z is multiplied by y reversedDigits[previousI] = 9 - diff
# so z reversedDigits[i] = 9
# y is zeroed out, set to w (the input), added with SOME NUMBER, then multiplied with x else: cantComputeYet.add (i, instructionArgs[o+15])
# add y to z reversedDigits.keys.toSeq.sorted.mapIt($reversedDigits[it]).join().parseBiggestUInt()
# if I need to get z to zero, I need to know where those 26-divisions are and leverage them
# if I can ever get y to be negative, that could help a lot as well
# probably a relatively simple way to bruteforce over this?
let instructions = input.map(parseInstruction)
# let oalu = instructions.runALUInstructions("13579246899999")
# echo oalu
let alu = instructions.runALUInstructions("99879249111111")
echo alu
0
proc p2(input: Lines): uint64 =
0
time(&"Day 24 Part 1"): echo 24.loadInput.p1 time(&"Day 24 Part 1"): echo 24.loadInput.p1