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 = [
|
||||
[
|
||||
(1928, "input9_test"),
|
||||
(6154342787400, "input9_dennis"),
|
||||
(6346871685398, "input9"),
|
||||
],
|
||||
[
|
||||
(2858, "input9_test"),
|
||||
(6183632723350, "input9_dennis"),
|
||||
(6373055193464, "input9"),
|
||||
],
|
||||
]
|
||||
@ -62,38 +64,18 @@ class Day(AOCDay):
|
||||
return head, tail
|
||||
|
||||
def part1(self) -> Any:
|
||||
head, tail = self.parse_input()
|
||||
|
||||
index = head
|
||||
while index is not None:
|
||||
if index.file_id != -1:
|
||||
index = index.right
|
||||
filesystem = [i // 2 if i % 2 == 0 else -1 for i, c in enumerate(map(int, self.getInput())) for _ in range(c)]
|
||||
index = 0
|
||||
while index < len(filesystem):
|
||||
if filesystem[index] != -1:
|
||||
index += 1
|
||||
continue
|
||||
|
||||
if tail.file_id == -1:
|
||||
tail = tail.left
|
||||
tail.right = None
|
||||
filesystem[index] = filesystem.pop()
|
||||
while filesystem[-1] == -1:
|
||||
filesystem.pop()
|
||||
|
||||
if tail.file_length > index.file_length:
|
||||
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)
|
||||
return sum(i * c for i, c in enumerate(filesystem))
|
||||
|
||||
def part2(self) -> Any:
|
||||
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