From f4a6aae5501c370cff775378233daf97a07bc2e5 Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 3 Dec 2024 11:50:13 +0000 Subject: [PATCH] Add Python day 1, 2, and 3 --- main.py | 6 ++++++ src/day_01/part_1.py | 14 ++++++++++++++ src/day_01/part_2.py | 24 ++++++++++++++++++++++++ src/day_02/part_1.py | 23 +++++++++++++++++++++++ src/day_02/part_2.py | 32 ++++++++++++++++++++++++++++++++ src/day_03/part_1.py | 9 +++++++++ src/day_03/part_2.py | 24 ++++++++++++++++++++++++ 7 files changed, 132 insertions(+) create mode 100644 main.py create mode 100644 src/day_01/part_1.py create mode 100644 src/day_01/part_2.py create mode 100644 src/day_02/part_1.py create mode 100644 src/day_02/part_2.py create mode 100644 src/day_03/part_1.py create mode 100644 src/day_03/part_2.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..1c5a83b --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +import src.day_01.part_1 +import src.day_01.part_2 +import src.day_02.part_1 +import src.day_02.part_2 +import src.day_03.part_1 +import src.day_03.part_2 diff --git a/src/day_01/part_1.py b/src/day_01/part_1.py new file mode 100644 index 0000000..06c6d8f --- /dev/null +++ b/src/day_01/part_1.py @@ -0,0 +1,14 @@ +lines = [ line.strip().split(" ") for line in open('inputs/01.txt') ] + +left = [ int(line[0]) for line in lines ] +right = [ int(line[1]) for line in lines ] + +left.sort() +right.sort() + +s = 0 + +for l, r in zip(left, right): + s += abs(l - r) + +print(f"Day 01 part 1: {s}") \ No newline at end of file diff --git a/src/day_01/part_2.py b/src/day_01/part_2.py new file mode 100644 index 0000000..f2f44d3 --- /dev/null +++ b/src/day_01/part_2.py @@ -0,0 +1,24 @@ +lines = [ line.strip().split(" ") for line in open('inputs/01.txt') ] + +left = [ int(line[0]) for line in lines ] +right = [ int(line[1]) for line in lines ] + +left.sort() +right.sort() + +s = 0 + +i, j = 0, 0 + +while i < len(left) and j < len(right): + l, r = left[i], right[j] + + if l < r: + i += 1 + elif l > r: + j += 1 + else: + j += 1 + s += l + +print(f"Day 01 part 2: {s}") \ No newline at end of file diff --git a/src/day_02/part_1.py b/src/day_02/part_1.py new file mode 100644 index 0000000..f0aca82 --- /dev/null +++ b/src/day_02/part_1.py @@ -0,0 +1,23 @@ +import itertools + +lines = [ [ int(l) for l in line.strip().split(" ") ] for line in open('inputs/02.txt') ] + +s = 0 + +for line in lines: + increasing, decreasing, max_diff = 0, 0, 0 + + for a, b in itertools.pairwise(line): + max_diff = max(max_diff, abs(a - b)) + + if a < b: + increasing += 1 + elif a > b: + decreasing += 1 + + single_direction = (increasing * decreasing == 0) and (increasing + decreasing == len(line) - 1) + + if single_direction and max_diff <= 3: + s += 1 + +print(f"Day 02 part 1: {s}") \ No newline at end of file diff --git a/src/day_02/part_2.py b/src/day_02/part_2.py new file mode 100644 index 0000000..fdaa818 --- /dev/null +++ b/src/day_02/part_2.py @@ -0,0 +1,32 @@ +import itertools + +def is_safe_line(line) -> bool: + increasing, decreasing, max_diff = 0, 0, 0 + + for a, b in itertools.pairwise(line): + max_diff = max(max_diff, abs(a - b)) + + if a < b: + increasing += 1 + elif a > b: + decreasing += 1 + + single_direction = (increasing * decreasing == 0) and (increasing + decreasing == len(line) - 1) + + return single_direction and max_diff <= 3 + + +lines = [ [ int(l) for l in line.strip().split(" ") ] for line in open('inputs/02.txt') ] + +s = 0 + +for line in lines: + if is_safe_line(line): + s += 1 + else: + for i in range(len(line)): + if is_safe_line(line[:i] + line[i+1:]): + s += 1 + break + +print(f"Day 02 part 2: {s}") \ No newline at end of file diff --git a/src/day_03/part_1.py b/src/day_03/part_1.py new file mode 100644 index 0000000..9bbbe86 --- /dev/null +++ b/src/day_03/part_1.py @@ -0,0 +1,9 @@ +import re + +lines = "".join(line for line in open('inputs/03.txt')) +total = 0 + +for a, b in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)", lines): + total += int(a) * int(b) + +print(f"Day 03 part 1: {total}") \ No newline at end of file diff --git a/src/day_03/part_2.py b/src/day_03/part_2.py new file mode 100644 index 0000000..bd284cc --- /dev/null +++ b/src/day_03/part_2.py @@ -0,0 +1,24 @@ +import re + +def find_muls(text : str) -> int: + tot = 0 + for a, b in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)", text): + tot += int(a) * int(b) + return tot + +lines = "".join(line for line in open('inputs/03.txt')) +total = 0 + +if "don't()" in lines: + parts = lines.split("don't()") + + # By default, first is valid + total += find_muls(parts[0]) + + for part in parts[1:]: + for valid_part in part.split("do()")[1:]: + total += find_muls(valid_part) +else: + total = find_muls(lines) + +print(f"Day 03 part 2: {total}")