diff --git a/tests/y_2024/test_2024_day10.py b/tests/y_2024/test_2024_day10.py new file mode 100644 index 0000000..63a9ee0 --- /dev/null +++ b/tests/y_2024/test_2024_day10.py @@ -0,0 +1,39 @@ +# from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2024.day10 import Day + +with patch( + "builtins.open", + mock_open( + read_data="""............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ +""", # noqa: E501 + ), +): + day = Day() + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + r = day._calculate_1() + assert r == 14 + + +def test_calculate_2(): + r = day._calculate_2() + assert r == 34 diff --git a/tests/y_2024/test_2024_day11.py b/tests/y_2024/test_2024_day11.py new file mode 100644 index 0000000..ed18184 --- /dev/null +++ b/tests/y_2024/test_2024_day11.py @@ -0,0 +1,39 @@ +# from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2024.day11 import Day + +with patch( + "builtins.open", + mock_open( + read_data="""............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ +""", # noqa: E501 + ), +): + day = Day() + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + r = day._calculate_1() + assert r == 14 + + +def test_calculate_2(): + r = day._calculate_2() + assert r == 34 diff --git a/tests/y_2024/test_2024_day12.py b/tests/y_2024/test_2024_day12.py new file mode 100644 index 0000000..5c63e49 --- /dev/null +++ b/tests/y_2024/test_2024_day12.py @@ -0,0 +1,39 @@ +# from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2024.day12 import Day + +with patch( + "builtins.open", + mock_open( + read_data="""............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ +""", # noqa: E501 + ), +): + day = Day() + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + r = day._calculate_1() + assert r == 14 + + +def test_calculate_2(): + r = day._calculate_2() + assert r == 34 diff --git a/tests/y_2024/test_2024_day9.py b/tests/y_2024/test_2024_day9.py new file mode 100644 index 0000000..ee29026 --- /dev/null +++ b/tests/y_2024/test_2024_day9.py @@ -0,0 +1,27 @@ +# from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2024.day9 import Day + +with patch( + "builtins.open", + mock_open( + read_data="""2333133121414131402""", # noqa: E501 + ), +): + day = Day() + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + r = day._calculate_1() + assert r == 1928 + + +def test_calculate_2(): + r = day._calculate_2() + assert r == 2858 diff --git a/y_2024/day9.py b/y_2024/day9.py index 7d1d614..df36f2e 100644 --- a/y_2024/day9.py +++ b/y_2024/day9.py @@ -35,26 +35,15 @@ def _preprocess_input(self): def _calculate_1(self): result = 0 - return 0 r = [] for c, x in enumerate(self.__input_data[0].original): - # print(f"{c//2=},{c%2=},{c=}{x=}") m = c // 2 if c % 2 == 0 else "." - # print(f"{m=}, {x=}") + for i in range(int(x)): r.append(Elem(str(m))) - # r.append(v) - # print(f"{v=}") - ... - # print(r) - # string = list(''.join(r)) - print("here") - len_file = len([i for i in r if i.x != "."]) - # print(f"{string=}, {len_file=}") - print(f"{len_file=}") - print(f"here, {len_file=}") + c = 0 - # d=0 + for i in range(len(r) - 1): x = r[len(r) - 1 - i].x try: @@ -75,22 +64,11 @@ def _calculate_2(self): result = 0 r = [] for c, x in enumerate(self.__input_data[0].original): - # print(f"{c//2=},{c%2=},{c=}{x=}") m = c // 2 if c % 2 == 0 else "." - # print(f"{m=}, {x=}") + for i in range(int(x)): r.append(Elem(str(m))) - # r.append(v) - # print(f"{v=}") - ... - # print(r) - # string = list(''.join(r)) - print("here") - len_file = len([i for i in r if i.x != "."]) - # print(f"{string=}, {len_file=}") - print(f"{len_file=}") - print(f"here, {len_file=}") - # chunks + chunks = [] chunks2 = [] c = [] @@ -99,13 +77,10 @@ def _calculate_2(self): c.append(x) c2.append(len(r) - 1) for i in range(1, len(r)): - # print(i) if r[-1 - i].x == x.x: - # print(r[-1-i].x,x.x) c.append(r[len(r) - 1 - i]) c2.append(len(r) - 1 - i) else: - # print(r[-1-i].x,x.x) if c[0].x != ".": chunks.append(c) chunks2.append(c2[::-1]) @@ -114,13 +89,7 @@ def _calculate_2(self): x = r[-1 - i] c.append(x) c2.append(len(r) - 1 - i) - # print(chunks) - - # for c,i in enumerate(chunks): - # print(i,chunks2[c]) - # print('now') - # free slots slots = [] slo = deque() for i in range(len(r)): @@ -130,40 +99,25 @@ def _calculate_2(self): if len(slo) > 0: slots.append(slo) slo = deque() - # for i in slots: - # print(i) - # return + c = 0 - # d=0 - print(len(r), r[-60:]) - print("tada\n\n\n") + for c, i in enumerate(chunks): - # # x = chunks[len(chunks)-1-i] - # print(i, len(i)) for j in range(len(slots)): - # print(j,len(j)) - if len(i) <= len(slots[j]): if slots[j][0] > chunks2[c][0]: break - # print("swap") - # print(chunks2[c]) for m in chunks2[c]: - # print(m) r[m] = Elem("#") n = slots[j].popleft() - # print(r[n],i[0], len(i)) - # if r[n].x!='.': - # breakpoint() + r[n] = i[0] break - print(len(r), r[-60:]) - print("".join([i.x for i in r])) + result = 0 for c, i in enumerate(r): - # print(i) if i.x not in ["#", "."]: result += c * int(i.x) - # 8504654861152 too high + return result