Compare commits

...

2 Commits

9 changed files with 12064 additions and 0 deletions

54
day5/common.py Normal file
View File

@ -0,0 +1,54 @@
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

26
day5/part1.py Normal file
View File

@ -0,0 +1,26 @@
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()

27
day5/part2.py Normal file
View File

@ -0,0 +1,27 @@
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()

9
day5/toy_input Normal file
View File

@ -0,0 +1,9 @@
[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 Normal file

File diff suppressed because it is too large Load Diff

3
day6/common.py Normal file
View File

@ -0,0 +1,3 @@
def parse(filename: str):
with open(filename) as f:
return f.readline()[:-1]

20
day6/part1.py Normal file
View File

@ -0,0 +1,20 @@
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()

20
day6/part2.py Normal file
View File

@ -0,0 +1,20 @@
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 Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/env python
from common import parse