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

#!/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