Thu, 03 Dec 2020 00:59:38 +0200
advent of code
0 | 1 | #!/usr/bin/env python3 |
2 | def puzzle_1(): | |
3 | import sys, itertools | |
4 | nums = map(int, sys.stdin) | |
5 | for num in { | |
6 | a * b | |
7 | for a, b in itertools.permutations(nums, 2) | |
8 | if a + b == 2020 | |
9 | }: | |
10 | print(num) | |
11 | ||
12 | def puzzle_2(): | |
13 | import sys, itertools | |
14 | nums = map(int, sys.stdin) | |
15 | for num in { | |
16 | a * b * c | |
17 | for a, b, c in itertools.permutations(nums, 3) | |
18 | if a + b + c == 2020 | |
19 | }: | |
20 | print(num) | |
21 | ||
22 | def puzzle_3(): | |
23 | import re, sys | |
24 | def is_valid(line): | |
25 | match = re.search(r'^(\d+)-(\d+) (.): (.+)$', line) | |
26 | req = range(int(match.group(1)), int(match.group(2)) + 1) | |
27 | char, string = match.group(3), match.group(4) | |
28 | return str.count(string, char) in req | |
29 | print(sum(map(is_valid, sys.stdin))) | |
30 | ||
31 | def puzzle_4(): | |
32 | import re, sys | |
33 | def is_valid(line): | |
34 | match = re.search(r'^(\d+)-(\d+) (.): (.+)$', line) | |
35 | a, b = int(match.group(1)) - 1, int(match.group(2)) - 1 | |
36 | char, string = match.group(3), match.group(4) | |
37 | return bool(string[a] == char) ^ bool(string[b] == char) | |
38 | print(sum(map(is_valid, sys.stdin))) | |
39 | ||
40 | import argparse | |
41 | parser = argparse.ArgumentParser() | |
42 | parser.add_argument('num') | |
43 | parser.add_argument('args', nargs = '*') | |
44 | args = parser.parse_args() | |
45 | fun = globals()['puzzle_' + args.num] | |
46 | fun(*args.args) |