-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2020_02.py
executable file
·70 lines (51 loc) · 1.61 KB
/
AoC2020_02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env python3
#
# Advent of Code 2020 Day 2
#
import sys
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
TEST = """\
1-3 a: abcde
2-9 c: ccccccccc
1-3 b: cdefg
"""
Input = list[tuple[int, int, str, str]]
Output1 = int
Output2 = int
class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
def parse(line: str) -> tuple[int, int, str, str]:
splits = line.split(": ")
left_and_right = splits[0].split(" ")
first, second = left_and_right[0].split("-")
wanted = left_and_right[1][0]
password = splits[1]
return (int(first), int(second), wanted, password)
return [parse(line) for line in input_data]
def part_1(self, inputs: Input) -> int:
def check_valid(
first: int, second: int, wanted: str, passw: str
) -> bool:
return first <= passw.count(wanted) <= second
return sum(check_valid(*line) for line in inputs)
def part_2(self, inputs: Input) -> int:
def check_valid(
first: int, second: int, wanted: str, passw: str
) -> bool:
return (passw[first - 1] == wanted) ^ (passw[second - 1] == wanted)
return sum(check_valid(*line) for line in inputs)
@aoc_samples(
(
("part_1", TEST, 2),
("part_2", TEST, 1),
)
)
def samples(self) -> None:
pass
solution = Solution(2020, 2)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()