Skip to content

Commit

Permalink
2024 Day 17 Part 2 Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorliss committed Dec 18, 2024
1 parent 0b1bfd5 commit fcb85d7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
36 changes: 32 additions & 4 deletions 2024/17/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def run_program(registers, program):
ip += 1
# The bst instruction (opcode 2) calculates the value of its combo operand modulo 8
elif op == 2:
# TODO this would be faster if we just grabbed the 3 lowest bits instead
registers['B'] = get_combo(program[ip], registers) % 8
registers['B'] = get_combo(program[ip], registers) & 7
ip += 1
# The jnz instruction (opcode 3) does nothing if the A register is 0.
# However, if the A register is not zero, it jumps by setting the instruction pointer
Expand All @@ -86,18 +85,47 @@ def run_program(registers, program):
# The out instruction (opcode 5) calculates the value of its combo operand modulo 8,
# then outputs that value.
elif op == 5:
output.append(get_combo(program[ip], registers) % 8)
output.append(get_combo(program[ip], registers) & 7)
ip += 1

return registers, output

def quinify_slow(registers, program):
# iterate from registers['a'] in increments of 8
for i in range(registers['A'], 1000000000, 8):
registers['A'] = i
_, output = run_program(registers, program)
if output == program:
return i

return -1

def quinify(registers, target_output):
candidate_ras = [0]
for i in range(len(target_output)):
new_candidates = []
for ra in candidate_ras:
for j in range(2**3):
candidate_ra = (ra << 3) + j
_, out = run_program({'A': candidate_ra, 'B': 0, 'C': 0},target_output)
if target_output[-(i + 1):] == out:
# print(f"Possibility: {candidate_ra} I: {i}")
# print(f"Output: {out} Target: {target_output[-(i + 1):]}")
new_candidates.append(candidate_ra)

candidate_ras = new_candidates

return min(candidate_ras)

def part1(input_text):
registers, program = parse(input_text)
_, output = run_program(registers, program)
return output

def part2(input_text):
return 0
start_registers, program = parse(input_text)
register_a = quinify(start_registers, program)
return register_a

if __name__ == "__main__":
with open(__file__.rsplit('/', 1)[0] + "/input.txt", 'r') as file:
Expand Down
47 changes: 41 additions & 6 deletions 2024/17/daily_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def sample_data():
Program: 0,1,5,4,3,0
"""

@pytest.fixture
def parsed_data_a(sample_data):
return parse(sample_data)

def test_parse(sample_data):
registers, program = parse(sample_data)

Expand Down Expand Up @@ -73,5 +69,44 @@ def test_run_program(initial_registers, program, expected_registers, expected_ou
def test_part1(sample_data):
assert part1(sample_data) == [4,6,3,5,6,3,5,2,1,0]

# def test_part2(sample_data_a):
# assert part2(sample_data_a) == 45
@pytest.fixture
def sample_data_part_2():
return """
Register A: 2024
Register B: 0
Register C: 0
Program: 0,3,5,4,3,0
"""

def test_part2_checks(sample_data_part_2):
start_registers, program = parse(sample_data_part_2)
registers, output = run_program(start_registers, program)

print(f"Registers: {registers}")
print(f"Output: {output}")

start_registers, program = parse(sample_data_part_2)
start_registers['A'] = 117440
registers, output = run_program(start_registers, program)

print(f"Registers: {registers}")
print(f"Output: {output}")
# assert False == True

def test_quinify(sample_data_part_2):
start_registers, program = parse(sample_data_part_2)
register_a = quinify(start_registers, program)

assert register_a == 117440

def test_verify():
r = {'A': 62343326, 'B': 0, 'C': 0}
program = [2,4,1,5,7,5,1,6,4,3,5,5,0,3,3,0]

_, output = run_program(r, program)
assert output == [6, 4, 3, 5, 5, 0, 3, 3, 0]


# def test_part2(sample_data_part_2):
# assert part2(sample_data_part_2) == 117440

0 comments on commit fcb85d7

Please sign in to comment.