finished day 3
This commit is contained in:
		
							parent
							
								
									9c95528e76
								
							
						
					
					
						commit
						64e7a253f2
					
				
					 6 changed files with 418 additions and 9 deletions
				
			
		
							
								
								
									
										38
									
								
								day3/day3.py
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										38
									
								
								day3/day3.py
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -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))
 | 
			
		||||
							
								
								
									
										11
									
								
								day3/example1
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								day3/example1
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
..##.......
 | 
			
		||||
#...#...#..
 | 
			
		||||
.#....#..#.
 | 
			
		||||
..#.#...#.#
 | 
			
		||||
.#...##..#.
 | 
			
		||||
..#.##.....
 | 
			
		||||
.#.#.#....#
 | 
			
		||||
.#........#
 | 
			
		||||
#.##...#...
 | 
			
		||||
#...##....#
 | 
			
		||||
.#..#...#.#
 | 
			
		||||
							
								
								
									
										323
									
								
								day3/input
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										323
									
								
								day3/input
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,323 @@
 | 
			
		|||
........#.............#........
 | 
			
		||||
...#....#...#....#.............
 | 
			
		||||
.#..#...#............#.....#..#
 | 
			
		||||
..#......#..##............###..
 | 
			
		||||
..........#......#..#..#.......
 | 
			
		||||
.#..#.......#.........#.#......
 | 
			
		||||
.........#..#....##..#.##....#.
 | 
			
		||||
..#....##...#..................
 | 
			
		||||
##..........#.##...#....##..#..
 | 
			
		||||
...#....#...#..............#...
 | 
			
		||||
...........................#..#
 | 
			
		||||
..##.##.#..................#...
 | 
			
		||||
...#.##..#............#........
 | 
			
		||||
........#.......#...#.....##.#.
 | 
			
		||||
.##..........#......#.......#..
 | 
			
		||||
...#..........#...#..#.......#.
 | 
			
		||||
......#...#...#.##.......#.#...
 | 
			
		||||
........#...#...#...##.........
 | 
			
		||||
#..............#.#....#.......#
 | 
			
		||||
..#..#..#.#....#...............
 | 
			
		||||
.....#........#...#..........#.
 | 
			
		||||
##......#...#..#.##.......#....
 | 
			
		||||
..#.#.....#.#.............#.#.#
 | 
			
		||||
#..#..##......##...#...........
 | 
			
		||||
..#......#........#.....#......
 | 
			
		||||
.....#.......#....#.#...#......
 | 
			
		||||
...#........#...........#...#..
 | 
			
		||||
.......#.#...........###....#..
 | 
			
		||||
...#...........##....##........
 | 
			
		||||
#....#..####....#.....#..#....#
 | 
			
		||||
..........#...........#........
 | 
			
		||||
...#.......#....#.#.........#..
 | 
			
		||||
....#...#.......#..###.........
 | 
			
		||||
......#......#..#......#..#....
 | 
			
		||||
...#.....#............#..#.....
 | 
			
		||||
...#.#.#.#..#.......#.....#....
 | 
			
		||||
#....##...#.........#...##.....
 | 
			
		||||
#..#.......#..#..#..#...##.....
 | 
			
		||||
#.......#............#.....#...
 | 
			
		||||
.#........##....##...#........#
 | 
			
		||||
.....#...#.....................
 | 
			
		||||
.......#........#..............
 | 
			
		||||
.....#............#.#.#...#.#..
 | 
			
		||||
.....##..#.............#.......
 | 
			
		||||
..#.##..#........#..#...#......
 | 
			
		||||
.........#.#....#...........#..
 | 
			
		||||
.#.....#..#....#.....#...#.....
 | 
			
		||||
....#.#................#.......
 | 
			
		||||
...............##......#...#...
 | 
			
		||||
.##...#...#.......##.#....#....
 | 
			
		||||
............#........#.......#.
 | 
			
		||||
......##.#.#...................
 | 
			
		||||
.#.#..............#.......#....
 | 
			
		||||
#.....#...#.......#..#...#.....
 | 
			
		||||
.............#....#..#......#..
 | 
			
		||||
........#...##................#
 | 
			
		||||
.......#...#..#..##............
 | 
			
		||||
..#..#...##...#..#.#.....#...#.
 | 
			
		||||
.#.#...#.........#.#...........
 | 
			
		||||
...###....#.......#...#........
 | 
			
		||||
........#......##.#...#..##..#.
 | 
			
		||||
.....................#.#.......
 | 
			
		||||
.............#...........#...#.
 | 
			
		||||
#..#..#.....#.#...#............
 | 
			
		||||
...#....#.....#...........#....
 | 
			
		||||
..##.....##...#......#..##.....
 | 
			
		||||
