Skip to content

Commit

Permalink
day9
Browse files Browse the repository at this point in the history
  • Loading branch information
Stegallo committed Dec 14, 2024
1 parent 12557e4 commit 12e241c
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 56 deletions.
39 changes: 39 additions & 0 deletions tests/y_2024/test_2024_day10.py
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions tests/y_2024/test_2024_day11.py
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions tests/y_2024/test_2024_day12.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions tests/y_2024/test_2024_day9.py
Original file line number Diff line number Diff line change
@@ -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
66 changes: 10 additions & 56 deletions y_2024/day9.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = []
Expand All @@ -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])
Expand All @@ -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)):
Expand All @@ -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

0 comments on commit 12e241c

Please sign in to comment.