2021 - Day 3 (Binary Diagnostic)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3

#Advent of Code
#2021 Day 3, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import Counter

def solve(puzzle_input):
    counts = [Counter() for i in range(len(puzzle_input[0]))]
    for number in puzzle_input:
        for i, d in enumerate(number):
            counts[i].update(d)

    epsilon = int("".join(c.most_common()[0][0] for c in counts), 2)
    gamma   = int("".join(c.most_common()[1][0] for c in counts), 2)
    return epsilon * gamma

def main():
    puzzle_input = util.read.as_lines()

    power = solve(puzzle_input)

    print("The power consumption is " + str(power) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["00100",
                                       "11110",
                                       "10110",
                                       "10111",
                                       "10101",
                                       "01111",
                                       "00111",
                                       "11100",
                                       "10000",
                                       "11001",
                                       "00010",
                                       "01010"]), 198)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3

#Advent of Code
#2021 Day 3, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import Counter

def solve(puzzle_input):
    def find_rating(rating):
        # rating: 1 => oxygen (most common, 1 in a tie), 0 => co2 (least common, 0 in a tie)

        candidates = puzzle_input[:]
        i = 0
        while len(candidates) > 1:
            count = Counter()
            for number in candidates:
                count.update(number[i])
                
            results = count.most_common(2)
            if results[0][1] == results[-1][1] and results[0][0] != results[-1][0]:
                criteria = str(rating)
            else:
                criteria = count.most_common()[1-rating][0]

            candidates = [n for n in candidates if n[i] == criteria]
            i += 1


        return int(candidates[0], 2)
    
    oxygen = find_rating(1)
    co2 = find_rating(0)
    return oxygen * co2

def main():
    puzzle_input = util.read.as_lines()

    lifesupport = solve(puzzle_input)

    print("The life support rating is " + str(lifesupport) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["00100",
                                       "11110",
                                       "10110",
                                       "10111",
                                       "10101",
                                       "01111",
                                       "00111",
                                       "11100",
                                       "10000",
                                       "11001",
                                       "00010",
                                       "01010"]), 230)

if __name__ == "__main__":
    run(main)
Return to solution index