finished day 2
This commit is contained in:
		
							parent
							
								
									db4717556e
								
							
						
					
					
						commit
						78ee9f6ec7
					
				
					 4 changed files with 1089 additions and 0 deletions
				
			
		
							
								
								
									
										49
									
								
								day2/day2.py
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										49
									
								
								day2/day2.py
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,49 @@
 | 
				
			||||||
 | 
					#! /usr/bin/env python3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import pathlib
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					from dataclasses import dataclass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@dataclass
 | 
				
			||||||
 | 
					class PasswordSpec:
 | 
				
			||||||
 | 
					    first:int
 | 
				
			||||||
 | 
					    second:int
 | 
				
			||||||
 | 
					    letter:str
 | 
				
			||||||
 | 
					    password:str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def parse(puzzle_input: str):
 | 
				
			||||||
 | 
					    """Parse input"""
 | 
				
			||||||
 | 
					    regex = re.compile(r'^(\d+)-(\d+) (\w): (\w+)$')
 | 
				
			||||||
 | 
					    toInt = lambda x: PasswordSpec(int(x[0]), int(x[1]), x[2], x[3])
 | 
				
			||||||
 | 
					    return [toInt(regex.match(i).groups()) for i in puzzle_input.splitlines()]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def part1(data):
 | 
				
			||||||
 | 
					    """Solve part 1"""
 | 
				
			||||||
 | 
					    test = lambda x: x.first<=x.password.count(x.letter)<=x.second
 | 
				
			||||||
 | 
					    return sum(1 for p in data if test(p))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_password(passwordSpec: PasswordSpec):
 | 
				
			||||||
 | 
					    if passwordSpec.password[passwordSpec.first-1]==passwordSpec.letter:
 | 
				
			||||||
 | 
					        return passwordSpec.password[passwordSpec.second-1]!=passwordSpec.letter
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        return passwordSpec.password[passwordSpec.second-1]==passwordSpec.letter
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def part2(data):
 | 
				
			||||||
 | 
					    """Solve part 2"""
 | 
				
			||||||
 | 
					    return sum(1 for p in data if test_password(p))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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))
 | 
				
			||||||
							
								
								
									
										3
									
								
								day2/example1
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								day2/example1
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,3 @@
 | 
				
			||||||
 | 
					1-3 a: abcde
 | 
				
			||||||
 | 
					1-3 b: cdefg
 | 
				
			||||||
 | 
					2-9 c: ccccccccc
 | 
				
			||||||
							
								
								
									
										1000
									
								
								day2/input
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1000
									
								
								day2/input
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										37
									
								
								day2/test_day2.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								day2/test_day2.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,37 @@
 | 
				
			||||||
 | 
					#! /usr/bin/env python3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import pathlib
 | 
				
			||||||
 | 
					import pytest
 | 
				
			||||||
 | 
					import day2 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 / "example2").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 == [
 | 
				
			||||||
 | 
					            aoc.PasswordSpec(first=1, second=3, letter='a', password='abcde'),
 | 
				
			||||||
 | 
					            aoc.PasswordSpec(first=1, second=3, letter='b', password='cdefg'),
 | 
				
			||||||
 | 
					            aoc.PasswordSpec(first=2, second=9, letter='c', password='ccccccccc')
 | 
				
			||||||
 | 
					            ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# @pytest.mark.skip(reason="Not implemented")
 | 
				
			||||||
 | 
					def test_part1_example1(example1):
 | 
				
			||||||
 | 
					    """Test part 1 on example input"""
 | 
				
			||||||
 | 
					    assert aoc.part1(example1) == 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# @pytest.mark.skip(reason="Not implemented")
 | 
				
			||||||
 | 
					def test_part2_example2(example1):
 | 
				
			||||||
 | 
					    """Test part 2 on example input"""
 | 
				
			||||||
 | 
					    assert aoc.part2(example1) == 1
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue