Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions advent_of_code/2025(python)/day2.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions advent_of_code/2025(python)/test_day2.py
Original file line number Diff line number Diff line change
@@ -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()
Loading