generated from public/aoc_template
day9 - foreign input; reworked p1 to work with both inputs
This commit is contained in:
parent
543684569b
commit
34801a6490
40
day09.py
40
day09.py
@ -36,10 +36,12 @@ class Day(AOCDay):
|
|||||||
inputs = [
|
inputs = [
|
||||||
[
|
[
|
||||||
(1928, "input9_test"),
|
(1928, "input9_test"),
|
||||||
|
(6154342787400, "input9_dennis"),
|
||||||
(6346871685398, "input9"),
|
(6346871685398, "input9"),
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
(2858, "input9_test"),
|
(2858, "input9_test"),
|
||||||
|
(6183632723350, "input9_dennis"),
|
||||||
(6373055193464, "input9"),
|
(6373055193464, "input9"),
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
@ -62,38 +64,18 @@ class Day(AOCDay):
|
|||||||
return head, tail
|
return head, tail
|
||||||
|
|
||||||
def part1(self) -> Any:
|
def part1(self) -> Any:
|
||||||
head, tail = self.parse_input()
|
filesystem = [i // 2 if i % 2 == 0 else -1 for i, c in enumerate(map(int, self.getInput())) for _ in range(c)]
|
||||||
|
index = 0
|
||||||
index = head
|
while index < len(filesystem):
|
||||||
while index is not None:
|
if filesystem[index] != -1:
|
||||||
if index.file_id != -1:
|
index += 1
|
||||||
index = index.right
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if tail.file_id == -1:
|
filesystem[index] = filesystem.pop()
|
||||||
tail = tail.left
|
while filesystem[-1] == -1:
|
||||||
tail.right = None
|
filesystem.pop()
|
||||||
|
|
||||||
if tail.file_length > index.file_length:
|
return sum(i * c for i, c in enumerate(filesystem))
|
||||||
index.file_id = tail.file_id
|
|
||||||
tail.file_length -= index.file_length
|
|
||||||
elif tail.file_length < index.file_length:
|
|
||||||
space_left = index.file_length - tail.file_length
|
|
||||||
index.file_id = tail.file_id
|
|
||||||
index.file_length = tail.file_length
|
|
||||||
tail = tail.left
|
|
||||||
tail.right = None
|
|
||||||
new_space = Block(-1, space_left, index)
|
|
||||||
new_space.right = index.right
|
|
||||||
index.right = new_space
|
|
||||||
else:
|
|
||||||
index.file_id = tail.file_id
|
|
||||||
tail = tail.left
|
|
||||||
tail.right = None
|
|
||||||
|
|
||||||
index = index.right
|
|
||||||
|
|
||||||
return get_checksum(head)
|
|
||||||
|
|
||||||
def part2(self) -> Any:
|
def part2(self) -> Any:
|
||||||
head, tail = self.parse_input()
|
head, tail = self.parse_input()
|
||||||
|
|||||||
1
inputs/input9_dennis
Normal file
1
inputs/input9_dennis
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user