advent.py

Thu, 03 Dec 2020 00:59:38 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 03 Dec 2020 00:59:38 +0200
changeset 0
6bf338377800
permissions
-rwxr-xr-x

advent of code

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

mercurial