Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advent of Code 2024 - Day 2 : Python version #41

Merged
merged 2 commits into from
Dec 2, 2024
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
80 changes: 80 additions & 0 deletions advent_of_code/2024(python)/day2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

def _is_single_record_safe(record:list[int]) -> bool:

if not 1 <= abs(record[0] - record[1]) <= 3:
return False
is_increasing = record[0] < record[1]

for i in range(1, len(record)-1):
diff = record[i] - record[i+1]
if (is_increasing and diff>0) or (not is_increasing and diff<0):
return False
if not 1 <= abs(diff) <= 3:
return False

return True

def _is_single_record_wrong_version(record:list[int]) -> bool:
if not 1 <= abs(record[0] - record[1]) <= 3:
# so we can remove either first or second
remove_1st_item_record = record[1:]
remove_2nd_item_record = record[:1] + record[2:]
return _is_single_record_safe(remove_1st_item_record) or _is_single_record_safe(remove_2nd_item_record)

is_increasing = record[0] < record[1]

for i in range(1, len(record)-1):
diff = record[i] - record[i+1]
if (is_increasing and diff>0) or (not is_increasing and diff<0) or (not 1 <= abs(diff) <= 3):
# either remove ith or (i+1)th item
remove_ith_item_record = record[:i] + record[i+1:]
remove_i1th_item_record = record[:i+1] + record[i+2:]
return _is_single_record_safe(remove_ith_item_record) or _is_single_record_safe(remove_i1th_item_record)

return True

def _is_single_record_safe_right_version(record:list[int]) -> bool:
if not 1 <= abs(record[0] - record[1]) <= 3:
# so we can remove either first or second
remove_1st_item_record = record[1:]
remove_2nd_item_record = record[:1] + record[2:]
return _is_single_record_safe(remove_1st_item_record) or _is_single_record_safe(remove_2nd_item_record)

is_increasing = record[0] < record[1]

for i in range(1, len(record)-1):
diff = record[i] - record[i+1]
if (is_increasing and diff>0) or (not is_increasing and diff<0) or (not 1 <= abs(diff) <= 3):
# try brutefocelly iterating all items
is_safe = False
for i in range(len(record)):
is_safe = is_safe or _is_single_record_safe(record[:i] + record[i+1:])

return is_safe

return True


def solve_day2_part1(inp:list[str]) -> int:

res = 0
for report in inp:
record = [int(item) for item in report.split()]

res += int(_is_single_record_safe(record))

return res

def solve_day2_part2(inp:list[str]) -> int:

res = 0
for report in inp:
record = [int(item) for item in report.split()]
# Uses `_is_single_record_wrong_version` previously
# Get answer 530, while expected answer is 531
# Wrong version can't solve `65 66 63 60`
# it will be valid when removing first item : 66 63 60
# but previous version will only try : 65 66 60 and 66 63 60
res += int(_is_single_record_safe_right_version(record))

return res
24 changes: 24 additions & 0 deletions advent_of_code/2024(python)/test_day2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest

from day2 import solve_day2_part1, solve_day2_part2

question2_input = [
"7 6 4 2 1",
"1 2 7 8 9",
"9 7 6 2 1",
"1 3 2 4 5",
"8 6 4 4 1",
"1 3 6 7 9",
]


class TestDay2(unittest.TestCase):

def test_part1(self):
self.assertEqual(solve_day2_part1(question2_input), 2)

def test_part2(self):
self.assertEqual(solve_day2_part2(question2_input), 4)

if __name__ == "__main__":
unittest.main()
Loading