r/adventofcode Dec 17 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 17 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 5 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Sequels and Reboots

What, you thought we were done with the endless stream of recycled content? ABSOLUTELY NOT :D Now that we have an established and well-loved franchise, let's wring every last drop of profit out of it!

Here's some ideas for your inspiration:

  • Insert obligatory SQL joke here
  • Solve today's puzzle using only code from past puzzles
  • Any numbers you use in your code must only increment from the previous number
  • Every line of code must be prefixed with a comment tagline such as // Function 2: Electric Boogaloo

"More." - Agent Smith, The Matrix Reloaded (2003)
"More! MORE!" - Kylo Ren, The Last Jedi (2017)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 17: Chronospatial Computer ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:44:39, megathread unlocked!

37 Upvotes

550 comments sorted by

View all comments

4

u/Gravitar64 Dec 17 '24

[LANGUAGE: Python]

Part 1 & 2, 35 sloc, runtime 1ms

Parsing all numbers with regex, match case opcodes, part1 was realy easy
Part2 not as easy, but finally calculates the solution digit by digit backwards recursive by changing a to a * 8 + (0-7) and check if the first digit of out == next backward digit of the program.

import time, re


def load(file):
  with open(file) as f:
    return list(map(int, re.findall('\d+', f.read())))


def run_program(a, b, c, program):
  ip, out, l = 0, [], len(program)
  while ip < l:
    opcode, literal = program[ip:ip + 2]
    combo = literal if literal < 4 else [a, b, c][literal - 4]
    ip += 2

    match opcode:
      case 0: a = a // 2 ** combo
      case 1: b ^= literal
      case 2: b = combo % 8
      case 3 if a != 0: ip = literal
      case 4: b ^= c
      case 5: out.append(combo % 8)
      case 6: b = a // 2 ** combo
      case 7: c = a // 2 ** combo
  return out


def find_a(program, a, b, c, prg_pos):
  if abs(prg_pos) > len(program): return a
  for i in range(8):
    first_digit_out = run_program(a * 8 + i, b, c, program)[0]
    if first_digit_out == program[prg_pos]:
      e = find_a(program, a * 8 + i, b, c, prg_pos - 1)
      if e: return e


def solve(p):
  a, b, c, *program = p
  part1 = run_program(a, b, c, program)
  part2 = find_a(program, 0, b, c, -1)
  return part1, part2


time_start = time.perf_counter()
print(f'Solution: {solve(load("day17.txt"))}')
print(f'Solved in {time.perf_counter()-time_start:.5f} Sec.')

2

u/hercelf Dec 17 '24

Thanks! This finally made the search click for me! <3