Compare commits
No commits in common. "d8006792913a0342aa6a3d79264e9afe29d6b65d" and "9e21d8d9c8ad2dd7bb03a36da2fbeed54e414d0b" have entirely different histories.
d800679291
...
9e21d8d9c8
@ -1,54 +0,0 @@
|
|||||||
import re
|
|
||||||
from copy import deepcopy
|
|
||||||
|
|
||||||
|
|
||||||
def parse(filename: str) -> tuple[list[list[str]], list[tuple[int, int, int]]]:
|
|
||||||
stacks_of_crates = []
|
|
||||||
operations = []
|
|
||||||
finalised_stacks = False
|
|
||||||
with open(filename) as f:
|
|
||||||
while not finalised_stacks:
|
|
||||||
row = f.readline()[:-1]
|
|
||||||
if re.match(r"(\s{3}|\[.\])\s?", row):
|
|
||||||
crates = []
|
|
||||||
while row != "":
|
|
||||||
crate = row[0:3]
|
|
||||||
crates.append(crate[1])
|
|
||||||
row = row[4:]
|
|
||||||
for idx, crate in enumerate(crates):
|
|
||||||
if crate != " ":
|
|
||||||
if idx < len(stacks_of_crates):
|
|
||||||
stacks_of_crates[idx].append(crate)
|
|
||||||
else:
|
|
||||||
stacks_of_crates.append([crate])
|
|
||||||
else:
|
|
||||||
if idx >= len(stacks_of_crates):
|
|
||||||
stacks_of_crates.append([])
|
|
||||||
else:
|
|
||||||
finalised_stacks = True
|
|
||||||
numbers = re.findall(r" \d ", row)
|
|
||||||
last = int(numbers[len(numbers) - 1])
|
|
||||||
assert last == len(stacks_of_crates)
|
|
||||||
f.readline()
|
|
||||||
while line := f.readline():
|
|
||||||
if match := re.match(r"move (\d+) from (\d) to (\d)", line):
|
|
||||||
operations.append((int(match.group(1)), int(match.group(2)), int(match.group(3))))
|
|
||||||
return list(map(lambda x: list(reversed(x)), stacks_of_crates)), operations
|
|
||||||
|
|
||||||
|
|
||||||
def pretty_print(stacks: list[list[str]]) -> None:
|
|
||||||
max_len = max(map(len, stacks))
|
|
||||||
the_stacks = deepcopy(stacks)
|
|
||||||
for idx, stack in enumerate(the_stacks):
|
|
||||||
for i in range(max_len - len(stack)):
|
|
||||||
the_stacks[idx].append(" ")
|
|
||||||
for i in range(max_len):
|
|
||||||
for j in range(len(the_stacks)):
|
|
||||||
char = the_stacks[j].pop()
|
|
||||||
if char != " ":
|
|
||||||
print(f"[{char}]", end=" ")
|
|
||||||
else:
|
|
||||||
print(" ", end=" ")
|
|
||||||
print("")
|
|
||||||
print("".join([f" {a} " for a in range(1, len(the_stacks) + 1)]))
|
|
||||||
pass
|
|
@ -1,26 +0,0 @@
|
|||||||
from common import parse # , pretty_print
|
|
||||||
|
|
||||||
|
|
||||||
def move(stacks: list[list[str]], operation: tuple[int, int, int]) -> list[list[str]]:
|
|
||||||
# print(f"move {operation[0]} from {operation[1]} to {operation[2]}")
|
|
||||||
for i in range(operation[0]):
|
|
||||||
num = stacks[operation[1] - 1].pop()
|
|
||||||
stacks[operation[2] - 1].append(num)
|
|
||||||
return stacks
|
|
||||||
|
|
||||||
|
|
||||||
def solve(input: tuple[list[list[str]], list[tuple[int, int, int]]]) -> str:
|
|
||||||
stacks, operations = input
|
|
||||||
# pretty_print(stacks)
|
|
||||||
for operation in operations:
|
|
||||||
stacks = move(stacks, operation)
|
|
||||||
# pretty_print(stacks)
|
|
||||||
return "".join(map(lambda x: x[len(x) - 1] if len(x) != 0 else "", stacks))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(solve(parse("input")))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
@ -1,27 +0,0 @@
|
|||||||
from common import parse # , pretty_print
|
|
||||||
|
|
||||||
|
|
||||||
def move(stacks: list[list[str]], operation: tuple[int, int, int]) -> list[list[str]]:
|
|
||||||
# print(f"move {operation[0]} from {operation[1]} to {operation[2]}")
|
|
||||||
dst = operation[2] - 1
|
|
||||||
orig = operation[1] - 1
|
|
||||||
stacks[dst] += stacks[orig][len(stacks[orig]) - operation[0] : len(stacks[orig])]
|
|
||||||
stacks[orig] = stacks[orig][: len(stacks[orig]) - operation[0]]
|
|
||||||
return stacks
|
|
||||||
|
|
||||||
|
|
||||||
def solve(input: tuple[list[list[str]], list[tuple[int, int, int]]]) -> str:
|
|
||||||
stacks, operations = input
|
|
||||||
# pretty_print(stacks)
|
|
||||||
for operation in operations:
|
|
||||||
stacks = move(stacks, operation)
|
|
||||||
# pretty_print(stacks)
|
|
||||||
return "".join(map(lambda x: x[len(x) - 1] if len(x) != 0 else "", stacks))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(solve(parse("input")))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
@ -1,9 +0,0 @@
|
|||||||
[D]
|
|
||||||
[N] [C]
|
|
||||||
[Z] [M] [P]
|
|
||||||
1 2 3
|
|
||||||
|
|
||||||
move 1 from 2 to 1
|
|
||||||
move 3 from 1 to 3
|
|
||||||
move 2 from 2 to 1
|
|
||||||
move 1 from 1 to 2
|
|
11904
day5/trac.log
11904
day5/trac.log
File diff suppressed because it is too large
Load Diff
@ -1,3 +0,0 @@
|
|||||||
def parse(filename: str):
|
|
||||||
with open(filename) as f:
|
|
||||||
return f.readline()[:-1]
|
|
@ -1,20 +0,0 @@
|
|||||||
from common import parse
|
|
||||||
|
|
||||||
|
|
||||||
def all_different_characters(input: str) -> bool:
|
|
||||||
return all(map(lambda c: input.count(c) == 1, input))
|
|
||||||
|
|
||||||
|
|
||||||
def solve(input: str) -> int:
|
|
||||||
i = 0
|
|
||||||
while i < len(input) and not all_different_characters(input[i : i + 4]):
|
|
||||||
i += 1
|
|
||||||
return i + 4
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(solve(parse("input")))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
@ -1,20 +0,0 @@
|
|||||||
from common import parse
|
|
||||||
|
|
||||||
|
|
||||||
def all_different_characters(input: str) -> bool:
|
|
||||||
return all(map(lambda c: input.count(c) == 1, input))
|
|
||||||
|
|
||||||
|
|
||||||
def solve(input: str) -> int:
|
|
||||||
i = 0
|
|
||||||
while i < len(input) and not all_different_characters(input[i : i + 14]):
|
|
||||||
i += 1
|
|
||||||
return i + 14
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(solve(parse("input")))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
1
template/part1.py
Executable file → Normal file
1
template/part1.py
Executable file → Normal file
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
from common import parse
|
from common import parse
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user