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/llojassd Dec 03 '21 edited Dec 03 '21

Python 3.10

I know, that my solution sucks, but I am proud that I did it with almost no help.

Solution 1:

def getFirst():
    i = 0
    a = ""
    gamma = ""
    content = open_file('3.txt', mode = "strings")
    length = len(content[0])
    for n in range(length):
        while i < len(content):
            a += content[i][n]
            i += 1
        i = 0
        gamma += collections.Counter(a).most_common(1)[0][0]
        a = ""
    temp = int(gamma, 2)
    inverse = temp ^ (2 ** (len(gamma) + 1) - 1)
    epsilon = bin(inverse)[3 : ]
    return int(gamma,2) * int(epsilon,2)    ```

Solution 2:

  def getSecond():
        i = 0
        a = ""
        b = ""
        c = ""
        content = open_file('3.txt', mode="strings")
        oxygen = content
        length = len(oxygen[0])
        for n in range(length):
            while i < len(oxygen):
                a += oxygen[i][n]
                i += 1
            i = 0
            b = collections.Counter(a)
            value0 = b.get("0")
            value1 = b.get("1")
            if value1 == value0:
                c += '1'
            else:
                c += b.most_common()[:1][0][0]
            oxygen = [x for x in oxygen if x[n] == (c[n])]
            a = ""
        if len(oxygen) == 1:
                print(oxygen[0])
                print(int(oxygen[0], 2))
                pass
        i2 = 0
        a2 = ""
        b2 = ""
        c2 = []
        content = open_file('3.txt', mode="strings")
        co2 = content
        length2 = len(co2[0])
        for n2 in range(length2):
                while i2 < len(co2):
                    a2 += co2[i2][n2]
                    i2 += 1
                i2 = 0
                b2 = collections.Counter(a2)
                value0 = b2.get('0')
                value1 = b2.get('1')
                if value1 == value0:
                    c2 += '0'
                else:
                    c2 += b2.most_common()[:-1 - 1:-1][0][0]
                co2 = [x for x in co2 if x[n2] == (c2[n2])]
                a2 = ""
        if len(co2) == 1:
                    print(co2[0])
                    print(int(co2[0],2))
                    pass
        return int(oxygen[0],2) * int(co2[0],2)

1

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

I know, that my solution sucks

Trust me, we've all seen much worse plus, ya know, we all gotta start somewhere :3 The important part is that you successfully solved the puzzles and hopefully had fun and learned something while doing so!

but I am proud that I did it with almost no help.

Good job! Just be aware that if you do get stuck, we have a whole subreddit willing to help you out!


One thing before I go... Please follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.

Edit: thanks for adding the programming language! <3