#.....#.....###.#.....#....##..
 | 
			
		||||
.#...........###...............
 | 
			
		||||
..................#..##.#...#..
 | 
			
		||||
................#....##.#......
 | 
			
		||||
.#.#.#...#....#.........#..#.#.
 | 
			
		||||
#.......#........##............
 | 
			
		||||
.......##.#....#.#............#
 | 
			
		||||
..........#..##.#....#.........
 | 
			
		||||
........##..#....#.............
 | 
			
		||||
.........#....#...........##...
 | 
			
		||||
#.........#.#..#..#..........#.
 | 
			
		||||
.....#........#......#.........
 | 
			
		||||
....#.#.#...............#......
 | 
			
		||||
.#..#..##...#.##..........#....
 | 
			
		||||
..#....................#.#.....
 | 
			
		||||
.........#....#...........#.#.#
 | 
			
		||||
........#....##.##.............
 | 
			
		||||
..#.....#.......#..#......#....
 | 
			
		||||
#..........#.#.....#.#....#....
 | 
			
		||||
........##.#.....#..#.....#.#..
 | 
			
		||||
...................#...#....#.#
 | 
			
		||||
............#..#....#...#...#..
 | 
			
		||||
..............#.#.........#....
 | 
			
		||||
...#..#..#.#..##..##...........
 | 
			
		||||
.#...........................#.
 | 
			
		||||
.#.......#...........#....#.#.#
 | 
			
		||||
......#..#...#........#...##...
 | 
			
		||||
.........#......#.#.......#...#
 | 
			
		||||
...#..##................#......
 | 
			
		||||
.............#.#..##....#.#....
 | 
			
		||||
...............#..#......#.....
 | 
			
		||||
.#......#.#.#....#........#....
 | 
			
		||||
........#..#.##..#..#.........#
 | 
			
		||||
...#....#.#...#..#.......#..#..
 | 
			
		||||
..#...##.........#..#...#......
 | 
			
		||||
...#...........#.............#.
 | 
			
		||||
....#.....................#....
 | 
			
		||||
.....#..#...............#.#...#
 | 
			
		||||
....#..........#........#......
 | 
			
		||||
..#....#........##..##.........
 | 
			
		||||
...#....#..#.#.......#...#.....
 | 
			
		||||
..#........#....#...##....#.#..
 | 
			
		||||
.#...#........##.....#....###..
 | 
			
		||||
#....#....##......#........#...
 | 
			
		||||
.........#..#.#..........#....#
 | 
			
		||||
....#...#.....#.......##.......
 | 
			
		||||
..............#..........#.##..
 | 
			
		||||
#...#..#..............#......#.
 | 
			
		||||
.................#......##....#
 | 
			
		||||
..#..##..#.......#..#.#......#.
 | 
			
		||||
.............#........#.....#.#
 | 
			
		||||
.#.##............#..#..........
 | 
			
		||||
..#...#...........#..##........
 | 
			
		||||
.#....#...#....#.......#.......
 | 
			
		||||
...#.#..#..#..#....#.....#..#..
 | 
			
		||||
....#..##..............#...#...
 | 
			
		||||
#..........###......###........
 | 
			
		||||
.##.##......#..#............#..
 | 
			
		||||
.#...........#.#.....#...#.....
 | 
			
		||||
#.#..#...#............#........
 | 
			
		||||
.........#...#...#..........##.
 | 
			
		||||
.......###..#..........#.......
 | 
			
		||||
...........###.....#........#..
 | 
			
		||||
.#.............#.....#......#..
 | 
			
		||||
...#.....#....#.#.........##...
 | 
			
		||||
....##..##...#.......##........
 | 
			
		||||
......#....##.........#......#.
 | 
			
		||||
..........#.....##..#.....#..#.
 | 
			
		||||
..........####...#..#.........#
 | 
			
		||||
.##....#..#.#...#.......#......
 | 
			
		||||
...#.#.##.#.#...#....#.#.#.....
 | 
			
		||||
.........#...##........##.....#
 | 
			
		||||
..#........#..........##...##.#
 | 
			
		||||
##...##..........#.#...........
 | 
			
		||||
..............#......#.........
 | 
			
		||||
........#.....#.#.......#......
 | 
			
		||||
.#...#.....#....#.#..#.........
 | 
			
		||||
.....#....................##...
 | 
			
		||||
....#..................#.#...##
 | 
			
		||||
.....#............#..##........
 | 
			
		||||
#..........#....#.#.......##.#.
 | 
			
		||||
....#..#.....................#.
 | 
			
		||||
#..#....##.....#...............
 | 
			
		||||
..#...#..#..##....#.#..........
 | 
			
		||||
.......#......#.#.......#.....#
 | 
			
		||||
