Browse Source

Day 03 - part 1 (will refactor)

pull/3/head
Ryan Reed 3 years ago
parent
commit
71232d04b9
4 changed files with 1115 additions and 0 deletions
  1. +12
    -0
      inputs/day03-example.txt
  2. +1000
    -0
      inputs/day03.txt
  3. +65
    -0
      puzzles/day03.py
  4. +38
    -0
      puzzles/test_day03.py

+ 12
- 0
inputs/day03-example.txt View File

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

+ 1000
- 0
inputs/day03.txt
File diff suppressed because it is too large
View File


+ 65
- 0
puzzles/day03.py View File

@ -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()

+ 38
- 0
puzzles/test_day03.py View File

@ -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) == ...

Loading…
Cancel
Save