From cc4c8f32d5be2ea9973819e7d03c200618f42d12 Mon Sep 17 00:00:00 2001 From: Allianzcortex Date: Tue, 2 Dec 2025 13:36:27 -0500 Subject: [PATCH] update day2 solution --- advent_of_code/2025(python)/day2.py | 61 ++++++++++++++++++++++++ advent_of_code/2025(python)/test_day2.py | 17 +++++++ 2 files changed, 78 insertions(+) create mode 100644 advent_of_code/2025(python)/day2.py create mode 100644 advent_of_code/2025(python)/test_day2.py diff --git a/advent_of_code/2025(python)/day2.py b/advent_of_code/2025(python)/day2.py new file mode 100644 index 0000000..b84931c --- /dev/null +++ b/advent_of_code/2025(python)/day2.py @@ -0,0 +1,61 @@ +def solve_day2_part1_wrong_version(input_): + # only work for test case + res = 0 + for ranges in input_.split(","): + left, right = ranges.split("-") + + invalid_left = invalid_right = "" + if len(left) % 2 == 0: + left_left_half = left[:len(left)//2] + left_right_half = left[len(left)//2:] + if int(left_left_half) >= int(left_right_half): + invalid_left = left_left_half+left_left_half + + if len(right) % 2 == 0: + right_left_half = right[:len(right)//2] + right_right_half = right[len(right)//2:] + if int(right_left_half) <= int(right_right_half) and int(right_left_half) >= int(left_right_half): + invalid_right = right_left_half + right_left_half + if int(left_left_half) > int(right_right_half): + invalid_left = "" + + res += int(invalid_left) if invalid_left != "" else 0 + res += int(invalid_right) if invalid_right != "" and invalid_left != invalid_right else 0 + + return res + + +def solve_day2_part1_right_version(input_): + res = 0 + for ranges in input_.split(","): + left, right = ranges.split("-") + # both are odds + if len(left) % 2 == 1 and len(right) % 2 == 1: + continue + + left_val, right_val = int(left), int(right) + # if left is even + if len(left) % 2 == 0: + left_left_half = int(left[:len(left)//2]) + while True: + next_val = f"{left_left_half}{left_left_half}" + if left_val <= int(next_val) <= right_val: + res += int(next_val) + elif int(next_val) > right_val: + break + + left_left_half += 1 + + # if right is even + elif len(right) % 2 == 0: + right_left_half = int(right[:len(right)//2]) + while True: + next_val = f"{right_left_half}{right_left_half}" + if left_val <= int(next_val) <= right_val: + res += int(next_val) + elif int(next_val) < left_val: + break + + right_left_half -= 1 + + return res diff --git a/advent_of_code/2025(python)/test_day2.py b/advent_of_code/2025(python)/test_day2.py new file mode 100644 index 0000000..308b4f1 --- /dev/null +++ b/advent_of_code/2025(python)/test_day2.py @@ -0,0 +1,17 @@ +import unittest + +from day2 import solve_day2_part1_right_version + +question_input = "11-22,95-115,998-1012,1188511880-1188511890,222220-222224,\ +1698522-1698528,446443-446449,38593856-38593862,565653-565659,\ +824824821-824824827,2121212118-2121212124" + + +class TestSample(unittest.TestCase): + def test_part1(self): + self.assertEqual(solve_day2_part1_right_version( + question_input), 1227775554) + + +if __name__ == "__main__": + unittest.main()