From 0321264beafc6606184ecbe4de0294945a7ad5e3 Mon Sep 17 00:00:00 2001 From: Allianzcortex Date: Fri, 5 Dec 2025 08:38:47 -0500 Subject: [PATCH 1/2] update solution --- advent_of_code/2025(python)/day5.py | 60 ++++++++++++++++++++++++ advent_of_code/2025(python)/test_day5.py | 30 ++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 advent_of_code/2025(python)/day5.py create mode 100644 advent_of_code/2025(python)/test_day5.py diff --git a/advent_of_code/2025(python)/day5.py b/advent_of_code/2025(python)/day5.py new file mode 100644 index 0000000..3d5df7c --- /dev/null +++ b/advent_of_code/2025(python)/day5.py @@ -0,0 +1,60 @@ + +def solve_day5_part1(input_): + + ranges = [] + cnt = 0 + + for line in input_: + if line.strip() == "": + merged_ranges = _merge_intervals(ranges) + continue + if "-" in line: + start, end = map(int, line.split("-")) + # Process range + ranges.append([start, end]) + else: + number = int(line) + # Process ingredients + for range in merged_ranges: + if range[0] <= number <= range[1]: + cnt += 1 + + return cnt + + +def _merge_intervals(intervals): + if not intervals: + return [] + + intervals.sort(key=lambda x: x[0]) + res = [intervals[0]] + prev_end = intervals[0][1] + for current in intervals[1:]: + # no overlap + if current[0] > prev_end: + res.append(current) + prev_end = current[1] + else: + res[-1][1] = max(prev_end, current[1]) + prev_end = res[-1][1] + return res + + +def solve_day5_part2(input_): + ranges = [] + cnt = 0 + + for line in input_: + + cnt = 0 + if "-" in line: + start, end = map(int, line.split("-")) + # Process range + ranges.append([start, end]) + + if line.strip() == "": + merged_ranges = _merge_intervals(ranges) + for range in merged_ranges: + cnt += (range[1]-range[0]+1) + + return cnt diff --git a/advent_of_code/2025(python)/test_day5.py b/advent_of_code/2025(python)/test_day5.py new file mode 100644 index 0000000..cdbe7ee --- /dev/null +++ b/advent_of_code/2025(python)/test_day5.py @@ -0,0 +1,30 @@ +import unittest + +from day2 import solve_day5_part1, solve_day5_part2 + +question_input = [ + "3-5", + "10-14", + "16-20", + "12-18", + "", + "1", + "5", + "8", + "11", + "17", + "32", +] + + +class TestSample(unittest.TestCase): + + def test_part1(self): + self.assertEqual(solve_day5_part1(question_input), 3) + + def test_part2(self): + self.assertEqual(solve_day5_part2(question_input), 14) + + +if __name__ == "__main__": + unittest.main() From 6d0f24672d0ee7f422cb4cb635825a2a0fac2851 Mon Sep 17 00:00:00 2001 From: Allianzcortex Date: Fri, 5 Dec 2025 08:41:36 -0500 Subject: [PATCH 2/2] fix test --- advent_of_code/2025(python)/day5.py | 1 + advent_of_code/2025(python)/test_day5.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/advent_of_code/2025(python)/day5.py b/advent_of_code/2025(python)/day5.py index 3d5df7c..21d6b57 100644 --- a/advent_of_code/2025(python)/day5.py +++ b/advent_of_code/2025(python)/day5.py @@ -35,6 +35,7 @@ def _merge_intervals(intervals): res.append(current) prev_end = current[1] else: + # with overlap, choose maximum end as new end res[-1][1] = max(prev_end, current[1]) prev_end = res[-1][1] return res diff --git a/advent_of_code/2025(python)/test_day5.py b/advent_of_code/2025(python)/test_day5.py index cdbe7ee..0af8151 100644 --- a/advent_of_code/2025(python)/test_day5.py +++ b/advent_of_code/2025(python)/test_day5.py @@ -1,6 +1,6 @@ import unittest -from day2 import solve_day5_part1, solve_day5_part2 +from day5 import solve_day5_part1, solve_day5_part2 question_input = [ "3-5",