diff --git a/day3/common.py b/day3/common.py new file mode 100644 index 0000000..88dc257 --- /dev/null +++ b/day3/common.py @@ -0,0 +1,3 @@ +def parse(filename: str) -> str: + with open(filename) as f: + return f.read() diff --git a/day3/part1.py b/day3/part1.py new file mode 100755 index 0000000..69b2cf0 --- /dev/null +++ b/day3/part1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +import re + +from common import parse + + +def solve(input: str) -> int: + regex = r"mul\((\d+),(\d+)\)" + muls = re.findall(regex, input) + return sum(map(lambda x: int(x[0]) * int(x[1]), muls)) + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main() diff --git a/day3/part2.py b/day3/part2.py new file mode 100755 index 0000000..d57c6b7 --- /dev/null +++ b/day3/part2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +import re + +from common import parse + + +def solve(input: str, debug=False) -> int: + regex = r"(mul\((\d+),(\d+)\)|do\(\)|don't\(\))" + prog = re.findall(regex, input) + the_sum = 0 + enabled = True + for stance in prog: + if debug: + print(stance[0], end=": ") + if enabled and stance[0].startswith("mul"): + if debug: + print("multiplying") + the_sum += int(stance[1]) * int(stance[2]) + elif enabled and stance[0] == "don't()": + if debug: + print("disabling") + enabled = False + elif not enabled and stance[0] == "do()": + if debug: + print("enabling") + enabled = True + else: + if debug: + print("noop") + return the_sum + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main()