...#.#.......#...#.##..........
 | 
			
		||||
....#..........#....#.#.#......
 | 
			
		||||
.......#..#..........#..##.....
 | 
			
		||||
#......#......#...#......#...#.
 | 
			
		||||
###..#....##......##........#..
 | 
			
		||||
.#..........#.....#.......#.#..
 | 
			
		||||
.......#.....#.....#.#.........
 | 
			
		||||
..#...#....#...................
 | 
			
		||||
..............#.##.............
 | 
			
		||||
.#...#.......#.##...#.#.......#
 | 
			
		||||
.......#......................#
 | 
			
		||||
....#.#...#.#........#.........
 | 
			
		||||
.#......#....#...#.............
 | 
			
		||||
#.......#...###.....#.#.#..#...
 | 
			
		||||
#....##.#...............##.....
 | 
			
		||||
..#.......#..................#.
 | 
			
		||||
.....####...............#......
 | 
			
		||||
.##......#......#.#.......##.#.
 | 
			
		||||
#......##..###....#....#......#
 | 
			
		||||
.##.......##.##...#.##.........
 | 
			
		||||
......##............#.......#..
 | 
			
		||||
......#..#.....##.#............
 | 
			
		||||
.#..........#.....##...........
 | 
			
		||||
#.........#......#......##.#...
 | 
			
		||||
.........#.......#..#......#.#.
 | 
			
		||||
.........#.......#...........#.
 | 
			
		||||
.#..##.#..................##...
 | 
			
		||||
.............#.............#...
 | 
			
		||||
.....##........#......##...##..
 | 
			
		||||
..#..#.#.....#..#....#.........
 | 
			
		||||
.....#....#.....#.....#........
 | 
			
		||||
#......##.....#....#....#......
 | 
			
		||||
#.................#..#.#......#
 | 
			
		||||
.......#..#......#....#.#...#.#
 | 
			
		||||
....#.........#..#..........#.#
 | 
			
		||||
##......#............#...#...#.
 | 
			
		||||
....##......#...#.....#....##..
 | 
			
		||||
.#...##.........#..............
 | 
			
		||||
......#.....................#..
 | 
			
		||||
..#..........###....#..........
 | 
			
		||||
#....#...#..#.............#....
 | 
			
		||||
#........#.#......#....#.......
 | 
			
		||||
.#...#.......#..#...#.#...#..#.
 | 
			
		||||
................##.#.....#.....
 | 
			
		||||
###.......#...#................
 | 
			
		||||
...#.......#...#.#.....#.......
 | 
			
		||||
..#.........#.....#.#.......#..
 | 
			
		||||
......#.......................#
 | 
			
		||||
#.....#.#..#....#.......#......
 | 
			
		||||
...#....#..#....####...........
 | 
			
		||||
.............#.....#...##......
 | 
			
		||||
.......#.........#...#..#......
 | 
			
		||||
.##..#.........#....#.#........
 | 
			
		||||
....##...#.#...........#....#..
 | 
			
		||||
.........................##....
 | 
			
		||||
..###.......##....#.#.........#
 | 
			
		||||
.#....#.#.#...........##....#..
 | 
			
		||||
......#...#..#..#..#..#.......#
 | 
			
		||||
..#....#.#.......#..#..#..#...#
 | 
			
		||||
.....##...#.##....#.#...#......
 | 
			
		||||
.........#..#....#..#..........
 | 
			
		||||
.##..##.........#.#.....#......
 | 
			
		||||
..........#...##...#.#...#.....
 | 
			
		||||
#.##..#..#.............#.......
 | 
			
		||||
...#...........#.......#......#
 | 
			
		||||
.......#....#....#...##.......#
 | 
			
		||||
..#.##........###..#......#....
 | 
			
		||||
...#...........###......#..#..#
 | 
			
		||||
.#.........#.#.........#.#.....
 | 
			
		||||
##.......##.##.##......##......
 | 
			
		||||
............#...#..........#...
 | 
			
		||||
....................#..........
 | 
			
		||||
...#..#...........#...#...#....
 | 
			
		||||
.................#...#......###
 | 
			
		||||
...#................#.#.##.....
 | 
			
		||||
...............#........#......
 | 
			
		||||
#.............##......#.#..#...
 | 
			
		||||
..#.#.....#..#.##.....##...#...
 | 
			
		||||
......#.........#......#.......
 | 
			
		||||
#.......#......#....#........#.
 | 
			
		||||
.#..##.....#.........#.........
 | 
			
		||||
....##.##.#...#.........##.#...
 | 
			
		||||
...............#..#..#..##.....
 | 
			
		||||
.#..#...............###........
 | 
			
		||||
.##............##..............
 | 
			
		||||
...............#...##...#...#.#
 | 
			
		||||
