From 7e7d773e3a58d1755baab74d7a5abab69febb26a Mon Sep 17 00:00:00 2001 From: Fedaya Date: Tue, 3 Dec 2024 06:54:39 +0100 Subject: [PATCH] Day 3 completed --- day3/common.py | 3 +++ day3/part1.py | 18 ++++++++++++++++++ day3/part2.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 day3/common.py create mode 100755 day3/part1.py create mode 100755 day3/part2.py 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()