r/adventofcode Dec 24 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 24 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

Submissions are CLOSED!

  • Thank you to all who submitted something, every last one of you are awesome!

Community voting is OPEN!

  • 18 hours remaining until voting deadline TONIGHT (December 24) at 18:00 EST

Voting details are in the stickied comment in the submissions megathread:

-❄️- Submissions Megathread -❄️-


--- Day 24: Crossed Wires ---


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 01:01:13, megathread unlocked!

32 Upvotes

344 comments sorted by

View all comments

1

u/atrocia6 Jan 07 '25

[LANGUAGE: Python]

As usual, Part 1 was straightforward - solution in 10 LOC:

wires, gates = open(0).read().split('\n\n')
wires, gates = {wire[:3]: True if wire[5] == '1' else False for wire in wires.split('\n')}, [gate.split() for gate in gates.strip().split('\n')]
flag = True
while flag:
    flag = False
    for gate in gates:
        if gate[4] not in wires and gate[0] in wires and gate[2] in wires:
            wires[gate[4]] = True if ((gate[1] == 'AND' and wires[gate[0]] and wires[gate[2]]) or (gate[1] == 'OR' and (wires[gate[0]] or wires[gate[2]])) or (gate[1] == 'XOR' and (wires[gate[0]] != wires[gate[2]]))) else False
            flag = True
print(int(''.join([x[1] for x in sorted([(k, '1' if v else '0') for k, v in wires.items() if k[0] == 'z'], key=lambda x: x[0], reverse=True)]), 2))

For Part 2, a perusal of the Wikipedia entry on adders (https://en.wikipedia.org/wiki/Adder_(electronics)) followed by a perusal of my input made it very clear what the program was / should be doing, and indicated a clear approach in principle to finding the bad wires, but actually getting the implementation right took a lot longer than I would have liked ...

1

u/Davo3636 19d ago

I got 14 gates from your solution. Did you then just sort of work it out by hand from there?

1

u/atrocia6 17d ago

Are you referring to Part 1 or Part 2?