..#.#......#.#..#.............#
 | 
			
		||||
#.#..#..##.........#.#.#...#...
 | 
			
		||||
....##.#....................##.
 | 
			
		||||
.........#..#.....#.....#..#..#
 | 
			
		||||
....#......#......#.##....#....
 | 
			
		||||
........###..#.............#..#
 | 
			
		||||
##................#.........#..
 | 
			
		||||
#.....#.......#....#...........
 | 
			
		||||
..#.......#..#........#....#...
 | 
			
		||||
..#.#.##..#.#...##........#.##.
 | 
			
		||||
..#..........#............#....
 | 
			
		||||
..........#...............##...
 | 
			
		||||
..........###........#.#.......
 | 
			
		||||
.....###..#.............#......
 | 
			
		||||
##.............#...#.....#.....
 | 
			
		||||
.....#......#....#........#.#..
 | 
			
		||||
............#..#..............#
 | 
			
		||||
.................#...........##
 | 
			
		||||
#........#.........###.....#...
 | 
			
		||||
..#.#..............##......#.#.
 | 
			
		||||
.#...........#.........#..##..#
 | 
			
		||||
...............................
 | 
			
		||||
.#.....#..#....#....#......#...
 | 
			
		||||
.#...#......#.#..#....#.......#
 | 
			
		||||
......#.##.......#......#......
 | 
			
		||||
......#..###..#................
 | 
			
		||||
#..#.....#........##...#.......
 | 
			
		||||
......##.........##....#...##..
 | 
			
		||||
.#..........#.................#
 | 
			
		||||
#..#.......#...............#...
 | 
			
		||||
.........#..###....#.#.##.#....
 | 
			
		||||
..#...#.##..##...............##
 | 
			
		||||
.........#.....................
 | 
			
		||||
.#....##...#......#....#.......
 | 
			
		||||
............#..........#..#....
 | 
			
		||||
...#......##....#....#........#
 | 
			
		||||
.#...................#.........
 | 
			
		||||
#.#........###....#..........#.
 | 
			
		||||
.........#....#....#........##.
 | 
			
		||||
.#....#..#.........#..#........
 | 
			
		||||
...............#..#...#..#...##
 | 
			
		||||
.........#....##....#......#...
 | 
			
		||||
.#.............................
 | 
			
		||||
...#........#...#.#...#.#..#...
 | 
			
		||||
.....#..##...#.#...............
 | 
			
		||||
#.....#....#.........#.........
 | 
			
		||||
#...#...........##.........#...
 | 
			
		||||
..##........#.#...#...#......#.
 | 
			
		||||
...........#.....#...#.#.......
 | 
			
		||||
......###....#.....#...........
 | 
			
		||||
......##...#..........#....#.#.
 | 
			
		||||
.......##..##..........#.......
 | 
			
		||||
....#............#..#....##....
 | 
			
		||||
..##...................#.#.....
 | 
			
		||||
...#.#..#.#....................
 | 
			
		||||
.#..##..#............##.###..#.
 | 
			
		||||
#.#...#....#.#..........#.#....
 | 
			
		||||
........#....#.....#...........
 | 
			
		||||
..##....#...#.......#..........
 | 
			
		||||
...........##.##....#..........
 | 
			
		||||
.....#............#............
 | 
			
		||||
.......#.............#....#....
 | 
			
		||||
.................#......#......
 | 
			
		||||
......##.......#....#..##...#..
 | 
			
		||||
.#..#....#.....................
 | 
			
		||||
...#.#.#...#......##...........
 | 
			
		||||
##........##.#....#....#.......
 | 
			
		||||
.......#.....#..#..#...#.##....
 | 
			
		||||
#..........#....#.#..#..#..#...
 | 
			
		||||
...##..............#...........
 | 
			
		||||
.........#.....#.#....#.......#
 | 
			
		||||
.........#....##..#..##..#.....
 | 
			
		||||
.....#......................#..
 | 
			
		||||
...###...#..#......#...........
 | 
			
		||||
....#.....................#....
 | 
			
		||||
...............................
 | 
			
		||||
..#.....###.......#..#....#....
 | 
			
		||||
#..........#.................#.
 | 
			
		||||
......#.......###.......#..##..
 | 
			
		||||
.............#.##..............
 | 
			
		||||
......#..#.#..#...........#....
 | 
			
		||||
...#....##.#...#..#.#...#....#.
 | 
			
		||||
..................#...#....#.##
 | 
			
		||||
......#.#....#.................
 | 
			
		||||
......#.#.....#.....#..##......
 | 
			
		||||
#..##...........#..#.....#.##..
 | 
			
		||||
							
								
								
									
										43
									
								
								day3/test_day3.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								day3/test_day3.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue