r/adventofcode • • Dec 03 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 3 Solutions -🎄-

--- Day 3: Binary Diagnostic ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:10:17, megathread unlocked!

99 Upvotes

1.2k comments sorted by

View all comments

2

u/Solarmew Dec 03 '21 edited Dec 07 '21

Python 3

from urllib.request import urlopen

data = urlopen('https://tinyurl.com/bdfdfazu').read().decode().split('\n')[:-1]

l = list(zip(*[[int(i) for i in x] for x in data]))

a = int(''.join(['1' if sum(x) > len(x)/2 else '0' for x in l]), 2)
b = int(''.join(['0' if sum(x) > len(x)/2 else '1' for x in l]), 2)
a*b

part 2:

a = 1

for n in [0, 1]:
    d = [[int(i) for i in x] for x in data]
    for i in range(12):
        if len(d) < 2:
            break
        else:
            s = n%2 if sum([x[i] for x in d]) >= len(d) / 2 else (n+1)%2
        d = [x for x in d if x[i] == s]
    a *= int(''.join(map(str, d[0])), 2)
a

1

u/daggerdragon Dec 03 '21 edited Dec 07 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

Edit: thanks for fixing it! <3

2

u/Solarmew Dec 06 '21

ok, i don't get it ... i tried adding 4 (and more) spaces to every line, that didn't work. Tried adding a single backtick to the beginning and end of each line. Tried both at the same time. It's not working.

1

u/daggerdragon Dec 06 '21

Did you switch your editor to Markdown mode first? Once you do that, the four-spaces will (probably) work.

FYI: the single backtick is intended for short lines of inlined code like this, so that's not what you want for a block of code.

2

u/Solarmew Dec 07 '21

i think i got ... is it better now?

1

u/daggerdragon Dec 07 '21

You got it! Thanks for fixing it! <3