# HG changeset patch # User Teemu Piippo # Date 1606949978 -7200 # Node ID 6bf338377800ed49f7eb3c3d729253c52846ef15 advent of code diff -r 000000000000 -r 6bf338377800 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Thu Dec 03 00:59:38 2020 +0200 @@ -0,0 +1,2 @@ +syntax:glob +input* diff -r 000000000000 -r 6bf338377800 advent.py --- /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)