Browse Source

Adding day02

The original was a bit smaller but I wanted to mess around with dataclasses
pull/3/head
Ryan Reed 3 years ago
parent
commit
c6011ee7fc
4 changed files with 1131 additions and 0 deletions
  1. +6
    -0
      inputs/day02-example1.txt
  2. +1000
    -0
      inputs/day02.txt
  3. +93
    -0
      puzzles/day02.py
  4. +32
    -0
      puzzles/test_day02.py

+ 6
- 0
inputs/day02-example1.txt View File

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

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


+ 93
- 0
puzzles/day02.py View File

@ -0,0 +1,93 @@
"""
Advent of Code 2021 - Day 02
Run with:
python puzzles/day02.py inputs/day02.txt
This is overly complicated to keep tests similar while also using dataclasses for learning
"""
import pathlib
import sys
from typing import List, Tuple
from dataclasses import dataclass
@dataclass
class Location:
horizontal = 0
depth = 0
aim = 0
def run(self, inputs: List[str]) -> int:
for line in inputs:
# forward 20 -> self.forward(20)
step = line.split()
getattr(self, step[0])(int(step[1]))
return self.calculate()
def calculate(self) -> int:
return self.horizontal * self.depth
class Part1(Location):
def forward(self, distance: int) -> None:
self.horizontal += distance
def up(self, distance: int) -> None:
self.depth -= distance
def down(self, distance: int) -> None:
self.depth += distance
class Part2(Location):
def forward(self, distance: int) -> None:
self.horizontal += distance
self.depth += self.aim * distance
def up(self, distance: int) -> None:
self.aim -= distance
def down(self, distance: int) -> None:
self.aim += distance
def part1(inputs: List[int]) -> int:
puzzle = Part1()
return puzzle.run(inputs)
def part2(inputs: List[int]) -> int:
puzzle = Part2()
return puzzle.run(inputs)
def parse(inputs: str) -> List[str]:
"""Parse the input string"""
return inputs.split("\n")
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()

+ 32
- 0
puzzles/test_day02.py View File

@ -0,0 +1,32 @@
import pathlib
import pytest
import day02 as aoc
INPUTS_DIR = f"{pathlib.Path(__file__).parent.parent}/inputs"
@pytest.fixture
def example_data():
input_path = f"{INPUTS_DIR}/day02-example1.txt"
return aoc.parse(pathlib.Path(input_path).read_text().strip())
@pytest.fixture
def day02_data():
input_path = f"{INPUTS_DIR}/day02.txt"
return aoc.parse(pathlib.Path(input_path).read_text().strip())
def test_example1(example_data):
assert aoc.part1(example_data) == 150
def test_example2(example_data):
assert aoc.part2(example_data) == 900
def test_part1(day02_data):
assert aoc.part1(day02_data) == 1670340
def test_part2(day02_data):
assert aoc.part2(day02_data) == 1954293920

Loading…
Cancel
Save