From 2eae905b552ba2e26f21d74f743084d6f81cce71 Mon Sep 17 00:00:00 2001 From: Lachlan Meyer Date: Wed, 18 Dec 2024 14:41:32 +1100 Subject: [PATCH] 17-2 Squashed commit of the following: commit 20335dc3d88c008792092df0500850f0f5280ebd Author: Lachlan Meyer Date: Wed Dec 18 14:38:32 2024 +1100 17-2 commit c335e1c42749d2830a8b73cb65621c3de471a162 Author: Lachlan Meyer Date: Wed Dec 18 00:59:54 2024 +1100 This thing is too friggin slow commit 4b1b30ca46d54209d992cc93a2a2ee6b8a5b221c Author: Lachlan Meyer Date: Wed Dec 18 00:50:24 2024 +1100 12-1 --- 2024/17/17-1.py | 5 +-- 2024/17/17-2.py | 100 +++++++++++++++++++++++++++++++++++++++++ 2024/17/notes.txt | 81 +++++++++++++++++++++++++++++++++ 2024/17/testinput2.txt | 5 +++ 2024/17/testinput3.txt | 5 +++ 5 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 2024/17/17-2.py create mode 100644 2024/17/notes.txt create mode 100644 2024/17/testinput2.txt create mode 100644 2024/17/testinput3.txt diff --git a/2024/17/17-1.py b/2024/17/17-1.py index cfcd67c..4a7dac7 100644 --- a/2024/17/17-1.py +++ b/2024/17/17-1.py @@ -1,10 +1,8 @@ - registers = {} operands = [] -outs = [] - pointer = 0 +outs = [] def getComboOperandValue(operand): match operand: @@ -16,7 +14,6 @@ def getComboOperandValue(operand): # 7 is 'reserved' raise Exception('bruh') - def runOperand(opcode, operand = None): global pointer pointer += 2 diff --git a/2024/17/17-2.py b/2024/17/17-2.py new file mode 100644 index 0000000..f88911c --- /dev/null +++ b/2024/17/17-2.py @@ -0,0 +1,100 @@ +originalRegisters = {} +registers = {} +operands = [] + +pointer = 0 +outs = [] + +def getComboOperandValue(operand): + match operand: + case 0 | 1 | 2 | 3: + return operand + case 4 | 5 | 6: + return registers[chr(operand + 61)] # 65 is 'A' in ASCII + case _: + # 7 is 'reserved' + raise Exception('bruh') + +def runOperand(opcode, operand = None): + global pointer + pointer += 2 + + match opcode: + case 0: + #adv + registers['A'] = int(registers['A']/(2**getComboOperandValue(operand))) + case 1: + #bxl + registers['B'] = registers['B'] ^ operand + case 2: + #bst + registers['B'] = getComboOperandValue(operand) % 8 + case 3: + #jnz + if registers['A'] == 0: + return + pointer = operand + case 4: + #bxc + registers['B'] = registers['B'] ^ registers['C'] + case 5: + #out + outs.append(getComboOperandValue(operand) % 8) + case 6: + #bdv + registers['B'] = int(registers['A']/(2**getComboOperandValue(operand))) + case 7: + #cdv + registers['C'] = int(registers['A']/(2**getComboOperandValue(operand))) + +with open('2024/17/input.txt') as f: + for i, line in enumerate(f): + line = line.strip() + + if i == 3: + continue + + split = line.split(': ') + if i == 4: + operands = [int(x) for x in split[1].split(',')] + break + + registers[split[0][-1]] = int(split[1]) + +print(registers) +print(operands) + +originalRegisters = registers.copy() + +def isSubarrayAtEnd(arr, sub): + # Check if the subarray is identical to the end of the array + return arr[-len(sub):] == sub + +patternMatches = [0] +numberFound = False + +while not numberFound: + currentMatch = patternMatches[0] + valueBin = format(currentMatch, 'o') + for i in range(8): + testValue = int(str(valueBin) + str(i), 8) + + registers['A'] = testValue + while pointer < len(operands): + runOperand(operands[pointer], operands[pointer+1]) + + if isSubarrayAtEnd(operands, outs): + patternMatches.append(testValue) + print(f'{testValue}: {valueBin} {outs}') + + if outs == operands: + print(f'Your lucky number is: {testValue}') + print(','.join(str(out) for out in outs)) + numberFound = True + break + + registers = originalRegisters.copy() + outs = [] + pointer = 0 + testValue += 1 + del patternMatches[0] \ No newline at end of file diff --git a/2024/17/notes.txt b/2024/17/notes.txt new file mode 100644 index 0000000..b744bb6 --- /dev/null +++ b/2024/17/notes.txt @@ -0,0 +1,81 @@ +note about notes: I do be rambling + +notice that sets ending in 0 seem to output in a pattern +4: [0] 100 + +32: [4, 0] 100000 +33: [4, 0] +34: [2, 0] +35: [7, 0] +36: [1, 0] +37: [3, 0] +38: [2, 0] +39: [3, 0] 100111 + +256: [4, 4, 0] 100000000 +257: [4, 4, 0] +258: [6, 4, 0] +259: [7, 4, 0] +... continues like this +315: [1, 3, 0] +316: [1, 3, 0] +317: [2, 3, 0] +318: [0, 3, 0] +319: [7, 3, 0] 100111111 + +Theory: seems to output 0 as the last number when starting with binary 100 + +2048 has 0 at end! +Likely not a coincidence +Note: last value in my input is 0, so will not work for opcodes with any other number at the end + +Still takes too long, what about base 8? + +4: 4 +32: 40 +39: 47 +256: 400 +319: 477 +2048: 4000 +16384: 40000 + +New theory: go from 4(0+) to 4(7+) + +Still chugging. +At 536870912, no luck yet. After 40 minutes we're still stuck here. + +Nearly an hour and we hit 4294967296 +Stuck here for hours now. This isn't going to work. + + +Investigating the sample set +117440 = 0o345300 or 0x1CAC0 +Didn't start with a 4, is this logic flawed? +It's possible all the outputs that have 0 as the last instead start with 3 for this sequence of steps. +But why? + +Ideas: +It's possible that the XORs from part 1 are relevant here. +Maybe there's some bit crunching that lets us skip some more numbers. + +========== + +New idea: what if we focus on patterns that match the output at the end? + +{'A': 46323429, 'B': 0, 'C': 0} +[2, 4, 1, 1, 7, 5, 1, 5, 4, 3, 0, 3, 5, 5, 3, 0] +4: 4 [0] +37: 45 [3, 0] +39: 47 [3, 0] +299: 453 [5, 3, 0] +2394: 4532 [5, 5, 3, 0] +19155: 45323 [3, 5, 5, 3, 0] +153240: 453230 [0, 3, 5, 5, 3, 0] +153245: 453235 [0, 3, 5, 5, 3, 0] +1225926: 4532306 [3, 0, 3, 5, 5, 3, 0] +1225962: 4532352 [3, 0, 3, 5, 5, 3, 0] +1225966: 4532356 [3, 0, 3, 5, 5, 3, 0] + +for each value that matches, the next pattern has the same octal + another digit 0-7 + +If this is the case, we'll have at most 8^n new calculations to run - still much better than every value that started with 0o4 \ No newline at end of file diff --git a/2024/17/testinput2.txt b/2024/17/testinput2.txt new file mode 100644 index 0000000..2ed9dda --- /dev/null +++ b/2024/17/testinput2.txt @@ -0,0 +1,5 @@ +Register A: 2024 +Register B: 0 +Register C: 0 + +Program: 0,3,5,4,3,0 \ No newline at end of file diff --git a/2024/17/testinput3.txt b/2024/17/testinput3.txt new file mode 100644 index 0000000..5b78441 --- /dev/null +++ b/2024/17/testinput3.txt @@ -0,0 +1,5 @@ +Register A: 117440 +Register B: 0 +Register C: 0 + +Program: 0,3,5,4,3,0 \ No newline at end of file