From 64e7a253f2d0256998208c99fec33bf1fdcda248 Mon Sep 17 00:00:00 2001 From: gabe Date: Sat, 20 Aug 2022 22:35:27 -0500 Subject: [PATCH] finished day 3 --- Pipfile.lock | 9 +- day3/day3.py | 38 ++++++ day3/example1 | 11 ++ day3/input | 323 ++++++++++++++++++++++++++++++++++++++++++++++ day3/test_day3.py | 43 ++++++ template.py | 3 +- 6 files changed, 418 insertions(+), 9 deletions(-) create mode 100755 day3/day3.py create mode 100644 day3/example1 create mode 100644 day3/input create mode 100644 day3/test_day3.py diff --git a/Pipfile.lock b/Pipfile.lock index 9431ae8..30838e9 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "bf24791e041f80c18b2c667226aa3b47e25ebb5779ca595d076d8cf87f785918" + "sha256": "1a3671b568f0cf9b012b6e7415c1952a33c8121d6876697499fff3dbdb69783f" }, "pipfile-spec": 6, "requires": { @@ -39,13 +39,6 @@ "markers": "python_version >= '3.6'", "version": "==21.3" }, - "parse": { - "hashes": [ - "sha256:9ff82852bcb65d139813e2a5197627a94966245c897796760a3a2a8eb66f020b" - ], - "index": "pypi", - "version": "==1.19.0" - }, "pluggy": { "hashes": [ "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159", diff --git a/day3/day3.py b/day3/day3.py new file mode 100755 index 0000000..e15d716 --- /dev/null +++ b/day3/day3.py @@ -0,0 +1,38 @@ +#! /usr/bin/env python3 + +import pathlib +import sys +import re +from dataclasses import dataclass +from pprint import pprint + +def parse(puzzle_input: str): + """Parse input""" + # returns a 2 dimentional array where true means there is a tree there. + return [[i=='#' for i in j] for j in puzzle_input.splitlines()] + +def part1(data): + """Solve part 1""" + return solveForSlope(data, 3, 1) + +def solveForSlope(data, right: int, down: int): + return sum(1 for index, row in enumerate(data[::down]) if row[(right*index)%len(row)]) + +def part2(data): + """Solve part 2""" + return solveForSlope(data,1,1)*solveForSlope(data,3,1)*solveForSlope(data,5,1)*solveForSlope(data,7,1)*solveForSlope(data,1,2) + +def solve(puzzle_input): + """Solve the puzzle for the given input""" + data = parse(puzzle_input) + solution1 = part1(data) + solution2 = part2(data) + + return solution1, solution2 + +if __name__ == "__main__": + for path in sys.argv[1:]: + print(f"{path}:") + puzzle_input = pathlib.Path(path).read_text().strip() + solutions = solve(puzzle_input) + print("\n".join(str(solution) for solution in solutions)) diff --git a/day3/example1 b/day3/example1 new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/day3/example1 @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# diff --git a/day3/input b/day3/input new file mode 100644 index 0000000..e4e0663 --- /dev/null +++ b/day3/input @@ -0,0 +1,323 @@ +........#.............#........ +...#....#...#....#............. +.#..#...#............#.....#..# +..#......#..##............###.. +..........#......#..#..#....... +.#..#.......#.........#.#...... +.........#..#....##..#.##....#. +..#....##...#.................. +##..........#.##...#....##..#.. +...#....#...#..............#... +...........................#..# +..##.##.#..................#... +...#.##..#............#........ +........#.......#...#.....##.#. +.##..........#......#.......#.. +...#..........#...#..#.......#. +......#...#...#.##.......#.#... +........#...#...#...##......... +#..............#.#....#.......# +..#..#..#.#....#............... +.....#........#...#..........#. +##......#...#..#.##.......#.... +..#.#.....#.#.............#.#.# +#..#..##......##...#........... +..#......#........#.....#...... +.....#.......#....#.#...#...... +...#........#...........#...#.. +.......#.#...........###....#.. +...#...........##....##........ +#....#..####....#.....#..#....# +..........#...........#........ +...#.......#....#.#.........#.. +....#...#.......#..###......... +......#......#..#......#..#.... +...#.....#............#..#..... +...#.#.#.#..#.......#.....#.... +#....##...#.........#...##..... +#..#.......#..#..#..#...##..... +#.......#............#.....#... +.#........##....##...#........# +.....#...#..................... +.......#........#.............. +.....#............#.#.#...#.#.. +.....##..#.............#....... +..#.##..#........#..#...#...... +.........#.#....#...........#.. +.#.....#..#....#.....#...#..... +....#.#................#....... +...............##......#...#... +.##...#...#.......##.#....#.... +............#........#.......#. +......##.#.#................... +.#.#..............#.......#.... +#.....#...#.......#..#...#..... +.............#....#..#......#.. +........#...##................# +.......#...#..#..##............ +..#..#...##...#..#.#.....#...#. +.#.#...#.........#.#........... +...###....#.......#...#........ +........#......##.#...#..##..#. +.....................#.#....... +.............#...........#...#. +#..#..#.....#.#...#............ +...#....#.....#...........#.... +..##.....##...#......#..##..... +#.....#.....###.#.....#....##.. +.#...........###............... +..................#..##.#...#.. +................#....##.#...... +.#.#.#...#....#.........#..#.#. +#.......#........##............ +.......##.#....#.#............# +..........#..##.#....#......... +........##..#....#............. +.........#....#...........##... +#.........#.#..#..#..........#. +.....#........#......#......... +....#.#.#...............#...... +.#..#..##...#.##..........#.... +..#....................#.#..... +.........#....#...........#.#.# +........#....##.##............. +..#.....#.......#..#......#.... +#..........#.#.....#.#....#.... +........##.#.....#..#.....#.#.. +...................#...#....#.# +............#..#....#...#...#.. +..............#.#.........#.... +...#..#..#.#..##..##........... +.#...........................#. +.#.......#...........#....#.#.# +......#..#...#........#...##... +.........#......#.#.......#...# +...#..##................#...... +.............#.#..##....#.#.... +...............#..#......#..... +.#......#.#.#....#........#.... +........#..#.##..#..#.........# +...#....#.#...#..#.......#..#.. +..#...##.........#..#...#...... +...#...........#.............#. +....#.....................#.... +.....#..#...............#.#...# +....#..........#........#...... +..#....#........##..##......... +...#....#..#.#.......#...#..... +..#........#....#...##....#.#.. +.#...#........##.....#....###.. +#....#....##......#........#... +.........#..#.#..........#....# +....#...#.....#.......##....... +..............#..........#.##.. +#...#..#..............#......#. +.................#......##....# +..#..##..#.......#..#.#......#. +.............#........#.....#.# +.#.##............#..#.......... +..#...#...........#..##........ +.#....#...#....#.......#....... +...#.#..#..#..#....#.....#..#.. +....#..##..............#...#... +#..........###......###........ +.##.##......#..#............#.. +.#...........#.#.....#...#..... +#.#..#...#............#........ +.........#...#...#..........##. +.......###..#..........#....... +...........###.....#........#.. +.#.............#.....#......#.. +...#.....#....#.#.........##... +....##..##...#.......##........ +......#....##.........#......#. +..........#.....##..#.....#..#. +..........####...#..#.........# +.##....#..#.#...#.......#...... +...#.#.##.#.#...#....#.#.#..... +.........#...##........##.....# +..#........#..........##...##.# +##...##..........#.#........... +..............#......#......... +........#.....#.#.......#...... +.#...#.....#....#.#..#......... +.....#....................##... +....#..................#.#...## +.....#............#..##........ +#..........#....#.#.......##.#. +....#..#.....................#. +#..#....##.....#............... +..#...#..#..##....#.#.......... +.......#......#.#.......#.....# +...#.#.......#...#.##.......... +....#..........#....#.#.#...... +.......#..#..........#..##..... +#......#......#...#......#...#. +###..#....##......##........#.. +.#..........#.....#.......#.#.. +.......#.....#.....#.#......... +..#...#....#................... +..............#.##............. +.#...#.......#.##...#.#.......# +.......#......................# +....#.#...#.#........#......... +.#......#....#...#............. +#.......#...###.....#.#.#..#... +#....##.#...............##..... +..#.......#..................#. +.....####...............#...... +.##......#......#.#.......##.#. +#......##..###....#....#......# +.##.......##.##...#.##......... +......##............#.......#.. +......#..#.....##.#............ +.#..........#.....##........... +#.........#......#......##.#... +.........#.......#..#......#.#. +.........#.......#...........#. +.#..##.#..................##... +.............#.............#... +.....##........#......##...##.. +..#..#.#.....#..#....#......... +.....#....#.....#.....#........ +#......##.....#....#....#...... +#.................#..#.#......# +.......#..#......#....#.#...#.# +....#.........#..#..........#.# +##......#............#...#...#. +....##......#...#.....#....##.. +.#...##.........#.............. +......#.....................#.. +..#..........###....#.......... +#....#...#..#.............#.... +#........#.#......#....#....... +.#...#.......#..#...#.#...#..#. +................##.#.....#..... +###.......#...#................ +...#.......#...#.#.....#....... +..#.........#.....#.#.......#.. +......#.......................# +#.....#.#..#....#.......#...... +...#....#..#....####........... +.............#.....#...##...... +.......#.........#...#..#...... +.##..#.........#....#.#........ +....##...#.#...........#....#.. +.........................##.... +..###.......##....#.#.........# +.#....#.#.#...........##....#.. +......#...#..#..#..#..#.......# +..#....#.#.......#..#..#..#...# +.....##...#.##....#.#...#...... +.........#..#....#..#.......... +.##..##.........#.#.....#...... +..........#...##...#.#...#..... +#.##..#..#.............#....... +...#...........#.......#......# +.......#....#....#...##.......# +..#.##........###..#......#.... +...#...........###......#..#..# +.#.........#.#.........#.#..... +##.......##.##.##......##...... +............#...#..........#... +....................#.......... +...#..#...........#...#...#.... +.................#...#......### +...#................#.#.##..... +...............#........#...... +#.............##......#.#..#... +..#.#.....#..#.##.....##...#... +......#.........#......#....... +#.......#......#....#........#. +.#..##.....#.........#......... +....##.##.#...#.........##.#... +...............#..#..#..##..... +.#..#...............###........ +.##............##.............. +...............#...##...#...#.# +..#.#......#.#..#.............# +#.#..#..##.........#.#.#...#... +....##.#....................##. +.........#..#.....#.....#..#..# +....#......#......#.##....#.... +........###..#.............#..# +##................#.........#.. +#.....#.......#....#........... +..#.......#..#........#....#... +..#.#.##..#.#...##........#.##. +..#..........#............#.... +..........#...............##... +..........###........#.#....... +.....###..#.............#...... +##.............#...#.....#..... +.....#......#....#........#.#.. +............#..#..............# +.................#...........## +#........#.........###.....#... +..#.#..............##......#.#. +.#...........#.........#..##..# +............................... +.#.....#..#....#....#......#... +.#...#......#.#..#....#.......# +......#.##.......#......#...... +......#..###..#................ +#..#.....#........##...#....... +......##.........##....#...##.. +.#..........#.................# +#..#.......#...............#... +.........#..###....#.#.##.#.... +..#...#.##..##...............## +.........#..................... +.#....##...#......#....#....... +............#..........#..#.... +...#......##....#....#........# +.#...................#......... +#.#........###....#..........#. +.........#....#....#........##. +.#....#..#.........#..#........ +...............#..#...#..#...## +.........#....##....#......#... +.#............................. +...#........#...#.#...#.#..#... +.....#..##...#.#............... +#.....#....#.........#......... +#...#...........##.........#... +..##........#.#...#...#......#. +...........#.....#...#.#....... +......###....#.....#........... +......##...#..........#....#.#. +.......##..##..........#....... +....#............#..#....##.... +..##...................#.#..... +...#.#..#.#.................... +.#..##..#............##.###..#. +#.#...#....#.#..........#.#.... +........#....#.....#........... +..##....#...#.......#.......... +...........##.##....#.......... +.....#............#............ +.......#.............#....#.... +.................#......#...... +......##.......#....#..##...#.. +.#..#....#..................... +...#.#.#...#......##........... +##........##.#....#....#....... +.......#.....#..#..#...#.##.... +#..........#....#.#..#..#..#... +...##..............#........... +.........#.....#.#....#.......# +.........#....##..#..##..#..... +.....#......................#.. +...###...#..#......#........... +....#.....................#.... +............................... +..#.....###.......#..#....#.... +#..........#.................#. +......#.......###.......#..##.. +.............#.##.............. +......#..#.#..#...........#.... +...#....##.#...#..#.#...#....#. +..................#...#....#.## +......#.#....#................. +......#.#.....#.....#..##...... +#..##...........#..#.....#.##.. diff --git a/day3/test_day3.py b/day3/test_day3.py new file mode 100644 index 0000000..0654e7e --- /dev/null +++ b/day3/test_day3.py @@ -0,0 +1,43 @@ +#! /usr/bin/env python3 + +import pathlib +import pytest +import day3 as aoc + +PUZZLE_DIR = pathlib.Path(__file__).parent + +#these test fixtures setup the test, mainly by reading the filename into a string in this simple case. +@pytest.fixture +def example1(): + puzzle_input = (PUZZLE_DIR / "example1").read_text().strip() + return aoc.parse(puzzle_input) + +@pytest.fixture +def example2(): + puzzle_input = (PUZZLE_DIR / "example1").read_text().strip() + return aoc.parse(puzzle_input) + +# @pytest.mark.skip(reason="Not implemented") +def test_parse_example1(example1): + """Test that input is parsed properly""" + assert example1 == [[False, False, True, True, False, False, False, False, False, False, False], + [True, False, False, False, True, False, False, False, True, False, False], + [False, True, False, False, False, False, True, False, False, True, False], + [False, False, True, False, True, False, False, False, True, False, True], + [False, True, False, False, False, True, True, False, False, True, False], + [False, False, True, False, True, True, False, False, False, False, False], + [False, True, False, True, False, True, False, False, False, False, True], + [False, True, False, False, False, False, False, False, False, False, True], + [True, False, True, True, False, False, False, True, False, False, False], + [True, False, False, False, True, True, False, False, False, False, True], + [False, True, False, False, True, False, False, False, True, False, True]] + +# @pytest.mark.skip(reason="Not implemented") +def test_part1_example1(example1): + """Test part 1 on example input""" + assert aoc.part1(example1) == 7 + +# @pytest.mark.skip(reason="Not implemented") +def test_part2_example2(example2): + """Test part 2 on example input""" + assert aoc.part2(example2) == 336 diff --git a/template.py b/template.py index 36e50d4..853407c 100644 --- a/template.py +++ b/template.py @@ -4,8 +4,9 @@ import pathlib import sys import re from dataclasses import dataclass +from pprint import pprint -def parse(puzzle_input): +def parse(puzzle_input: str): """Parse input""" def part1(data):