advent.py

changeset 0
6bf338377800
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/advent.py	Thu Dec 03 00:59:38 2020 +0200
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+def puzzle_1():
+	import sys, itertools
+	nums = map(int, sys.stdin)
+	for num in {
+		a * b
+		for a, b in itertools.permutations(nums, 2)
+		if a + b == 2020
+	}:
+		print(num)
+
+def puzzle_2():
+	import sys, itertools
+	nums = map(int, sys.stdin)
+	for num in {
+		a * b * c
+		for a, b, c in itertools.permutations(nums, 3)
+		if a + b + c == 2020
+	}:
+		print(num)
+
+def puzzle_3():
+	import re, sys
+	def is_valid(line):
+		match = re.search(r'^(\d+)-(\d+) (.): (.+)$', line)
+		req = range(int(match.group(1)), int(match.group(2)) + 1)
+		char, string = match.group(3), match.group(4)
+		return str.count(string, char) in req
+	print(sum(map(is_valid, sys.stdin)))
+
+def puzzle_4():
+	import re, sys
+	def is_valid(line):
+		match = re.search(r'^(\d+)-(\d+) (.): (.+)$', line)
+		a, b = int(match.group(1)) - 1, int(match.group(2)) - 1
+		char, string = match.group(3), match.group(4)
+		return bool(string[a] == char) ^ bool(string[b] == char)
+	print(sum(map(is_valid, sys.stdin)))
+
+import argparse
+parser = argparse.ArgumentParser()
+parser.add_argument('num')
+parser.add_argument('args', nargs = '*')
+args = parser.parse_args()
+fun = globals()['puzzle_' + args.num]
+fun(*args.args)

mercurial