From 71232d04b9445385175d072eff4ac3e62692960b Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 00:49:51 -0500 Subject: [PATCH 1/8] Day 03 - part 1 (will refactor) --- inputs/day03-example.txt | 12 + inputs/day03.txt | 1000 ++++++++++++++++++++++++++++++++++++++ puzzles/day03.py | 65 +++ puzzles/test_day03.py | 38 ++ 4 files changed, 1115 insertions(+) create mode 100644 inputs/day03-example.txt create mode 100644 inputs/day03.txt create mode 100644 puzzles/day03.py create mode 100644 puzzles/test_day03.py diff --git a/inputs/day03-example.txt b/inputs/day03-example.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/inputs/day03-example.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/inputs/day03.txt b/inputs/day03.txt new file mode 100644 index 0000000..d89bdcc --- /dev/null +++ b/inputs/day03.txt @@ -0,0 +1,1000 @@ +110000000001 +010011111011 +111000011110 +110101011110 +100000010100 +000101101111 +110100101000 +011100010001 +101001011011 +011000001100 +111101001011 +010001100100 +100101011010 +111101110101 +101001111100 +010010010101 +011100011000 +010110011101 +110001110101 +001111000010 +111011001011 +111111111110 +010000000100 +001011111101 +100111110101 +011011110100 +110101000100 +111010001110 +110101001011 +011100100110 +011100001001 +101001111101 +111111000010 +010010001100 +110010011001 +010100010010 +011010010000 +101111000100 +001010100101 +100010010100 +001000000110 +000011110100 +101101100001 +111100000111 +011010001110 +010110001000 +111101111010 +001001010111 +011001010011 +000111000010 +000010110001 +101000010111 +111010000100 +111001110111 +000000110001 +011111000110 +010110010111 +110100001001 +101100110110 +100111111000 +110000110000 +110110111101 +110011011100 +010001010010 +011011000101 +000001100010 +011001101001 +000111111011 +111000001101 +110100000101 +011000011011 +100101100110 +001001000001 +111011011010 +010101110110 +100011100000 +100000001011 +110101111001 +000101001000 +110111111010 +011001111111 +101000000101 +101000011111 +010010000111 +101101111100 +101110100001 +100111100101 +101111001100 +110101001111 +011100001111 +010000011110 +000010111111 +011110100101 +100010010110 +011101110011 +000000010111 +110111000110 +100110010100 +011110101111 +101101000111 +111111011100 +110000100010 +010110001010 +011001000100 +111001100011 +111101110100 +000011011100 +110011010001 +110011100110 +100000011110 +100100000010 +001000100001 +011000011111 +001001101001 +000100011100 +100001010000 +100000110010 +111001101000 +100011101101 +000111000101 +000011000101 +111110001011 +001111011101 +110110010100 +011010011011 +111111111010 +001010111111 +101111101001 +010000111100 +111010110111 +100100011000 +111010010110 +010100111100 +101001010000 +011100011011 +100101001110 +011100010110 +111010011100 +100110011001 +100110000110 +111010110010 +011000010111 +111101000110 +000001000011 +001011111011 +011100000111 +111011100100 +101111110000 +011111010010 +001100111000 +011001000010 +100110100111 +101000101110 +110100101100 +011101001100 +111100100011 +110101001101 +101111000110 +111010100100 +100101010101 +111011100000 +011100101010 +111111001111 +010010100001 +111001111111 +101000110101 +010001101010 +000110110011 +001010110011 +110001101001 +110110000011 +100111110011 +000100101100 +111010101100 +101011101100 +010110000100 +101001010101 +000001010011 +011110000000 +101111110011 +001010001000 +110000010001 +101010101100 +011010111010 +010100101000 +101100011011 +010010101011 +110101110001 +010101001101 +011110011001 +100000110011 +010110110000 +101100100010 +010000110100 +001000100101 +100011100011 +111000010110 +000110011000 +000011011000 +010101001000 +101000010100 +111011111011 +010110010001 +100111100011 +011011111100 +001010011110 +011110110101 +111100101110 +001001011110 +010100010111 +100011010001 +110110000111 +001000001101 +001011110001 +111001000011 +101101011101 +111000111010 +111111110001 +001100000000 +110011011010 +011010101001 +011010101011 +011010011100 +100100111110 +111001011110 +001010001101 +101101010100 +100100110100 +011000111111 +101011010100 +111010100110 +001111010010 +011001100000 +011010010100 +001100001010 +101001001111 +111011110110 +100101100001 +000100110010 +101111001001 +111001011000 +100010001101 +101101001010 +011001110001 +010110010110 +000100010100 +011011011100 +000101101011 +110010011110 +010010011010 +111110110010 +000010001111 +000010010011 +000000111010 +110001100011 +000010000100 +101011111000 +110111001001 +101111100000 +110101111000 +100010000011 +000001111111 +000110001110 +000010000000 +110110011110 +000101101000 +001101111011 +011101101100 +101100011101 +110001010100 +011010111000 +011101101010 +110010110101 +100101100000 +101001011100 +100101001010 +011110111011 +111101111100 +101000111001 +111001110001 +101100001111 +100001100111 +011001101000 +111000100101 +000000001011 +110011100011 +111010001011 +111100001011 +111101101001 +111001101110 +000110100101 +000111100001 +101101101101 +100000000011 +001100011111 +101101111110 +101010111000 +010100000001 +000110011011 +000100011000 +000001110110 +100111101001 +001111011110 +000010011110 +101001000010 +101001011111 +110011010010 +101100010110 +101110001111 +000111110011 +011011101010 +101010011010 +101100111001 +111111000000 +101100110101 +110100101101 +010000100111 +110011000001 +010001001000 +000111110000 +100101010110 +001010001001 +100100111010 +001010010110 +010111010010 +010010100000 +010110001011 +011001101011 +110001000111 +111001001000 +001101001101 +101000111100 +100011101001 +100011111010 +100000010101 +110010100110 +001000110101 +111100011011 +011001101010 +101011010000 +011111001110 +010011010111 +101100111000 +011111011010 +110110001010 +100010111110 +100001001001 +000001011101 +110001100101 +011001100111 +111001101010 +001000011001 +000011101000 +100100001001 +110001111000 +010111000101 +000111001101 +001110100010 +010110101011 +100111110001 +110000001001 +011100111001 +100111010000 +101111010000 +001110110100 +010111001010 +000100101111 +100000010010 +110110111000 +111010010001 +001101011001 +001011011100 +001001111000 +011101001011 +001101110111 +111111001000 +011001001001 +011100100111 +011011011011 +101010100111 +010101011100 +100000010110 +101101110010 +110111010001 +010000100110 +010011100011 +101111000011 +011110101011 +111101110010 +001100101111 +111101011101 +001010010001 +001110111110 +010101000010 +111010101101 +100001111000 +111100001111 +011010100000 +110001011110 +010000100001 +110001001001 +100101101011 +000010111010 +000110101101 +100110011000 +100100110111 +100010000001 +001110010000 +010001101110 +111001000000 +100010001011 +111010111010 +000100001101 +010101101000 +001101111010 +111100110100 +010010111100 +011010010110 +000001100000 +111111111001 +101001101111 +100011000110 +001100010110 +000110111110 +000001010001 +101010000011 +000110001011 +000110101011 +000011100111 +101011110110 +110100000100 +111000101101 +000100100010 +011011111001 +000010110000 +111010101010 +011000001101 +101111110001 +110101000001 +000000110011 +000100111111 +111001100100 +000011100010 +001100110001 +111000010000 +001011000000 +111000011011 +100001101001 +111100001000 +001011100000 +111101101000 +100100101111 +110110001111 +011110011110 +010011011100 +110110010001 +011001000111 +101111110101 +011101011111 +010100100010 +101101010110 +010001111111 +111110111000 +001010110101 +111000111000 +110101011010 +011001110110 +011101011001 +010100110111 +101101111111 +111111101011 +111010101111 +010000111011 +111010000111 +000100111010 +010011000100 +101010001011 +010111110110 +100001100011 +111001110100 +011000100101 +000011111111 +100101010111 +011000000110 +001011001000 +011100000000 +011110011100 +100101001001 +000001110000 +010110011110 +001111000101 +100101110001 +110111100111 +001010010000 +110011101011 +011111001011 +011010110000 +011011010110 +000111100011 +110000001010 +000010111101 +100100101001 +010100000100 +001101111100 +111000100010 +010110000001 +111010111101 +000001010100 +100100100110 +110011000000 +000111101000 +000110101110 +011101011010 +001100110011 +101011111001 +001101001011 +100110011100 +110001111001 +101101001100 +110000110011 +110000111110 +000010101010 +010001111010 +001100001001 +111000011100 +101011000001 +101000011110 +111001010101 +010001100001 +011001111001 +100010100001 +111101011110 +111110100001 +001011000011 +100010110111 +010100101110 +110001110011 +111001011111 +001101000010 +000001111110 +010011101100 +111010110000 +110001100001 +101001111001 +000101100001 +111110110110 +010010001110 +001010001111 +001010001100 +111000000010 +000101000011 +010110000101 +011111110111 +000000000111 +011101100000 +000011110010 +010110100100 +111011000101 +010111011001 +111111100011 +010111001011 +101110111110 +100001110110 +111110111100 +110011000110 +101100110111 +001000011111 +000000111111 +010100010000 +100000001111 +100010101000 +110110001101 +010101100101 +111110101111 +010010110011 +000101011000 +110001110100 +001111111010 +110100111110 +111010111111 +110100110101 +100000111111 +011011101110 +000001101000 +110011100010 +111011110101 +111100000010 +101000001110 +100101101001 +101110010111 +000001100001 +000101001111 +000101101010 +101011001010 +001100001100 +111010110110 +101100000100 +010011010011 +001000100111 +001110100001 +010101011101 +101011111101 +111110110011 +001101100000 +101111000010 +001110010101 +000111111100 +000110000100 +000101111001 +000011111000 +000010000110 +001010011000 +111111101001 +011000001111 +111010100001 +111110001111 +101110010110 +010011000011 +110001000010 +110111100110 +010100000111 +000011000010 +000000010010 +100111111011 +001011111000 +110010001100 +000111011101 +000101001010 +001100110101 +110110110000 +110111100101 +000000111000 +100011100111 +100100111101 +011011001011 +000100101000 +111101010101 +010011111101 +110100110100 +110011111011 +100111011011 +101000100100 +011000001110 +101000001000 +110101011001 +100110111100 +110010000001 +000111000011 +010001001100 +101001000100 +100000101101 +011011111010 +101000100010 +011001011111 +011001000001 +001100011010 +100011011111 +110011010111 +111000101000 +001001110110 +101010101011 +001110110110 +011111010011 +100111111010 +001111101110 +110010101000 +000001111000 +100110111101 +110110001001 +111000011111 +011111000111 +111100011000 +101001000110 +110010000010 +000100111000 +001000101000 +011100100001 +101101101010 +110110111011 +001011101100 +110111110111 +011000111101 +110000111000 +101010000101 +111010011011 +111011101010 +101100101100 +000110101000 +110011111001 +010111011000 +100101111101 +101110110000 +100001111010 +110101100001 +100010011110 +011000100100 +101110101111 +111011011110 +000110110100 +010100001100 +101001101000 +001001000010 +100110001001 +001011111100 +100011111100 +011100001011 +001100000100 +100111001011 +011101000101 +011110000100 +011011011101 +001001011010 +110110011101 +000000001100 +010111100101 +111011011101 +000100101110 +001100011001 +110001001110 +101010100001 +011101100011 +100000111000 +111111100111 +001000100011 +110000000011 +101001010010 +111101010100 +111101111110 +001110100100 +111100100010 +010000100101 +010100001101 +011010011110 +001110000110 +101000000001 +010111111000 +111111011011 +110110101010 +000000110110 +111001111010 +001110110010 +111011010110 +001100100010 +011111000100 +000111010000 +100101110111 +001011110011 +111110011011 +111001010011 +000110011100 +010111000111 +101000001100 +010101111011 +100001011011 +010100110101 +010000000000 +110010011100 +110101001110 +111010111110 +101101110101 +011010101111 +101010011110 +010111110100 +010100110011 +110110001100 +001011110100 +010001000000 +100011010110 +100111000000 +010111100000 +100010100110 +001100101101 +010111100110 +101101000100 +101110011001 +011011100101 +101101101111 +110010100000 +100100100010 +001100110000 +110110011100 +000010000001 +101001101001 +001001100110 +000101100011 +111101000111 +000110000011 +011011110000 +110010000011 +010111000001 +101101011001 +101100001001 +101100010101 +011010011111 +010100011100 +100111100111 +101110100110 +101011010101 +110110101111 +010001010101 +000010001100 +001110000001 +110011111100 +000001111001 +001000111101 +010011110001 +110100010010 +100000100101 +100100000011 +111110010000 +110010001001 +011111100001 +010011011001 +111110110001 +000011010010 +011101101011 +110011100001 +000110101111 +001111101101 +110101110000 +001000011101 +111111001100 +110101101111 +000110111000 +101100010100 +011101100100 +111000101010 +110001001011 +100010001111 +001101000111 +011000010000 +110010110001 +111100100001 +110111110110 +000010000011 +100001111110 +111001001001 +001011011101 +000100011101 +001100100111 +000011010001 +111111001110 +001111111101 +110001110001 +101010011101 +110010010101 +001011010101 +111110010011 +011001101110 +101000011100 +110100011111 +000001001111 +100001111001 +001101110110 +010111111100 +111101011111 +001111110001 +101001101110 +100100100111 +100010011100 +010110011111 +100010111011 +111011110010 +111010010011 +111001111100 +011100010011 +001010100100 +111001100101 +111100011001 +101110001011 +100000011111 +010010011000 +100001000011 +001001111101 +010111001100 +100101011101 +110011101010 +010010101110 +100011001101 +101100101001 +011110010000 +101000011010 +110100010101 +010110111110 +011110010111 +100000001000 +101000011000 +000111100111 +011001100110 +001010011001 +011011101011 +101000000110 +000100010010 +100100101011 +000101011111 +010000110101 +000000011010 +111101111001 +111011011111 +010000001001 +100101100011 +000111011001 +111011011011 +000011011001 +111101011001 +001000001100 +000110001000 +110111010010 +101100101011 +111101110001 +110101101101 +100111001110 +110010111110 +000010001010 +101101000001 +000011101111 +110000100100 +010001101001 +000000001110 +001110011101 +000100101011 +110100111100 +011000100111 +001110101101 +010101100100 +101110100000 +100110001010 +101110010000 +000111111010 +011011110101 +001111110100 +000111110110 +101011111110 +100010110000 +100100100101 +101000001101 +010011111111 +001110111001 +101010110110 +110010011011 +110001001000 +101110100011 +101001011000 +100111101110 +100111000111 +001000111100 +010101111010 +100010100101 +110011010011 +100100000111 +111010101001 +110001001101 +011000101001 +000110100001 +100011111110 +001100011000 +110100010000 +001010110100 +000100000110 +001011101010 +001100100110 +011111101011 +000101111101 +001110110101 +110000001111 +001010000011 +110101001000 +001000010001 +111011101011 +001001011101 +000001000000 +000110010101 +100110101010 +101010000111 +110101101110 +100000010011 +001110001011 +011110000010 +100000100010 +111111101100 +011010000011 +001101001110 +011100001000 +000001001110 +000111001000 +100100011010 +010000010100 +111110001100 +111100101111 +101000100101 +011100101110 +000011101101 +000100100100 +011111111001 +000100110011 +001011001011 +110101100000 +101111111011 +001101111101 +100110100011 diff --git a/puzzles/day03.py b/puzzles/day03.py new file mode 100644 index 0000000..a120f60 --- /dev/null +++ b/puzzles/day03.py @@ -0,0 +1,65 @@ +""" +Advent of Code 2021 - Day 03 + +Run with: + python puzzles/day03.py inputs/day03.txt +""" + +import pathlib +import sys +from typing import List, Tuple + +from collections import defaultdict + +def part1(inputs: List[int]) -> int: + num_1 = defaultdict(int) + for line in inputs: + for index, value in enumerate(line): + num_1[index] += int(value) + + total = len(inputs) + gamma = epsilon = "" + for value in num_1.values(): + if value > (total/2): + gamma += "1" + epsilon += "0" + else: + gamma += "0" + epsilon += "1" + + gamma = int(gamma, 2) + epsilon = int(epsilon, 2) + power = gamma * epsilon + return power + + +def part2(inputs: List[int]) -> int: + return False + + +def parse(inputs: str) -> List[int]: + """Parse the input string""" + return [line for line in inputs.split()] + + +def solve(path: str) -> Tuple[int, int]: + """Solve the puzzle""" + puzzle_input = parse(pathlib.Path(path).read_text().strip()) + part1_result = part1(puzzle_input) + part2_result = part2(puzzle_input) + + return part1_result, part2_result + + +def main() -> None: + for path in sys.argv[1:]: + print(f"Input File: {path}") + + part1_result, part2_result = solve(path) + + print(f"Part 1 Result: {part1_result}") + print(f"Part 2 Result: {part2_result}") + + +if __name__ == "__main__": + main() diff --git a/puzzles/test_day03.py b/puzzles/test_day03.py new file mode 100644 index 0000000..8e2e458 --- /dev/null +++ b/puzzles/test_day03.py @@ -0,0 +1,38 @@ +""" +Remove the 'Not implemented' marks when ready to run the test +""" +import pathlib +import pytest +import day03 as aoc + +INPUTS_DIR = f"{pathlib.Path(__file__).parent.parent}/inputs" + +@pytest.fixture +def example_data(): + input_path = f"{INPUTS_DIR}/day03-example.txt" + return aoc.parse(pathlib.Path(input_path).read_text().strip()) + + +@pytest.fixture +def day03_data(): + input_path = f"{INPUTS_DIR}/day03.txt" + return aoc.parse(pathlib.Path(input_path).read_text().strip()) + + +def test_example1(example_data): + assert aoc.part1(example_data) == 198 + + +@pytest.mark.skip(reason="Not implemented") +def test_example2(example_data): + assert aoc.part2(example_data) == ... + + +@pytest.mark.skip(reason="Not implemented") +def test_part1(day02_data): + assert aoc.part1(day02_data) == ... + + +@pytest.mark.skip(reason="Not implemented") +def test_part2(day02_data): + assert aoc.part2(day02_data) == ... -- 2.30.1 From c31eb2ef7734ce4a7ac6a42d0477fa11a1563d70 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 11:47:12 -0500 Subject: [PATCH 2/8] Converting to a class --- puzzles/day03.py | 49 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/puzzles/day03.py b/puzzles/day03.py index a120f60..f6e4860 100644 --- a/puzzles/day03.py +++ b/puzzles/day03.py @@ -11,27 +11,36 @@ from typing import List, Tuple from collections import defaultdict -def part1(inputs: List[int]) -> int: - num_1 = defaultdict(int) - for line in inputs: - for index, value in enumerate(line): - num_1[index] += int(value) - - total = len(inputs) - gamma = epsilon = "" - for value in num_1.values(): - if value > (total/2): - gamma += "1" - epsilon += "0" - else: - gamma += "0" - epsilon += "1" - - gamma = int(gamma, 2) - epsilon = int(epsilon, 2) - power = gamma * epsilon - return power +class Submarine: + gamma: int = 0 + epsilon: int = 0 + power: int = 0 + + def decode_report(self, report: List[int]) -> None: + num_1 = defaultdict(int) + for binary in report: + for index, bit in enumerate(binary): + num_1[index] += int(bit) + + total = len(report) + gamma = epsilon = "" + for binary in num_1.values(): + if binary > (total/2): + gamma += "1" + epsilon += "0" + else: + gamma += "0" + epsilon += "1" + + self.gamma = int(gamma, 2) + self.epsilon = int(epsilon, 2) + self.power = self.gamma * self.epsilon + +def part1(inputs: List[int]) -> int: + sub = Submarine() + sub.decode_report(inputs) + return sub.power def part2(inputs: List[int]) -> int: return False -- 2.30.1 From 614bdb6f996b8d3e282a4d7c57f1ddd84239c210 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 16:22:47 -0500 Subject: [PATCH 3/8] Initial working version for both parts --- puzzles/day03.py | 114 ++++++++++++++++++++++++++++++++++-------- puzzles/test_day03.py | 13 ++--- 2 files changed, 99 insertions(+), 28 deletions(-) diff --git a/puzzles/day03.py b/puzzles/day03.py index f6e4860..f2675e6 100644 --- a/puzzles/day03.py +++ b/puzzles/day03.py @@ -7,43 +7,117 @@ Run with: import pathlib import sys -from typing import List, Tuple +from typing import Dict, List, Tuple from collections import defaultdict -class Submarine: - gamma: int = 0 - epsilon: int = 0 - power: int = 0 +class Diagnostics: + _most_common = None + report = None + + def __init__(self, report: List[int]) -> None: + self.report = report + self._most_common = self.commonality(report) - def decode_report(self, report: List[int]) -> None: - num_1 = defaultdict(int) - for binary in report: - for index, bit in enumerate(binary): - num_1[index] += int(bit) - total = len(report) + def power_usage(self) -> None: + half_count = len(self.report)/2 gamma = epsilon = "" - for binary in num_1.values(): - if binary > (total/2): + for common in self._most_common: + if common == 1: gamma += "1" epsilon += "0" else: gamma += "0" epsilon += "1" - self.gamma = int(gamma, 2) - self.epsilon = int(epsilon, 2) - self.power = self.gamma * self.epsilon + gamma = int(gamma, 2) + epsilon = int(epsilon, 2) + return gamma * epsilon + + + def calculate_oxygen_generator_rating(self, inputs) -> int: + """ + Bit Criteria: + Most common value in current bit position + If 0 and 1 are equally common, keep values with a 1 + Keep only values with this bit in this position + """ + report = inputs.copy() + for column in range(len(report[0])): + most_common = self.commonality(report) + for binary in list(report): + if (int(binary[column]) != most_common[column] and most_common[column] != 2) or (most_common[column] == 2 and binary[column] != "1"): + report.remove(binary) + + if len(report) == 1: + return int(report[0], 2) + + if len(report) > 1: # May not be necessary + report = self.calculate_oxygen_generator_rating(report) + + return int(report[0], 2) + + + def calculate_co2_scrubber_rating(self, inputs) -> int: + """ + Bit Criteria: + Least common value in current position + If 0 and 1 are equally common, keep values with a 0 + Keep only values with this bit in this position + """ + report = inputs.copy() + for column in range(len(report[0])): + most_common = self.commonality(report) + for binary in list(report): + if (int(binary[column]) == most_common[column]) or (most_common[column] == 2 and binary[column] != "0"): + report.remove(binary) + + if len(report) == 1: + return int(report[0], 2) + + if len(report) > 1: # May not be necessary + report = self.calculate_oxygen_generator_rating(report) + + return int(report[0], 2) + + + def calculate_life_support_rating(self) -> int: + oxygen_generator_rating = self.calculate_oxygen_generator_rating(self.report) + co2_scrubber_rating = self.calculate_co2_scrubber_rating(self.report) + return oxygen_generator_rating * co2_scrubber_rating + + @staticmethod + def commonality(report: List[int]) -> List[Dict[str, int]]: + common_counts = [] + for binary in report: + for column, bit in enumerate(binary): + try: + common_counts[column][bit] += 1 + except IndexError: + common_counts.append(defaultdict(int)) + common_counts[column][bit] = 1 + common = [] + half_total = len(report)/2 + for count in common_counts: + if count["1"] == half_total: + common.append(2) # Indicating equally common + elif count["1"] > half_total: + common.append(1) + else: + common.append(0) + + return common + def part1(inputs: List[int]) -> int: - sub = Submarine() - sub.decode_report(inputs) - return sub.power + diag = Diagnostics(inputs) + return diag.power_usage() def part2(inputs: List[int]) -> int: - return False + diag = Diagnostics(inputs) + return diag.calculate_life_support_rating() def parse(inputs: str) -> List[int]: diff --git a/puzzles/test_day03.py b/puzzles/test_day03.py index 8e2e458..de6fa30 100644 --- a/puzzles/test_day03.py +++ b/puzzles/test_day03.py @@ -23,16 +23,13 @@ def test_example1(example_data): assert aoc.part1(example_data) == 198 -@pytest.mark.skip(reason="Not implemented") def test_example2(example_data): - assert aoc.part2(example_data) == ... + assert aoc.part2(example_data) == 230 -@pytest.mark.skip(reason="Not implemented") -def test_part1(day02_data): - assert aoc.part1(day02_data) == ... +def test_part1(day03_data): + assert aoc.part1(day03_data) == 3885894 -@pytest.mark.skip(reason="Not implemented") -def test_part2(day02_data): - assert aoc.part2(day02_data) == ... +def test_part2(day03_data): + assert aoc.part2(day03_data) == 4375225 -- 2.30.1 From 659e2105a517704cf2d954015de5976be96d4f16 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 16:30:18 -0500 Subject: [PATCH 4/8] Cleanup --- puzzles/day03.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/puzzles/day03.py b/puzzles/day03.py index f2675e6..e920ede 100644 --- a/puzzles/day03.py +++ b/puzzles/day03.py @@ -12,18 +12,16 @@ from typing import Dict, List, Tuple from collections import defaultdict class Diagnostics: - _most_common = None report = None def __init__(self, report: List[int]) -> None: self.report = report - self._most_common = self.commonality(report) def power_usage(self) -> None: - half_count = len(self.report)/2 + most_common = self.commonality(self.report) gamma = epsilon = "" - for common in self._most_common: + for common in most_common: if common == 1: gamma += "1" epsilon += "0" -- 2.30.1 From 2a6e7403cc92c8fec628eefed6431bcc836555d7 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 17:05:34 -0500 Subject: [PATCH 5/8] Updating function names --- puzzles/day03.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/puzzles/day03.py b/puzzles/day03.py index e920ede..b7dff89 100644 --- a/puzzles/day03.py +++ b/puzzles/day03.py @@ -18,7 +18,7 @@ class Diagnostics: self.report = report - def power_usage(self) -> None: + def calc_power_usage(self) -> None: most_common = self.commonality(self.report) gamma = epsilon = "" for common in most_common: @@ -34,7 +34,7 @@ class Diagnostics: return gamma * epsilon - def calculate_oxygen_generator_rating(self, inputs) -> int: + def calc_oxygen_generator_rating(self, inputs) -> int: """ Bit Criteria: Most common value in current bit position @@ -52,12 +52,12 @@ class Diagnostics: return int(report[0], 2) if len(report) > 1: # May not be necessary - report = self.calculate_oxygen_generator_rating(report) + report = self.calc_oxygen_generator_rating(report) return int(report[0], 2) - def calculate_co2_scrubber_rating(self, inputs) -> int: + def calc_co2_scrubber_rating(self, inputs) -> int: """ Bit Criteria: Least common value in current position @@ -75,16 +75,17 @@ class Diagnostics: return int(report[0], 2) if len(report) > 1: # May not be necessary - report = self.calculate_oxygen_generator_rating(report) + report = self.calc_oxygen_generator_rating(report) return int(report[0], 2) - def calculate_life_support_rating(self) -> int: - oxygen_generator_rating = self.calculate_oxygen_generator_rating(self.report) - co2_scrubber_rating = self.calculate_co2_scrubber_rating(self.report) + def calc_life_support_rating(self) -> int: + oxygen_generator_rating = self.calc_oxygen_generator_rating(self.report) + co2_scrubber_rating = self.calc_co2_scrubber_rating(self.report) return oxygen_generator_rating * co2_scrubber_rating + @staticmethod def commonality(report: List[int]) -> List[Dict[str, int]]: common_counts = [] @@ -108,14 +109,14 @@ class Diagnostics: return common - def part1(inputs: List[int]) -> int: diag = Diagnostics(inputs) - return diag.power_usage() + return diag.calc_power_usage() + def part2(inputs: List[int]) -> int: diag = Diagnostics(inputs) - return diag.calculate_life_support_rating() + return diag.calc_life_support_rating() def parse(inputs: str) -> List[int]: -- 2.30.1 From 6950609ab011f4cda88371803ad388d47f4b4a3a Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 17:05:46 -0500 Subject: [PATCH 6/8] Simplifying commonality --- puzzles/day03.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/puzzles/day03.py b/puzzles/day03.py index b7dff89..eb7c82f 100644 --- a/puzzles/day03.py +++ b/puzzles/day03.py @@ -88,20 +88,17 @@ class Diagnostics: @staticmethod def commonality(report: List[int]) -> List[Dict[str, int]]: - common_counts = [] + num_1 = defaultdict(int) for binary in report: for column, bit in enumerate(binary): - try: - common_counts[column][bit] += 1 - except IndexError: - common_counts.append(defaultdict(int)) - common_counts[column][bit] = 1 + num_1[column] += int(bit) + common = [] half_total = len(report)/2 - for count in common_counts: - if count["1"] == half_total: + for count in num_1.values(): + if count == half_total: common.append(2) # Indicating equally common - elif count["1"] > half_total: + elif count > half_total: common.append(1) else: common.append(0) -- 2.30.1 From f61cfdf04d46b5118c3d35fb6e04d90fb12e5540 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 17:07:00 -0500 Subject: [PATCH 7/8] Black formatting --- puzzles/day03.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/puzzles/day03.py b/puzzles/day03.py index eb7c82f..d1598de 100644 --- a/puzzles/day03.py +++ b/puzzles/day03.py @@ -11,13 +11,13 @@ from typing import Dict, List, Tuple from collections import defaultdict + class Diagnostics: report = None def __init__(self, report: List[int]) -> None: self.report = report - def calc_power_usage(self) -> None: most_common = self.commonality(self.report) gamma = epsilon = "" @@ -33,7 +33,6 @@ class Diagnostics: epsilon = int(epsilon, 2) return gamma * epsilon - def calc_oxygen_generator_rating(self, inputs) -> int: """ Bit Criteria: @@ -45,18 +44,20 @@ class Diagnostics: for column in range(len(report[0])): most_common = self.commonality(report) for binary in list(report): - if (int(binary[column]) != most_common[column] and most_common[column] != 2) or (most_common[column] == 2 and binary[column] != "1"): + if ( + int(binary[column]) != most_common[column] + and most_common[column] != 2 + ) or (most_common[column] == 2 and binary[column] != "1"): report.remove(binary) if len(report) == 1: return int(report[0], 2) - if len(report) > 1: # May not be necessary + if len(report) > 1: # May not be necessary report = self.calc_oxygen_generator_rating(report) return int(report[0], 2) - def calc_co2_scrubber_rating(self, inputs) -> int: """ Bit Criteria: @@ -68,24 +69,24 @@ class Diagnostics: for column in range(len(report[0])): most_common = self.commonality(report) for binary in list(report): - if (int(binary[column]) == most_common[column]) or (most_common[column] == 2 and binary[column] != "0"): + if (int(binary[column]) == most_common[column]) or ( + most_common[column] == 2 and binary[column] != "0" + ): report.remove(binary) if len(report) == 1: return int(report[0], 2) - if len(report) > 1: # May not be necessary + if len(report) > 1: # May not be necessary report = self.calc_oxygen_generator_rating(report) return int(report[0], 2) - def calc_life_support_rating(self) -> int: oxygen_generator_rating = self.calc_oxygen_generator_rating(self.report) co2_scrubber_rating = self.calc_co2_scrubber_rating(self.report) return oxygen_generator_rating * co2_scrubber_rating - @staticmethod def commonality(report: List[int]) -> List[Dict[str, int]]: num_1 = defaultdict(int) @@ -94,10 +95,10 @@ class Diagnostics: num_1[column] += int(bit) common = [] - half_total = len(report)/2 + half_total = len(report) / 2 for count in num_1.values(): if count == half_total: - common.append(2) # Indicating equally common + common.append(2) # Indicating equally common elif count > half_total: common.append(1) else: -- 2.30.1 From 8e208459d29eeca8bc64739ce43a85d5e31af8fa Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 3 Dec 2021 17:12:54 -0500 Subject: [PATCH 8/8] Adding docstring for commonality --- puzzles/day03.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/puzzles/day03.py b/puzzles/day03.py index d1598de..832277a 100644 --- a/puzzles/day03.py +++ b/puzzles/day03.py @@ -88,7 +88,20 @@ class Diagnostics: return oxygen_generator_rating * co2_scrubber_rating @staticmethod - def commonality(report: List[int]) -> List[Dict[str, int]]: + def commonality(report: List[int]) -> List[int]: + """ + Return the most common value in each column of binary numbers + + Args: + report: List of binary numbers + + Returns: + List containing the most common value in the column + Possible Values: + 0 + 1 + 2 - This indicates equally common + """ num_1 = defaultdict(int) for binary in report: for column, bit in enumerate(binary): -- 2.30.1