Skip to content

Commit

Permalink
17-2
Browse files Browse the repository at this point in the history
Squashed commit of the following:
commit 20335dc
Author: Lachlan Meyer <lachlanmeyer@gmail.com>
Date:   Wed Dec 18 14:38:32 2024 +1100

    17-2

commit c335e1c
Author: Lachlan Meyer <lachlanmeyer@gmail.com>
Date:   Wed Dec 18 00:59:54 2024 +1100

    This thing is too friggin slow

commit 4b1b30c
Author: Lachlan Meyer <lachlanmeyer@gmail.com>
Date:   Wed Dec 18 00:50:24 2024 +1100

    12-1
  • Loading branch information
koosvary committed Dec 18, 2024
1 parent bed7ab3 commit 2eae905
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 4 deletions.
5 changes: 1 addition & 4 deletions 2024/17/17-1.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

registers = {}
operands = []

outs = []

pointer = 0
outs = []

def getComboOperandValue(operand):
match operand:
Expand All @@ -16,7 +14,6 @@ def getComboOperandValue(operand):
# 7 is 'reserved'
raise Exception('bruh')


def runOperand(opcode, operand = None):
global pointer
pointer += 2
Expand Down
100 changes: 100 additions & 0 deletions 2024/17/17-2.py
Original file line number Diff line number Diff line change
@@ -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]
81 changes: 81 additions & 0 deletions 2024/17/notes.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions 2024/17/testinput2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Register A: 2024
Register B: 0
Register C: 0

Program: 0,3,5,4,3,0
5 changes: 5 additions & 0 deletions 2024/17/testinput3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Register A: 117440
Register B: 0
Register C: 0

Program: 0,3,5,4,3,0

0 comments on commit 2eae905

Please sign in to comment.