From 417eb9d2703a605ee4ec3426e6c13e50b3f5fb92 Mon Sep 17 00:00:00 2001 From: Allianzcortex Date: Mon, 2 Dec 2024 07:47:56 -0500 Subject: [PATCH 1/2] update day2 code with part1 --- advent_of_code/2024(python)/day2.py | 64 +++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 advent_of_code/2024(python)/day2.py diff --git a/advent_of_code/2024(python)/day2.py b/advent_of_code/2024(python)/day2.py new file mode 100644 index 0000000..6cdec0f --- /dev/null +++ b/advent_of_code/2024(python)/day2.py @@ -0,0 +1,64 @@ + +def _is_single_record_safe(record:list[int]) -> bool: + print(record) + 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_comprehensive(record:list[int]) -> bool: + if not 1 <= abs(record[0] - record[1]) <= 3: + # so we can remove either first or second + print(record) + remove_1st_item_record = record[1:] + remove_2nd_item_record = record[:1] + record[2:] + print("==fist==") + print(remove_1st_item_record) + print(remove_2nd_item_record) + 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:] + print(record) + print(i) + print("==second==") + print(remove_ith_item_record) + print(remove_i1th_item_record) + return _is_single_record_safe(remove_ith_item_record) or _is_single_record_safe(remove_i1th_item_record) + + return True + + +def solve_day2_part1(inp:list[str]) -> int: + + res = 0 + for report in inp: + record = [int(item) for item in report.split()] + # check first gap + + 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()] + res += int(_is_single_comprehensive(record)) + + return res \ No newline at end of file From 2913255a61163b091b3ac04635b0a8fa59fde2a4 Mon Sep 17 00:00:00 2001 From: Allianzcortex Date: Mon, 2 Dec 2024 08:11:50 -0500 Subject: [PATCH 2/2] fix the issue --- advent_of_code/2024(python)/day2.py | 46 ++++++++++++++++-------- advent_of_code/2024(python)/test_day2.py | 24 +++++++++++++ 2 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 advent_of_code/2024(python)/test_day2.py diff --git a/advent_of_code/2024(python)/day2.py b/advent_of_code/2024(python)/day2.py index 6cdec0f..31bf6e8 100644 --- a/advent_of_code/2024(python)/day2.py +++ b/advent_of_code/2024(python)/day2.py @@ -1,6 +1,6 @@ def _is_single_record_safe(record:list[int]) -> bool: - print(record) + if not 1 <= abs(record[0] - record[1]) <= 3: return False is_increasing = record[0] < record[1] @@ -14,17 +14,13 @@ def _is_single_record_safe(record:list[int]) -> bool: return True -def _is_single_comprehensive(record:list[int]) -> bool: +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 - print(record) remove_1st_item_record = record[1:] remove_2nd_item_record = record[:1] + record[2:] - print("==fist==") - print(remove_1st_item_record) - print(remove_2nd_item_record) 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): @@ -33,13 +29,29 @@ def _is_single_comprehensive(record:list[int]) -> bool: # 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:] - print(record) - print(i) - print("==second==") - print(remove_ith_item_record) - print(remove_i1th_item_record) 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 @@ -48,7 +60,6 @@ def solve_day2_part1(inp:list[str]) -> int: res = 0 for report in inp: record = [int(item) for item in report.split()] - # check first gap res += int(_is_single_record_safe(record)) @@ -59,6 +70,11 @@ def solve_day2_part2(inp:list[str]) -> int: res = 0 for report in inp: record = [int(item) for item in report.split()] - res += int(_is_single_comprehensive(record)) + # 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 \ No newline at end of file diff --git a/advent_of_code/2024(python)/test_day2.py b/advent_of_code/2024(python)/test_day2.py new file mode 100644 index 0000000..d6dd4f4 --- /dev/null +++ b/advent_of_code/2024(python)/test_day2.py @@ -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()