Compare commits

...

29 commits

Author SHA1 Message Date
Bruno BELANYI f501a74b5c 2022: d10: ex2: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 5731ff8e82 2022: d10: ex2: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 426deae1ce 2022: d10: ex1: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 63bbd74790 2022: d10: ex1: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 20779b5e2e 2022: d09: ex2: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 912e8e6edb 2022: d09: ex2: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI a505c9de49 2022: d09: ex1: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 71280e082d 2022: d09: ex1: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI f6fbf6d6e6 2022: d08: ex2: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 8b63d66750 2022: d08: ex2: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 637b7e64c2 2022: d08: ex1: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI a45e00083c 2022: d08: ex1: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI a6f1d09f91 2022: d07: ex2: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 02a0c87f74 2022: d07: ex2: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 5270736a28 2022: d07: ex1: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 761a37e8fe 2022: d07: ex1: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 0c2712d7db 2022: d06: ex2: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 20fcfe32b2 2022: d06: ex2: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 6c4ead5e1b 2022: d06: ex1: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 65cb34a16a 2022: d06: ex1: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 5ad92f1585 2022: d05: ex2: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI fd66f79400 2022: d05: ex2: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI e2d1a5f74d 2022: d05: ex1: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI cea94a22fb 2022: d05: ex1: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI c2cfef5e30 2022: d04: ex2: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 9b01dc5723 2022: d04: ex2: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI d43d8c0cf1 2022: d04: ex1: add solution 2022-12-10 10:32:40 +01:00
Bruno BELANYI 32ccf155df 2022: d04: ex1: add input 2022-12-10 10:32:40 +01:00
Bruno BELANYI 3030162e09 2022: d03: ex2: add solution 2022-12-10 10:32:40 +01:00
29 changed files with 10343 additions and 0 deletions

29
2022/d03/ex2/ex2.py Executable file
View file

@ -0,0 +1,29 @@
#!/usr/bin/env python
import sys
from collections.abc import Iterator
Group = tuple[str, str, str]
def solve(input: list[str]) -> int:
def score(lines: Group) -> int:
common_items = set(lines[0]) & set(lines[1]) & set(lines[2])
assert len(common_items) == 1 # Sanity check
common = common_items.pop()
return common.isupper() * 26 + ord(common.lower()) - ord("a") + 1
def iter_3() -> Iterator[Group]:
args = [iter(input)] * 3
yield from zip(*args, strict=True) # type: ignore
return sum(map(score, iter_3()))
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

24
2022/d04/ex1/ex1.py Executable file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env python
import sys
def solve(input: list[str]) -> int:
def is_redundant(line: str) -> bool:
def assignment_to_set(elf: str) -> set[int]:
start, end = elf.split("-")
return set(range(int(start), int(end) + 1))
elf1, elf2 = map(assignment_to_set, line.split(","))
return elf1.issubset(elf2) or elf2.issubset(elf1)
return sum(map(is_redundant, input))
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

1000
2022/d04/ex1/input Normal file

File diff suppressed because it is too large Load diff

24
2022/d04/ex2/ex2.py Executable file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env python
import sys
def solve(input: list[str]) -> int:
def is_overlapping(line: str) -> bool:
def assignment_to_set(elf: str) -> set[int]:
start, end = elf.split("-")
return set(range(int(start), int(end) + 1))
elf1, elf2 = map(assignment_to_set, line.split(","))
return bool(elf1 & elf2)
return sum(map(is_overlapping, input))
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

1000
2022/d04/ex2/input Normal file

File diff suppressed because it is too large Load diff

55
2022/d05/ex1/ex1.py Executable file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env python
import dataclasses
import sys
Stack = list[str]
@dataclasses.dataclass
class Instruction:
n: int
start: int
end: int
@classmethod
def from_input(cls, input: str) -> "Instruction":
words = input.split()
return Instruction(int(words[1]), int(words[3]) - 1, int(words[5]) - 1)
def parse(input: list[str]) -> tuple[list[Stack], list[Instruction]]:
def parse_stacks(input: list[str]) -> list[Stack]:
stacks: list[Stack] = [[] for _ in range(1, len(input[-1]), 4)]
for line in reversed(input[:-1]):
for stack, i in enumerate(range(1, len(line), 4)):
c = line[i]
if c == " ":
continue
stacks[stack].append(c)
return stacks
def parse_instructions(input: list[str]) -> list[Instruction]:
return list(map(Instruction.from_input, input))
empty_line = input.index("")
return parse_stacks(input[:empty_line]), parse_instructions(input[empty_line + 1 :])
def solve(input: list[str]) -> str:
stacks, instructions = parse(input)
for instr in instructions:
start, end = stacks[instr.start], stacks[instr.end]
items = (start.pop() for _ in range(instr.n))
end.extend(items)
return "".join(stack[-1] for stack in stacks if stack)
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

512
2022/d05/ex1/input Normal file
View file

@ -0,0 +1,512 @@
[W] [V] [P]
[B] [T] [C] [B] [G]
[G] [S] [V] [H] [N] [T]
[Z] [B] [W] [J] [D] [M] [S]
[R] [C] [N] [N] [F] [W] [C] [W]
[D] [F] [S] [M] [L] [T] [L] [Z] [Z]
[C] [W] [B] [G] [S] [V] [F] [D] [N]
[V] [G] [C] [Q] [T] [J] [P] [B] [M]
1 2 3 4 5 6 7 8 9
move 2 from 8 to 4
move 2 from 7 to 3
move 2 from 9 to 2
move 4 from 1 to 9
move 1 from 7 to 8
move 1 from 9 to 6
move 6 from 6 to 1
move 6 from 1 to 6
move 2 from 7 to 1
move 9 from 4 to 1
move 6 from 2 to 7
move 5 from 9 to 7
move 7 from 3 to 7
move 19 from 7 to 9
move 1 from 7 to 1
move 3 from 6 to 8
move 4 from 5 to 6
move 1 from 4 to 1
move 2 from 5 to 2
move 8 from 9 to 7
move 1 from 5 to 1
move 3 from 9 to 4
move 1 from 4 to 9
move 2 from 4 to 7
move 1 from 7 to 6
move 3 from 8 to 9
move 17 from 1 to 7
move 21 from 7 to 3
move 4 from 6 to 2
move 1 from 7 to 2
move 8 from 9 to 2
move 2 from 8 to 3
move 1 from 1 to 7
move 2 from 7 to 9
move 1 from 6 to 1
move 1 from 7 to 4
move 1 from 1 to 2
move 4 from 6 to 1
move 1 from 9 to 2
move 1 from 7 to 9
move 1 from 4 to 1
move 7 from 3 to 7
move 6 from 2 to 8
move 3 from 3 to 4
move 1 from 8 to 4
move 12 from 2 to 8
move 2 from 9 to 2
move 12 from 3 to 2
move 12 from 8 to 6
move 3 from 4 to 2
move 19 from 2 to 8
move 4 from 1 to 9
move 1 from 3 to 8
move 1 from 4 to 6
move 1 from 2 to 4
move 1 from 6 to 3
move 8 from 9 to 6
move 1 from 4 to 9
move 1 from 3 to 1
move 1 from 9 to 5
move 11 from 6 to 3
move 5 from 8 to 6
move 14 from 6 to 9
move 2 from 1 to 4
move 3 from 8 to 1
move 8 from 8 to 4
move 3 from 3 to 4
move 8 from 3 to 1
move 9 from 8 to 2
move 12 from 4 to 2
move 12 from 9 to 3
move 9 from 3 to 4
move 1 from 5 to 3
move 7 from 7 to 1
move 2 from 9 to 1
move 2 from 4 to 6
move 16 from 2 to 6
move 1 from 2 to 8
move 10 from 1 to 4
move 1 from 8 to 2
move 4 from 1 to 6
move 15 from 4 to 8
move 1 from 4 to 2
move 2 from 6 to 8
move 5 from 2 to 8
move 21 from 8 to 3
move 6 from 1 to 3
move 15 from 6 to 1
move 1 from 2 to 1
move 1 from 8 to 9
move 15 from 1 to 3
move 7 from 3 to 8
move 1 from 7 to 9
move 2 from 9 to 8
move 2 from 3 to 7
move 4 from 6 to 1
move 2 from 7 to 8
move 1 from 6 to 2
move 4 from 8 to 3
move 2 from 4 to 8
move 1 from 2 to 1
move 4 from 1 to 5
move 3 from 5 to 8
move 1 from 5 to 1
move 12 from 8 to 3
move 3 from 1 to 2
move 17 from 3 to 5
move 2 from 5 to 3
move 15 from 5 to 1
move 1 from 1 to 4
move 17 from 3 to 2
move 5 from 2 to 8
move 17 from 3 to 6
move 6 from 1 to 3
move 5 from 1 to 6
move 4 from 8 to 9
move 10 from 3 to 8
move 7 from 2 to 9
move 2 from 6 to 3
move 2 from 2 to 8
move 1 from 1 to 4
move 17 from 6 to 9
move 13 from 8 to 2
move 2 from 4 to 1
move 1 from 6 to 7
move 2 from 2 to 4
move 8 from 2 to 7
move 1 from 6 to 1
move 4 from 7 to 9
move 1 from 4 to 7
move 1 from 4 to 6
move 1 from 1 to 7
move 5 from 2 to 4
move 2 from 3 to 8
move 6 from 7 to 1
move 1 from 7 to 4
move 11 from 9 to 7
move 1 from 8 to 4
move 8 from 1 to 2
move 1 from 1 to 4
move 1 from 1 to 9
move 1 from 6 to 1
move 1 from 8 to 4
move 6 from 2 to 3
move 1 from 1 to 3
move 1 from 6 to 7
move 1 from 4 to 6
move 6 from 2 to 5
move 7 from 3 to 4
move 2 from 7 to 6
move 2 from 7 to 3
move 8 from 7 to 5
move 3 from 6 to 7
move 1 from 5 to 7
move 1 from 7 to 5
move 13 from 9 to 3
move 1 from 3 to 8
move 8 from 4 to 3
move 3 from 5 to 1
move 7 from 4 to 1
move 5 from 1 to 4
move 3 from 1 to 4
move 2 from 1 to 8
move 2 from 7 to 5
move 2 from 8 to 9
move 1 from 7 to 6
move 1 from 8 to 7
move 4 from 5 to 1
move 1 from 7 to 2
move 2 from 1 to 8
move 1 from 2 to 1
move 5 from 9 to 7
move 3 from 9 to 4
move 8 from 4 to 8
move 6 from 8 to 5
move 11 from 5 to 1
move 3 from 4 to 2
move 9 from 3 to 7
move 6 from 7 to 2
move 13 from 3 to 2
move 3 from 8 to 1
move 2 from 2 to 8
move 1 from 6 to 7
move 3 from 8 to 4
move 9 from 1 to 5
move 5 from 5 to 8
move 2 from 8 to 4
move 3 from 9 to 4
move 2 from 8 to 2
move 8 from 1 to 5
move 8 from 7 to 9
move 1 from 8 to 3
move 15 from 5 to 9
move 6 from 4 to 1
move 1 from 7 to 2
move 4 from 2 to 1
move 1 from 3 to 4
move 5 from 1 to 7
move 3 from 7 to 3
move 14 from 9 to 8
move 1 from 4 to 8
move 1 from 7 to 6
move 2 from 4 to 5
move 4 from 1 to 5
move 1 from 6 to 5
move 4 from 9 to 3
move 5 from 3 to 7
move 4 from 5 to 9
move 1 from 3 to 7
move 1 from 3 to 2
move 4 from 5 to 2
move 4 from 7 to 5
move 4 from 2 to 1
move 1 from 5 to 4
move 7 from 9 to 7
move 1 from 4 to 2
move 1 from 5 to 8
move 21 from 2 to 4
move 1 from 9 to 8
move 1 from 9 to 4
move 3 from 4 to 1
move 7 from 1 to 6
move 1 from 5 to 1
move 18 from 4 to 7
move 1 from 5 to 8
move 27 from 7 to 8
move 1 from 7 to 3
move 1 from 3 to 7
move 1 from 7 to 2
move 1 from 2 to 1
move 42 from 8 to 9
move 1 from 8 to 7
move 1 from 8 to 2
move 1 from 4 to 6
move 1 from 2 to 9
move 2 from 1 to 2
move 1 from 7 to 3
move 7 from 6 to 4
move 4 from 9 to 6
move 1 from 3 to 2
move 1 from 2 to 7
move 2 from 2 to 5
move 1 from 8 to 4
move 1 from 9 to 3
move 5 from 4 to 7
move 1 from 5 to 6
move 1 from 5 to 9
move 1 from 6 to 3
move 1 from 7 to 5
move 2 from 3 to 2
move 22 from 9 to 7
move 2 from 2 to 3
move 18 from 7 to 9
move 1 from 4 to 9
move 1 from 1 to 4
move 4 from 7 to 3
move 4 from 3 to 2
move 3 from 4 to 5
move 1 from 2 to 4
move 5 from 6 to 9
move 1 from 5 to 3
move 1 from 4 to 7
move 2 from 5 to 1
move 3 from 2 to 4
move 1 from 5 to 6
move 2 from 7 to 9
move 1 from 6 to 8
move 2 from 3 to 2
move 2 from 4 to 7
move 1 from 8 to 7
move 1 from 4 to 6
move 35 from 9 to 7
move 13 from 7 to 3
move 1 from 2 to 7
move 1 from 2 to 5
move 1 from 5 to 8
move 1 from 8 to 5
move 8 from 7 to 3
move 1 from 6 to 4
move 6 from 3 to 9
move 1 from 1 to 9
move 1 from 4 to 1
move 14 from 9 to 8
move 1 from 5 to 7
move 16 from 3 to 2
move 2 from 1 to 2
move 1 from 9 to 2
move 1 from 8 to 1
move 1 from 1 to 3
move 7 from 2 to 9
move 6 from 9 to 8
move 1 from 3 to 4
move 3 from 7 to 6
move 2 from 2 to 1
move 1 from 4 to 7
move 2 from 2 to 5
move 1 from 9 to 6
move 2 from 2 to 5
move 2 from 6 to 2
move 4 from 5 to 4
move 5 from 2 to 6
move 1 from 1 to 7
move 1 from 1 to 2
move 13 from 8 to 1
move 2 from 8 to 4
move 19 from 7 to 4
move 3 from 1 to 6
move 11 from 4 to 3
move 2 from 7 to 9
move 4 from 2 to 5
move 2 from 9 to 5
move 1 from 7 to 4
move 2 from 5 to 7
move 4 from 3 to 4
move 3 from 4 to 1
move 3 from 5 to 1
move 9 from 6 to 4
move 1 from 7 to 9
move 1 from 7 to 5
move 10 from 1 to 4
move 1 from 9 to 6
move 1 from 6 to 8
move 32 from 4 to 5
move 7 from 5 to 4
move 27 from 5 to 9
move 5 from 3 to 2
move 3 from 2 to 8
move 1 from 6 to 2
move 8 from 4 to 9
move 1 from 2 to 9
move 8 from 8 to 6
move 2 from 4 to 3
move 1 from 2 to 3
move 15 from 9 to 8
move 4 from 1 to 4
move 3 from 4 to 8
move 6 from 9 to 7
move 1 from 4 to 9
move 8 from 8 to 2
move 2 from 1 to 9
move 2 from 7 to 9
move 10 from 8 to 3
move 6 from 2 to 6
move 2 from 3 to 2
move 6 from 6 to 3
move 1 from 7 to 5
move 8 from 3 to 2
move 4 from 3 to 2
move 1 from 3 to 5
move 6 from 6 to 1
move 4 from 3 to 7
move 2 from 5 to 8
move 3 from 7 to 5
move 6 from 1 to 7
move 1 from 3 to 4
move 1 from 3 to 9
move 10 from 7 to 4
move 8 from 2 to 8
move 11 from 9 to 5
move 11 from 4 to 1
move 5 from 2 to 6
move 3 from 2 to 7
move 11 from 1 to 6
move 1 from 5 to 6
move 8 from 5 to 4
move 19 from 6 to 7
move 3 from 7 to 9
move 3 from 5 to 4
move 1 from 2 to 5
move 3 from 5 to 7
move 8 from 9 to 6
move 2 from 4 to 1
move 1 from 1 to 9
move 2 from 9 to 7
move 6 from 6 to 2
move 2 from 4 to 6
move 4 from 8 to 6
move 1 from 8 to 1
move 7 from 6 to 7
move 1 from 9 to 4
move 5 from 8 to 4
move 3 from 2 to 6
move 4 from 6 to 4
move 2 from 9 to 6
move 3 from 2 to 9
move 16 from 4 to 8
move 1 from 6 to 8
move 2 from 9 to 5
move 1 from 9 to 7
move 2 from 5 to 2
move 1 from 4 to 6
move 2 from 2 to 5
move 1 from 9 to 6
move 3 from 7 to 3
move 7 from 7 to 8
move 2 from 7 to 1
move 3 from 8 to 5
move 3 from 6 to 2
move 4 from 7 to 4
move 1 from 5 to 1
move 1 from 5 to 7
move 3 from 3 to 4
move 5 from 1 to 4
move 16 from 7 to 2
move 5 from 4 to 7
move 19 from 8 to 1
move 11 from 2 to 9
move 11 from 9 to 6
move 2 from 1 to 6
move 2 from 4 to 1
move 5 from 4 to 6
move 1 from 5 to 9
move 1 from 9 to 6
move 2 from 2 to 6
move 1 from 5 to 4
move 8 from 6 to 5
move 16 from 1 to 6
move 1 from 4 to 9
move 3 from 2 to 9
move 2 from 2 to 5
move 2 from 5 to 8
move 4 from 8 to 4
move 4 from 9 to 7
move 2 from 1 to 3
move 5 from 6 to 4
move 21 from 6 to 2
move 9 from 7 to 3
move 1 from 1 to 2
move 1 from 5 to 3
move 23 from 2 to 7
move 1 from 7 to 5
move 3 from 6 to 1
move 9 from 4 to 5
move 11 from 7 to 1
move 2 from 3 to 4
move 1 from 3 to 7
move 1 from 4 to 1
move 10 from 1 to 6
move 5 from 7 to 1
move 3 from 1 to 4
move 7 from 1 to 7
move 4 from 3 to 8
move 4 from 7 to 4
move 5 from 7 to 3
move 2 from 4 to 9
move 1 from 8 to 1
move 4 from 4 to 1
move 1 from 6 to 1
move 1 from 6 to 5
move 16 from 5 to 1
move 2 from 5 to 7
move 1 from 5 to 6
move 2 from 8 to 2
move 1 from 7 to 9
move 3 from 9 to 5
move 2 from 5 to 4
move 6 from 7 to 1
move 3 from 4 to 7
move 1 from 8 to 6
move 5 from 1 to 4
move 1 from 6 to 1
move 19 from 1 to 5
move 1 from 7 to 6
move 9 from 3 to 1
move 6 from 6 to 5
move 4 from 6 to 9
move 3 from 9 to 4
move 13 from 1 to 4
move 1 from 3 to 1
move 2 from 5 to 1
move 1 from 2 to 3
move 1 from 3 to 9
move 4 from 5 to 4
move 1 from 2 to 3
move 1 from 3 to 5
move 1 from 9 to 1
move 1 from 9 to 5
move 19 from 4 to 7
move 4 from 1 to 6
move 5 from 4 to 3
move 3 from 6 to 1
move 1 from 6 to 8
move 2 from 1 to 6
move 2 from 1 to 7
move 2 from 6 to 3
move 2 from 3 to 1
move 8 from 7 to 6
move 5 from 3 to 9
move 2 from 4 to 9
move 2 from 6 to 8
move 10 from 7 to 2
move 7 from 2 to 9
move 1 from 8 to 9
move 1 from 1 to 2
move 2 from 9 to 3
move 2 from 8 to 7
move 1 from 1 to 6
move 1 from 2 to 8
move 2 from 2 to 5
move 4 from 5 to 7
move 5 from 6 to 1
move 1 from 3 to 4

56
2022/d05/ex2/ex2.py Executable file
View file

@ -0,0 +1,56 @@
#!/usr/bin/env python
import dataclasses
import sys
Stack = list[str]
@dataclasses.dataclass
class Instruction:
n: int
start: int
end: int
@classmethod
def from_input(cls, input: str) -> "Instruction":
words = input.split()
return Instruction(int(words[1]), int(words[3]) - 1, int(words[5]) - 1)
def parse(input: list[str]) -> tuple[list[Stack], list[Instruction]]:
def parse_stacks(input: list[str]) -> list[Stack]:
stacks: list[Stack] = [[] for _ in range(1, len(input[-1]), 4)]
for line in reversed(input[:-1]):
for stack, i in enumerate(range(1, len(line), 4)):
c = line[i]
if c == " ":
continue
stacks[stack].append(c)
return stacks
def parse_instructions(input: list[str]) -> list[Instruction]:
return list(map(Instruction.from_input, input))
empty_line = input.index("")
return parse_stacks(input[:empty_line]), parse_instructions(input[empty_line + 1 :])
def solve(input: list[str]) -> str:
stacks, instructions = parse(input)
for instr in instructions:
start, end = stacks[instr.start], stacks[instr.end]
items = [start.pop() for _ in range(instr.n)]
items.reverse()
end.extend(items)
return "".join(stack[-1] for stack in stacks if stack)
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

512
2022/d05/ex2/input Normal file
View file

@ -0,0 +1,512 @@
[W] [V] [P]
[B] [T] [C] [B] [G]
[G] [S] [V] [H] [N] [T]
[Z] [B] [W] [J] [D] [M] [S]
[R] [C] [N] [N] [F] [W] [C] [W]
[D] [F] [S] [M] [L] [T] [L] [Z] [Z]
[C] [W] [B] [G] [S] [V] [F] [D] [N]
[V] [G] [C] [Q] [T] [J] [P] [B] [M]
1 2 3 4 5 6 7 8 9
move 2 from 8 to 4
move 2 from 7 to 3
move 2 from 9 to 2
move 4 from 1 to 9
move 1 from 7 to 8
move 1 from 9 to 6
move 6 from 6 to 1
move 6 from 1 to 6
move 2 from 7 to 1
move 9 from 4 to 1
move 6 from 2 to 7
move 5 from 9 to 7
move 7 from 3 to 7
move 19 from 7 to 9
move 1 from 7 to 1
move 3 from 6 to 8
move 4 from 5 to 6
move 1 from 4 to 1
move 2 from 5 to 2
move 8 from 9 to 7
move 1 from 5 to 1
move 3 from 9 to 4
move 1 from 4 to 9
move 2 from 4 to 7
move 1 from 7 to 6
move 3 from 8 to 9
move 17 from 1 to 7
move 21 from 7 to 3
move 4 from 6 to 2
move 1 from 7 to 2
move 8 from 9 to 2
move 2 from 8 to 3
move 1 from 1 to 7
move 2 from 7 to 9
move 1 from 6 to 1
move 1 from 7 to 4
move 1 from 1 to 2
move 4 from 6 to 1
move 1 from 9 to 2
move 1 from 7 to 9
move 1 from 4 to 1
move 7 from 3 to 7
move 6 from 2 to 8
move 3 from 3 to 4
move 1 from 8 to 4
move 12 from 2 to 8
move 2 from 9 to 2
move 12 from 3 to 2
move 12 from 8 to 6
move 3 from 4 to 2
move 19 from 2 to 8
move 4 from 1 to 9
move 1 from 3 to 8
move 1 from 4 to 6
move 1 from 2 to 4
move 1 from 6 to 3
move 8 from 9 to 6
move 1 from 4 to 9
move 1 from 3 to 1
move 1 from 9 to 5
move 11 from 6 to 3
move 5 from 8 to 6
move 14 from 6 to 9
move 2 from 1 to 4
move 3 from 8 to 1
move 8 from 8 to 4
move 3 from 3 to 4
move 8 from 3 to 1
move 9 from 8 to 2
move 12 from 4 to 2
move 12 from 9 to 3
move 9 from 3 to 4
move 1 from 5 to 3
move 7 from 7 to 1
move 2 from 9 to 1
move 2 from 4 to 6
move 16 from 2 to 6
move 1 from 2 to 8
move 10 from 1 to 4
move 1 from 8 to 2
move 4 from 1 to 6
move 15 from 4 to 8
move 1 from 4 to 2
move 2 from 6 to 8
move 5 from 2 to 8
move 21 from 8 to 3
move 6 from 1 to 3
move 15 from 6 to 1
move 1 from 2 to 1
move 1 from 8 to 9
move 15 from 1 to 3
move 7 from 3 to 8
move 1 from 7 to 9
move 2 from 9 to 8
move 2 from 3 to 7
move 4 from 6 to 1
move 2 from 7 to 8
move 1 from 6 to 2
move 4 from 8 to 3
move 2 from 4 to 8
move 1 from 2 to 1
move 4 from 1 to 5
move 3 from 5 to 8
move 1 from 5 to 1
move 12 from 8 to 3
move 3 from 1 to 2
move 17 from 3 to 5
move 2 from 5 to 3
move 15 from 5 to 1
move 1 from 1 to 4
move 17 from 3 to 2
move 5 from 2 to 8
move 17 from 3 to 6
move 6 from 1 to 3
move 5 from 1 to 6
move 4 from 8 to 9
move 10 from 3 to 8
move 7 from 2 to 9
move 2 from 6 to 3
move 2 from 2 to 8
move 1 from 1 to 4
move 17 from 6 to 9
move 13 from 8 to 2
move 2 from 4 to 1
move 1 from 6 to 7
move 2 from 2 to 4
move 8 from 2 to 7
move 1 from 6 to 1
move 4 from 7 to 9
move 1 from 4 to 7
move 1 from 4 to 6
move 1 from 1 to 7
move 5 from 2 to 4
move 2 from 3 to 8
move 6 from 7 to 1
move 1 from 7 to 4
move 11 from 9 to 7
move 1 from 8 to 4
move 8 from 1 to 2
move 1 from 1 to 4
move 1 from 1 to 9
move 1 from 6 to 1
move 1 from 8 to 4
move 6 from 2 to 3
move 1 from 1 to 3
move 1 from 6 to 7
move 1 from 4 to 6
move 6 from 2 to 5
move 7 from 3 to 4
move 2 from 7 to 6
move 2 from 7 to 3
move 8 from 7 to 5
move 3 from 6 to 7
move 1 from 5 to 7
move 1 from 7 to 5
move 13 from 9 to 3
move 1 from 3 to 8
move 8 from 4 to 3
move 3 from 5 to 1
move 7 from 4 to 1
move 5 from 1 to 4
move 3 from 1 to 4
move 2 from 1 to 8
move 2 from 7 to 5
move 2 from 8 to 9
move 1 from 7 to 6
move 1 from 8 to 7
move 4 from 5 to 1
move 1 from 7 to 2
move 2 from 1 to 8
move 1 from 2 to 1
move 5 from 9 to 7
move 3 from 9 to 4
move 8 from 4 to 8
move 6 from 8 to 5
move 11 from 5 to 1
move 3 from 4 to 2
move 9 from 3 to 7
move 6 from 7 to 2
move 13 from 3 to 2
move 3 from 8 to 1
move 2 from 2 to 8
move 1 from 6 to 7
move 3 from 8 to 4
move 9 from 1 to 5
move 5 from 5 to 8
move 2 from 8 to 4
move 3 from 9 to 4
move 2 from 8 to 2
move 8 from 1 to 5
move 8 from 7 to 9
move 1 from 8 to 3
move 15 from 5 to 9
move 6 from 4 to 1
move 1 from 7 to 2
move 4 from 2 to 1
move 1 from 3 to 4
move 5 from 1 to 7
move 3 from 7 to 3
move 14 from 9 to 8
move 1 from 4 to 8
move 1 from 7 to 6
move 2 from 4 to 5
move 4 from 1 to 5
move 1 from 6 to 5
move 4 from 9 to 3
move 5 from 3 to 7
move 4 from 5 to 9
move 1 from 3 to 7
move 1 from 3 to 2
move 4 from 5 to 2
move 4 from 7 to 5
move 4 from 2 to 1
move 1 from 5 to 4
move 7 from 9 to 7
move 1 from 4 to 2
move 1 from 5 to 8
move 21 from 2 to 4
move 1 from 9 to 8
move 1 from 9 to 4
move 3 from 4 to 1
move 7 from 1 to 6
move 1 from 5 to 1
move 18 from 4 to 7
move 1 from 5 to 8
move 27 from 7 to 8
move 1 from 7 to 3
move 1 from 3 to 7
move 1 from 7 to 2
move 1 from 2 to 1
move 42 from 8 to 9
move 1 from 8 to 7
move 1 from 8 to 2
move 1 from 4 to 6
move 1 from 2 to 9
move 2 from 1 to 2
move 1 from 7 to 3
move 7 from 6 to 4
move 4 from 9 to 6
move 1 from 3 to 2
move 1 from 2 to 7
move 2 from 2 to 5
move 1 from 8 to 4
move 1 from 9 to 3
move 5 from 4 to 7
move 1 from 5 to 6
move 1 from 5 to 9
move 1 from 6 to 3
move 1 from 7 to 5
move 2 from 3 to 2
move 22 from 9 to 7
move 2 from 2 to 3
move 18 from 7 to 9
move 1 from 4 to 9
move 1 from 1 to 4
move 4 from 7 to 3
move 4 from 3 to 2
move 3 from 4 to 5
move 1 from 2 to 4
move 5 from 6 to 9
move 1 from 5 to 3
move 1 from 4 to 7
move 2 from 5 to 1
move 3 from 2 to 4
move 1 from 5 to 6
move 2 from 7 to 9
move 1 from 6 to 8
move 2 from 3 to 2
move 2 from 4 to 7
move 1 from 8 to 7
move 1 from 4 to 6
move 35 from 9 to 7
move 13 from 7 to 3
move 1 from 2 to 7
move 1 from 2 to 5
move 1 from 5 to 8
move 1 from 8 to 5
move 8 from 7 to 3
move 1 from 6 to 4
move 6 from 3 to 9
move 1 from 1 to 9
move 1 from 4 to 1
move 14 from 9 to 8
move 1 from 5 to 7
move 16 from 3 to 2
move 2 from 1 to 2
move 1 from 9 to 2
move 1 from 8 to 1
move 1 from 1 to 3
move 7 from 2 to 9
move 6 from 9 to 8
move 1 from 3 to 4
move 3 from 7 to 6
move 2 from 2 to 1
move 1 from 4 to 7
move 2 from 2 to 5
move 1 from 9 to 6
move 2 from 2 to 5
move 2 from 6 to 2
move 4 from 5 to 4
move 5 from 2 to 6
move 1 from 1 to 7
move 1 from 1 to 2
move 13 from 8 to 1
move 2 from 8 to 4
move 19 from 7 to 4
move 3 from 1 to 6
move 11 from 4 to 3
move 2 from 7 to 9
move 4 from 2 to 5
move 2 from 9 to 5
move 1 from 7 to 4
move 2 from 5 to 7
move 4 from 3 to 4
move 3 from 4 to 1
move 3 from 5 to 1
move 9 from 6 to 4
move 1 from 7 to 9
move 1 from 7 to 5
move 10 from 1 to 4
move 1 from 9 to 6
move 1 from 6 to 8
move 32 from 4 to 5
move 7 from 5 to 4
move 27 from 5 to 9
move 5 from 3 to 2
move 3 from 2 to 8
move 1 from 6 to 2
move 8 from 4 to 9
move 1 from 2 to 9
move 8 from 8 to 6
move 2 from 4 to 3
move 1 from 2 to 3
move 15 from 9 to 8
move 4 from 1 to 4
move 3 from 4 to 8
move 6 from 9 to 7
move 1 from 4 to 9
move 8 from 8 to 2
move 2 from 1 to 9
move 2 from 7 to 9
move 10 from 8 to 3
move 6 from 2 to 6
move 2 from 3 to 2
move 6 from 6 to 3
move 1 from 7 to 5
move 8 from 3 to 2
move 4 from 3 to 2
move 1 from 3 to 5
move 6 from 6 to 1
move 4 from 3 to 7
move 2 from 5 to 8
move 3 from 7 to 5
move 6 from 1 to 7
move 1 from 3 to 4
move 1 from 3 to 9
move 10 from 7 to 4
move 8 from 2 to 8
move 11 from 9 to 5
move 11 from 4 to 1
move 5 from 2 to 6
move 3 from 2 to 7
move 11 from 1 to 6
move 1 from 5 to 6
move 8 from 5 to 4
move 19 from 6 to 7
move 3 from 7 to 9
move 3 from 5 to 4
move 1 from 2 to 5
move 3 from 5 to 7
move 8 from 9 to 6
move 2 from 4 to 1
move 1 from 1 to 9
move 2 from 9 to 7
move 6 from 6 to 2
move 2 from 4 to 6
move 4 from 8 to 6
move 1 from 8 to 1
move 7 from 6 to 7
move 1 from 9 to 4
move 5 from 8 to 4
move 3 from 2 to 6
move 4 from 6 to 4
move 2 from 9 to 6
move 3 from 2 to 9
move 16 from 4 to 8
move 1 from 6 to 8
move 2 from 9 to 5
move 1 from 9 to 7
move 2 from 5 to 2
move 1 from 4 to 6
move 2 from 2 to 5
move 1 from 9 to 6
move 3 from 7 to 3
move 7 from 7 to 8
move 2 from 7 to 1
move 3 from 8 to 5
move 3 from 6 to 2
move 4 from 7 to 4
move 1 from 5 to 1
move 1 from 5 to 7
move 3 from 3 to 4
move 5 from 1 to 4
move 16 from 7 to 2
move 5 from 4 to 7
move 19 from 8 to 1
move 11 from 2 to 9
move 11 from 9 to 6
move 2 from 1 to 6
move 2 from 4 to 1
move 5 from 4 to 6
move 1 from 5 to 9
move 1 from 9 to 6
move 2 from 2 to 6
move 1 from 5 to 4
move 8 from 6 to 5
move 16 from 1 to 6
move 1 from 4 to 9
move 3 from 2 to 9
move 2 from 2 to 5
move 2 from 5 to 8
move 4 from 8 to 4
move 4 from 9 to 7
move 2 from 1 to 3
move 5 from 6 to 4
move 21 from 6 to 2
move 9 from 7 to 3
move 1 from 1 to 2
move 1 from 5 to 3
move 23 from 2 to 7
move 1 from 7 to 5
move 3 from 6 to 1
move 9 from 4 to 5
move 11 from 7 to 1
move 2 from 3 to 4
move 1 from 3 to 7
move 1 from 4 to 1
move 10 from 1 to 6
move 5 from 7 to 1
move 3 from 1 to 4
move 7 from 1 to 7
move 4 from 3 to 8
move 4 from 7 to 4
move 5 from 7 to 3
move 2 from 4 to 9
move 1 from 8 to 1
move 4 from 4 to 1
move 1 from 6 to 1
move 1 from 6 to 5
move 16 from 5 to 1
move 2 from 5 to 7
move 1 from 5 to 6
move 2 from 8 to 2
move 1 from 7 to 9
move 3 from 9 to 5
move 2 from 5 to 4
move 6 from 7 to 1
move 3 from 4 to 7
move 1 from 8 to 6
move 5 from 1 to 4
move 1 from 6 to 1
move 19 from 1 to 5
move 1 from 7 to 6
move 9 from 3 to 1
move 6 from 6 to 5
move 4 from 6 to 9
move 3 from 9 to 4
move 13 from 1 to 4
move 1 from 3 to 1
move 2 from 5 to 1
move 1 from 2 to 3
move 1 from 3 to 9
move 4 from 5 to 4
move 1 from 2 to 3
move 1 from 3 to 5
move 1 from 9 to 1
move 1 from 9 to 5
move 19 from 4 to 7
move 4 from 1 to 6
move 5 from 4 to 3
move 3 from 6 to 1
move 1 from 6 to 8
move 2 from 1 to 6
move 2 from 1 to 7
move 2 from 6 to 3
move 2 from 3 to 1
move 8 from 7 to 6
move 5 from 3 to 9
move 2 from 4 to 9
move 2 from 6 to 8
move 10 from 7 to 2
move 7 from 2 to 9
move 1 from 8 to 9
move 1 from 1 to 2
move 2 from 9 to 3
move 2 from 8 to 7
move 1 from 1 to 6
move 1 from 2 to 8
move 2 from 2 to 5
move 4 from 5 to 7
move 5 from 6 to 1
move 1 from 3 to 4

36
2022/d06/ex1/ex1.py Executable file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python
import collections
import itertools
import sys
from collections.abc import Iterable
from typing import TypeVar
T = TypeVar("T")
def sliding_window(iterable: Iterable[T], n: int) -> Iterable[tuple[T, ...]]:
it = iter(iterable)
window = collections.deque(itertools.islice(it, n), maxlen=n)
if len(window) == n:
yield tuple(window)
for x in it:
window.append(x)
yield tuple(window)
def solve(input: str) -> int:
SIZE = 4
for i, tup in enumerate(sliding_window(input, SIZE), start=SIZE):
if len(set(tup)) == SIZE:
return i
assert False
def main() -> None:
input = sys.stdin.read().replace("\n", "")
print(solve(input))
if __name__ == "__main__":
main()

1
2022/d06/ex1/input Normal file
View file

@ -0,0 +1 @@
zcncrcnrrlccmhchssgqsqrsstfffnqfnfsswgwjjcmcnnvjvwjvwvfvnvwwhvvwmwhmwwwhbhhldhdmhhdfdbdfbdblllnfllgslllqhlhfhlhqllncnfnsffwmwzzlglzlwlhwwmvmddpvvbrvrdvrdvdhhzdhdpdzzbgbsssrpsrshrshhbqbgbmmtwwcbcrbbvnvhnncscwcwlcwlwnwswbbnwntthzzdlzdzffvqvdqvvtptdptphpddzzvhzvzwwqgqjjjvsspvspsbsffvpvcvjcvjvrvjjgmjjhrrdhhdvhvttjttzptpssnlnjjlnnhmnndjnjwwtjjtbbfflwfwgwvvpjpwpcwcgglmgmdmnmbbqtqctcwcmwwvmvlmvmdvdvhvlhlzhzttghttnvnjntnqqtmtwwhvhrhfhchqchhdrdllnwnfwfjjmsmmnhhshnhpptstjjpnpzzhmmpssgrsrzsrrffzjjhvjhjcjcpcvpccfnfjnfjfllssrdsdnnmffldldppcctcmtctffprpsrrrfhfmflldccnpppvsswppdgpgjpgpqgpqpjppclcjczcnzzzqvzzwlzzqfzffsqqphqhvqqbcccstsdttwcwcvvmjjhqqmllpglgtgwgnnbnrbnnjdndhhbrbvrvwwwblwwwppmdmttztqzqrqjjpggpmgpmpqqmssvnsvnndnvddtztddrrsnsbbshhqmhqmmdnnrsrllqvvfjfpfqpqfqflqlmqmrrqgrqrrhvvfddfhhfnhnlncnzzwwjwswpsscpchcssrzssjqsqrsswbbzggnqnwqqsrqqzbqbddtffcjjdggwlwnwtnnpddcqddsppcwpcwpphvphhhrprqrqttwgwqwppvdpvvrgggmpmddzvvwwthwtttbqtbbftfrffbdfbbspshphpwhppnhnmhhbbshbsbmbdmmtrmrbrprbprphhnmhmvhmhhqqbvqvqcvcwcbwcchbbgwwqffrcffsvvcczrztrzzdhdmhdmmzbbvlvqqtptpvvfsfppdzppmddfvdfdwdfdmdwwlvvqdvqdqmdqqmnmdndsswsfwfvwfwzzhrzhhwfhhpvhppmssnhsnsnwswvwlwdwzwnwswwmqmjmbbdjdbbtllcpllltqlttnvvppznpnttlrrwlwvllphpbblccgjcggbtbwtwdwrrbnndpddbqqnpnrnnqqshspsggtptztpzzclcggtqtgqttcmmbgbfbdfdfsddlrlvrlvvmqqgbqgbqbcbbnpplqqblqbqmbbbdqbbhjjcgcdgdwdjwjqqlsqqnnwffglfglgnlggjgjtgjtgjtgjjclcwcqqvpvwwvgvhgvhvjjzpzbznnvrrrpbpdbdhhmshsnnnwppcbcvvlldccdggqnqgnqqzbzzcfflnnmppcgcmgmrgmrgmgngwwptpzzpfzpfzpzdppgcgtcgtgccvtctzzdmzmlmzlzwllbplblltgtctrrghgvhgvvfvssngsschshrhvrvddghgwwwwzgwgvgccjdjzjwwvdvvqsqshsvvddmddbllvppmlpmlmwlwppjmppwrrdzrrpmrmrdmrmbmzzwttvwwsfswwlflnlrlvvdllzbztbtpbtppnjpnpdnnjrjsshtssvvsjvjfjwffsszvsvjssqzsslpslppjpspqptbncvzrlwtjvsrwtnzzhwdfsmlthvgqgjrpshpbsrrvnsdbqslbcplnpcjqwmwqsnwdcjsdmccbdglwbrcdcqsfhjqhstvhqqdwltqwhhqcrnpvnzjhhbjvqbqhclwggjqfvnfsvcnjjhbmrvbpjqrbljbtltvnsgdfhddlmsdhcrfwvlvbsdrjwjvtnqzhrlqgjmzsmjlpdjsrjmdhmvgwjmfwtqffnzfrtswrlgvvhhqgpzcjwscfqgjmdhtvbgzdlvzfhgqlqbfwsjrmmhrlcwhrcnwwvngcmsrfgczsfqvvmdtmtprfvjrwrwcqwvgmzcjncrzvcswlzsdszvdtwmptnrhgzqwrhjjtbchhpwsdjnqmnsgzwqzvlzlsznpqgvtqnldjqpvndtsjlzhpzsgthbwvwnlbwjlmndqpcdvjdgdzhctpghlfwrtqtvfwdpgrjbmwzqgthjpmlrsqmzsznddhrbjnggqrdntpbngvldnnltfnmdwfhftjvpqbrzqvdzbzzctshzldtcdgfnczglrrjtwswzdvjrfgwztwznbpplmbgwpcmstcsjtqhmzmzsjwsfbjlbnbdtdsmlpdmrrbhdhpzrjdpzhcwsrgfrhmqzqtjfhpvltnthwjrrrpnsbmmwrhsfqbmnvwhpntltsgwgnqhcvlndfrtrfrnlmbhltmtgzhlzqgtsbbnggdjvbslfbczhpghqqcqlrbtpnbqbflfjrmpmwwvjqgvcqtmfggmptqlqstcmdtqlslnnzwbgnstftfsvsjdrmgbzfnzltwbjmqhsvshnmwhftjdndltdpngzwbrjpgpwmqgfsflnhmtzcmdjmzwrsrrrmpvwgggwrhrwtfwdbgbpmwpcdspdtbqvdwwsnwdtrtdtgnfzzsmlbcqdzbsqnrgtvvnlfcdlcgcnfpqbddqcjfqtmpndmnwvfqgjzrltqvlprmbbhmtwbzjjgfhhhfjswpffmjnsdmrcjrwlwpmfrmhljpphlwwwwmgsjcsrcvmfrdjdhshddshpplzsnsphcmdhvllgmdgrvbvjmtpltdthffsvwwhvgqrhmfjfpdswcqldrhpmznffjsntwrnmnpmsshljszbchctptsdlnbcvpfvtlfnzcrljpdwrsjnlpqcpnwvnqhzhqmjbvlbtgslzthlbbjzsgdglbrltzjdshpfbndjtssvsjqlstnrjdzzjvlpqhmwvrsvndcqrqjjcsqvmvrbhngtcfdprlbnqmhqllddgjpdzbjlphntrtgjrdgtbslrtzczbnnlddzzsvqvqvvzjpjqfhztgtsfggdppfdhzsbjzqjmpnmgqzlsdhjjbfpbsbnzpmhwrzjqhczrgcsflfwtrgwbnbrshjpwltntsnsdhmhqlmzdprcrcpcpjnphsmjwhzdqtncdbwgspmnfzsgmpbdhmslqchhhbbwfrghhnfjplsvrtbvplgrwdnbnfsgpwrqczvzlnfsngnsnbwvpmfdcmjztdnrllslnwcfwwnwsvztqmgqtfvmdqrrrmwfmphbcvwwttpmwjjbvqrmlwtwfsjdpcbmdlnlzcqntfzzmslshwprjfhwwpbbdfcdjwllfwcznwpjpwrlsfnnbgzjllrgtzcdcvhdhbtlrcvfdvsdjlzsmwwqvpzfhzjlqpfbqstvfrpcchmtwgbrhqqbglrvzmctdlpnvmglgdtzpbdngtfdnmsmwbgjstzbqwqcdlhfrtqqnhqvpfhdrjqvsvstftdgwwnwpfbfbdcfqnqlwpdnfhhfctwrgdqpbpbmgnfsnbpjfctvdtjnsfqlrtctrnjgltndngcmrdphhsqpjhprbngjzqqhnhhrdwlwwpmhzwshvrtzfgzlrhwghvpvfprbbvflltplpptvrmwcrdqndfqbfqtlqqwvphsmcvnbzghvsptrphhfcgdsslhfbcwhtjcmnpbvqrfgpsjgqpnnwwhjjwqrhhqgznwdzjqbtmmjljjwctqtfgwqbdrjwqwbbcftvjwfdfrgvsrlcrccpvfzdrcjvqfbhddpvrrhjrmhdgchrghbzsqpmgnmslfctblwlvphdfpvtdtwpdfsjwssmgnsvsqpdbqngccsplhmjbwjwtzwsbjhwpwcslqjdchmbvzrbgnwvjrrrdtvhtlzlrbwthzlhhqzzpvpwbzrrbrbtpwnhldhqqltqrqdddfwdmjzgctnlrjrjwvddfmjpnptdmrvnqjvsjfrmlvlqsthhsbvnjlsdzrjngfnqdjfssmvgrchbwmwbbvfqfhvrtwghmrpddnwbrbvbmqvfzbjdsnbzgrtmsfhmsmjtrqsgmpnwwbfwtp

36
2022/d06/ex2/ex2.py Executable file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python
import collections
import itertools
import sys
from collections.abc import Iterable
from typing import TypeVar
T = TypeVar("T")
def sliding_window(iterable: Iterable[T], n: int) -> Iterable[tuple[T, ...]]:
it = iter(iterable)
window = collections.deque(itertools.islice(it, n), maxlen=n)
if len(window) == n:
yield tuple(window)
for x in it:
window.append(x)
yield tuple(window)
def solve(input: str) -> int:
SIZE = 14
for i, tup in enumerate(sliding_window(input, SIZE), start=SIZE):
if len(set(tup)) == SIZE:
return i
assert False
def main() -> None:
input = sys.stdin.read().replace("\n", "")
print(solve(input))
if __name__ == "__main__":
main()

1
2022/d06/ex2/input Normal file
View file

@ -0,0 +1 @@
zcncrcnrrlccmhchssgqsqrsstfffnqfnfsswgwjjcmcnnvjvwjvwvfvnvwwhvvwmwhmwwwhbhhldhdmhhdfdbdfbdblllnfllgslllqhlhfhlhqllncnfnsffwmwzzlglzlwlhwwmvmddpvvbrvrdvrdvdhhzdhdpdzzbgbsssrpsrshrshhbqbgbmmtwwcbcrbbvnvhnncscwcwlcwlwnwswbbnwntthzzdlzdzffvqvdqvvtptdptphpddzzvhzvzwwqgqjjjvsspvspsbsffvpvcvjcvjvrvjjgmjjhrrdhhdvhvttjttzptpssnlnjjlnnhmnndjnjwwtjjtbbfflwfwgwvvpjpwpcwcgglmgmdmnmbbqtqctcwcmwwvmvlmvmdvdvhvlhlzhzttghttnvnjntnqqtmtwwhvhrhfhchqchhdrdllnwnfwfjjmsmmnhhshnhpptstjjpnpzzhmmpssgrsrzsrrffzjjhvjhjcjcpcvpccfnfjnfjfllssrdsdnnmffldldppcctcmtctffprpsrrrfhfmflldccnpppvsswppdgpgjpgpqgpqpjppclcjczcnzzzqvzzwlzzqfzffsqqphqhvqqbcccstsdttwcwcvvmjjhqqmllpglgtgwgnnbnrbnnjdndhhbrbvrvwwwblwwwppmdmttztqzqrqjjpggpmgpmpqqmssvnsvnndnvddtztddrrsnsbbshhqmhqmmdnnrsrllqvvfjfpfqpqfqflqlmqmrrqgrqrrhvvfddfhhfnhnlncnzzwwjwswpsscpchcssrzssjqsqrsswbbzggnqnwqqsrqqzbqbddtffcjjdggwlwnwtnnpddcqddsppcwpcwpphvphhhrprqrqttwgwqwppvdpvvrgggmpmddzvvwwthwtttbqtbbftfrffbdfbbspshphpwhppnhnmhhbbshbsbmbdmmtrmrbrprbprphhnmhmvhmhhqqbvqvqcvcwcbwcchbbgwwqffrcffsvvcczrztrzzdhdmhdmmzbbvlvqqtptpvvfsfppdzppmddfvdfdwdfdmdwwlvvqdvqdqmdqqmnmdndsswsfwfvwfwzzhrzhhwfhhpvhppmssnhsnsnwswvwlwdwzwnwswwmqmjmbbdjdbbtllcpllltqlttnvvppznpnttlrrwlwvllphpbblccgjcggbtbwtwdwrrbnndpddbqqnpnrnnqqshspsggtptztpzzclcggtqtgqttcmmbgbfbdfdfsddlrlvrlvvmqqgbqgbqbcbbnpplqqblqbqmbbbdqbbhjjcgcdgdwdjwjqqlsqqnnwffglfglgnlggjgjtgjtgjtgjjclcwcqqvpvwwvgvhgvhvjjzpzbznnvrrrpbpdbdhhmshsnnnwppcbcvvlldccdggqnqgnqqzbzzcfflnnmppcgcmgmrgmrgmgngwwptpzzpfzpfzpzdppgcgtcgtgccvtctzzdmzmlmzlzwllbplblltgtctrrghgvhgvvfvssngsschshrhvrvddghgwwwwzgwgvgccjdjzjwwvdvvqsqshsvvddmddbllvppmlpmlmwlwppjmppwrrdzrrpmrmrdmrmbmzzwttvwwsfswwlflnlrlvvdllzbztbtpbtppnjpnpdnnjrjsshtssvvsjvjfjwffsszvsvjssqzsslpslppjpspqptbncvzrlwtjvsrwtnzzhwdfsmlthvgqgjrpshpbsrrvnsdbqslbcplnpcjqwmwqsnwdcjsdmccbdglwbrcdcqsfhjqhstvhqqdwltqwhhqcrnpvnzjhhbjvqbqhclwggjqfvnfsvcnjjhbmrvbpjqrbljbtltvnsgdfhddlmsdhcrfwvlvbsdrjwjvtnqzhrlqgjmzsmjlpdjsrjmdhmvgwjmfwtqffnzfrtswrlgvvhhqgpzcjwscfqgjmdhtvbgzdlvzfhgqlqbfwsjrmmhrlcwhrcnwwvngcmsrfgczsfqvvmdtmtprfvjrwrwcqwvgmzcjncrzvcswlzsdszvdtwmptnrhgzqwrhjjtbchhpwsdjnqmnsgzwqzvlzlsznpqgvtqnldjqpvndtsjlzhpzsgthbwvwnlbwjlmndqpcdvjdgdzhctpghlfwrtqtvfwdpgrjbmwzqgthjpmlrsqmzsznddhrbjnggqrdntpbngvldnnltfnmdwfhftjvpqbrzqvdzbzzctshzldtcdgfnczglrrjtwswzdvjrfgwztwznbpplmbgwpcmstcsjtqhmzmzsjwsfbjlbnbdtdsmlpdmrrbhdhpzrjdpzhcwsrgfrhmqzqtjfhpvltnthwjrrrpnsbmmwrhsfqbmnvwhpntltsgwgnqhcvlndfrtrfrnlmbhltmtgzhlzqgtsbbnggdjvbslfbczhpghqqcqlrbtpnbqbflfjrmpmwwvjqgvcqtmfggmptqlqstcmdtqlslnnzwbgnstftfsvsjdrmgbzfnzltwbjmqhsvshnmwhftjdndltdpngzwbrjpgpwmqgfsflnhmtzcmdjmzwrsrrrmpvwgggwrhrwtfwdbgbpmwpcdspdtbqvdwwsnwdtrtdtgnfzzsmlbcqdzbsqnrgtvvnlfcdlcgcnfpqbddqcjfqtmpndmnwvfqgjzrltqvlprmbbhmtwbzjjgfhhhfjswpffmjnsdmrcjrwlwpmfrmhljpphlwwwwmgsjcsrcvmfrdjdhshddshpplzsnsphcmdhvllgmdgrvbvjmtpltdthffsvwwhvgqrhmfjfpdswcqldrhpmznffjsntwrnmnpmsshljszbchctptsdlnbcvpfvtlfnzcrljpdwrsjnlpqcpnwvnqhzhqmjbvlbtgslzthlbbjzsgdglbrltzjdshpfbndjtssvsjqlstnrjdzzjvlpqhmwvrsvndcqrqjjcsqvmvrbhngtcfdprlbnqmhqllddgjpdzbjlphntrtgjrdgtbslrtzczbnnlddzzsvqvqvvzjpjqfhztgtsfggdppfdhzsbjzqjmpnmgqzlsdhjjbfpbsbnzpmhwrzjqhczrgcsflfwtrgwbnbrshjpwltntsnsdhmhqlmzdprcrcpcpjnphsmjwhzdqtncdbwgspmnfzsgmpbdhmslqchhhbbwfrghhnfjplsvrtbvplgrwdnbnfsgpwrqczvzlnfsngnsnbwvpmfdcmjztdnrllslnwcfwwnwsvztqmgqtfvmdqrrrmwfmphbcvwwttpmwjjbvqrmlwtwfsjdpcbmdlnlzcqntfzzmslshwprjfhwwpbbdfcdjwllfwcznwpjpwrlsfnnbgzjllrgtzcdcvhdhbtlrcvfdvsdjlzsmwwqvpzfhzjlqpfbqstvfrpcchmtwgbrhqqbglrvzmctdlpnvmglgdtzpbdngtfdnmsmwbgjstzbqwqcdlhfrtqqnhqvpfhdrjqvsvstftdgwwnwpfbfbdcfqnqlwpdnfhhfctwrgdqpbpbmgnfsnbpjfctvdtjnsfqlrtctrnjgltndngcmrdphhsqpjhprbngjzqqhnhhrdwlwwpmhzwshvrtzfgzlrhwghvpvfprbbvflltplpptvrmwcrdqndfqbfqtlqqwvphsmcvnbzghvsptrphhfcgdsslhfbcwhtjcmnpbvqrfgpsjgqpnnwwhjjwqrhhqgznwdzjqbtmmjljjwctqtfgwqbdrjwqwbbcftvjwfdfrgvsrlcrccpvfzdrcjvqfbhddpvrrhjrmhdgchrghbzsqpmgnmslfctblwlvphdfpvtdtwpdfsjwssmgnsvsqpdbqngccsplhmjbwjwtzwsbjhwpwcslqjdchmbvzrbgnwvjrrrdtvhtlzlrbwthzlhhqzzpvpwbzrrbrbtpwnhldhqqltqrqdddfwdmjzgctnlrjrjwvddfmjpnptdmrvnqjvsjfrmlvlqsthhsbvnjlsdzrjngfnqdjfssmvgrchbwmwbbvfqfhvrtwghmrpddnwbrbvbmqvfzbjdsnbzgrtmsfhmsmjtrqsgmpnwwbfwtp

69
2022/d07/ex1/ex1.py Executable file
View file

@ -0,0 +1,69 @@
#!/usr/bin/env python
import sys
from typing import Union
FileSystem = dict[str, Union[int, "FileSystem"]]
def solve(input: list[str]) -> int:
def build_tree(input: list[str], i: int = 0) -> tuple[FileSystem, int]:
fs: FileSystem = {}
while i < len(input):
assert input[i][0] == "$" # Sanity check
command = input[i].split()[1:]
if command[0] == "ls":
while (i := i + 1) < len(input):
if input[i][0] == "$":
break
else:
type, name = input[i].split()
if type == "dir":
continue
fs[name] = int(type)
elif command[0] == "cd":
if command[1] == "..":
i += 1
break
else:
fs[command[1]], i = build_tree(input, i + 1)
else:
assert False # Sanity check
return fs, i
def total_size(
fs: FileSystem, parent_path: str = ""
) -> dict[str, tuple[int, bool]]:
sizes: dict[str, tuple[int, bool]] = {}
for f, content in fs.items():
path = f"{parent_path}/{f}"
if isinstance(content, int):
sizes[path] = content, False
elif isinstance(content, dict):
children_sizes = total_size(content, path)
total = sum(children_sizes[f"{path}/{c}"][0] for c in content.keys())
sizes[path] = total, True
sizes.update(children_sizes)
else:
assert False # Sanity check
return sizes
fs, i = build_tree(input)
assert i == len(input) # Sanity check
sizes = total_size(fs)
THRESHOLD = 100000
return sum(size for size, is_dir in sizes.values() if is_dir and size <= THRESHOLD)
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

1010
2022/d07/ex1/input Normal file

File diff suppressed because it is too large Load diff

70
2022/d07/ex2/ex2.py Executable file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env python
import sys
from typing import Union
FileSystem = dict[str, Union[int, "FileSystem"]]
def solve(input: list[str]) -> int:
def build_tree(input: list[str], i: int = 0) -> tuple[FileSystem, int]:
fs: FileSystem = {}
while i < len(input):
assert input[i][0] == "$" # Sanity check
command = input[i].split()[1:]
if command[0] == "ls":
while (i := i + 1) < len(input):
if input[i][0] == "$":
break
else:
type, name = input[i].split()
if type == "dir":
continue
fs[name] = int(type)
elif command[0] == "cd":
if command[1] == "..":
i += 1
break
else:
fs[command[1]], i = build_tree(input, i + 1)
else:
assert False # Sanity check
return fs, i
def total_size(
fs: FileSystem, parent_path: str = ""
) -> dict[str, tuple[int, bool]]:
sizes: dict[str, tuple[int, bool]] = {}
for f, content in fs.items():
path = f"{parent_path}/{f}"
if isinstance(content, int):
sizes[path] = content, False
elif isinstance(content, dict):
children_sizes = total_size(content, path)
total = sum(children_sizes[f"{path}/{c}"][0] for c in content.keys())
sizes[path] = total, True
sizes.update(children_sizes)
else:
assert False # Sanity check
return sizes
fs, i = build_tree(input)
assert i == len(input) # Sanity check
sizes = total_size(fs)
THRESHOLD = 30000000 - (70000000 - sizes["//"][0])
assert THRESHOLD > 0 # Sanity check
return min(size for size, is_dir in sizes.values() if is_dir and size >= THRESHOLD)
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

1010
2022/d07/ex2/input Normal file

File diff suppressed because it is too large Load diff

55
2022/d08/ex1/ex1.py Executable file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env python
import dataclasses
import sys
from collections.abc import Iterator
@dataclasses.dataclass
class Point:
x: int
y: int
def solve(input: list[list[int]]) -> int:
def up(p: Point) -> Iterator[Point]:
x = p.x
while (x := x - 1) >= 0:
yield Point(x, p.y)
def down(p: Point) -> Iterator[Point]:
x = p.x
while (x := x + 1) < len(input):
yield Point(x, p.y)
def left(p: Point) -> Iterator[Point]:
y = p.y
while (y := y - 1) >= 0:
yield Point(p.x, y)
def right(p: Point) -> Iterator[Point]:
y = p.y
while (y := y + 1) < len(input[0]):
yield Point(p.x, y)
def is_visible(p: Point) -> bool:
height = input[p.x][p.y]
for neighbours in (up, down, left, right):
if all(input[n.x][n.y] < height for n in neighbours(p)):
return True
return False
visibility = [
[is_visible(Point(x, y)) for y in range(len(input[x]))]
for x in range(len(input))
]
return sum(map(sum, visibility))
def main() -> None:
input = [[int(c) for c in line] for line in sys.stdin.read().splitlines()]
print(solve(input))
if __name__ == "__main__":
main()

99
2022/d08/ex1/input Normal file
View file

@ -0,0 +1,99 @@
404310113342042430523206553054613026452223317713352040314520532034136044035422242052404520323121423
120022131035104020413264056552251024207112052153646716635163015540126546334604634352130343204203212
114444043404350333465355412140536532335724647116565610572546474626506052303052063065052530351012030
212240414554223531015555065165021520410363021033163304514155663733352116600665403462501544352500033
324144130111224105125305614015103771206061460425505011635353313773273343325122626415203015344311232
113245430241150240464126012614135430370003075735010164413125456766205302613425655024334411031155403
441245002130523346005210411662277072426351216071200446765701447237330324567025204602342520044502121
214045553122025654652221354616163561012500762010384874262074144065234401637722263412266464251452314
420554052436551160102504245073471242400253801258004372606111667766236046654353552356241642235535400
230444034403653045355316273427144521155460537330375254634265217831710133466511570022515061342332314
215330305636623403455371527067601715250678103470620507380025232122252031461570076036342312211025412
152531154664324333510065716166433534816054420634864077383448250450766671674224724566465113516230555
214332343546262452365454705617472281546633211772075171047360414071436550632720021764434561305210301
111443005611044613477737043746056557178022072372401206038184673647422320773366100676006211551510212
133313521334520014277727658110473505777283286103991788280475205774027243280366061162025220015443455
155404252050544375401645717463634684248661783289138494563725362778686756122861113531553244022156404
101264441110131756443257557872335606716942314559648922296471179061870856246756574324520340610552502
552345216103355364312158072306454410946149736569151493634487672710443362287688034427626033035266435
032116136355447236216431376728258285654613995348237354962282338329733408310145842020562245054642441
224363655451243141363087456580326751652471257429153117112342588642859256252141665722503521125022565
203656402177710126565314466702484652152875481365818345352121487714962821315157703464343750046056642
556042122224622324270101114885994351144861589211656115175234527658398149331284812500044434453264665
333402317273401421323318415855844499641764213948757537893227517242385296970317342824206711503004500
341045445002443367084450562962259886574728664877676766689484955395194385797583863611332250124025511
530441155216276525375870173389743919985448259629562325456237285529598688811050240824703721074602060
320021441136216827813607342916845194696625696785568454728524752386544154132864251748117643644612016
356236015127477502168551236473444979246675949449587984562945859468987964451443376640177554302403555
521032161450418348764563917129159623593365988455562286236885389659621188427923285451742156213351116
161327255665606701821864175493119539272489637754729983857664245665796727199928145136462257620362465
324256011240774073378929732467468357478524626995373968674237767743387885552216853467388044247756643
342275312602047038565615877434724873572784389557789777857693948752846554139723438258054200025014665
051242516637708146638198373275222758924486548779744574439965559638384373152521363134036477637737533
643152555635778437871298236855485688298578349488385645847564753765967563925377954286572876073532255
052252355510464443878597562686999366353446874577654743895687373779864325297372128203202721414431575
003626454683627045725543215594452394956436955758673339474559874959389629573279978978206522053161354
627516237702078544682942119397343558584845947957956986767739349699889797692213624665730454802067156
200457151510133551651137637224323537736454384779467644967339586954746668462789875763614758041306350
335154370506881211537946276895249658859666595888979499974679997597784647296794417997582782381025574
417013603706282285595672445598476844553485594594694865577988599545878778554434654896767653370600163
151076753567166396584462282676939868463579768879699478585698835334464668886671938943507604626741055
316424603540766436474549593426596456855354984858479546978868766769885893482223982736645038024543705
004215748043040351658449499986735473677885875695647795765956883964888467568526739498861211513560376
277541056808037332121324298298467888439897499685669789559864468637637659443647357982947036001076457
301501750655275336942638945358964763536465468989488667796645858846573465972686416787340853151245644
712676410408042175645992725856638455389978885467555675756857577896385395967245722641824067046404672
127263578732604583334129426765554856475867757556696788664596649687355745252374587569983167874135470
372567082480204357437552435378439546954796599589886886999448548554873566373887522667838861416626063
504053336576743468622556295365645349984889785678558796559694969799447958394395964464961434514564147
064634031154473874813798648457894764786965996797567598577449556796733655629459883126531875350855667
400242382466736377478123774763774843994888788596767997699956676943467867843525339987287625323107646
574363428370183567442332638863797996446594766875586998755697498794589458457898531351415551433522556
412167557672308712367589426397979758975789888776586986996554696687355758873775853874897420702317101
770303703378804536976332878328747387584568459887597696678765458466869646823658768383568726667515101
452233157246118285247999798264378649666585777768676869779576767654376979948352929633336613662437752
445720367378533569416697754293496373467957556998785966978548459859767957423835655379853826412537652
743314715884636853952588482996464655775879867757788958998964888966363788982234644268286248732605364
421252340411081749754123523897998635856478675875579877695848758757598799352859935972592464121200154
240060534144421235633966383826944588955556569877797756857998594637769364656894472644372534212151333
030406408132303595787173445575575686448657959549968444868588895645466777395756386243943041550563144
273114334761050642652349846942498578754978859865947785499768464999467827979546133588431400553072571
545140256541065084978642274659978546685549897854997987959494546787556536925383718147440765477035264
213551156551872738118554524476466757347767578664548796858878373885346253453663631131187832513353720
031302225003187455433275258559853476398764745884957978785699446894568423546775622669458815340346707
622067430504086108575211657573256737949367744989999457768543933556446848583667956928715425470730162
242111564658304088777519536233444248435478893367596684875833738467857765957226924427228481325217576
364055473230737526383625952385867723976677963779587753646766774437577338682519314383352032477133503
321304645532064452197673316923724578983475574649393989787463485775923766996461422676353215802454115
233105015700144131749152169592849597799334664843639757588555464399727868723189114371617811204424461
200116461554525361612654498296559659356483355845588365383658756592935783639172813284867400343573434
063667611325433422432889612288938865635845895684777885359355942764253445159748552521586663111276320
602227464555468655756429585352455445365885785394937349896765628578363566623445953670505237256637361
565061226117741561827365137945864958743698627664964883854386585344264536463794276685706331233201162
022104543457075344433186746554898622888662879483534875732366545699493494249416960261188772111451356
436620604222365732527426815953936962792895744565572436255439745543587676567692850112721235162354066
131524412733104587364628695894553856954929535227798584623638988936368718849285573225440174764501540
542435561362065510830610739646131257342496749563278998673868857392617956152266780580611770145235126
403602071007077303754367568233894922487258962426625788335236293726841614813262288087316403413301325
653365633721421431240238511233988284565865357732839935658824268124333235971843281322245237465244640
306442137750441441547377231538498282333378867895866774269433657996442236932105133474315101276433026
026362613577242456258634050671963436933444863559863424588962566983574869370120588454323506304034454
324415624033560610606167505705335537131257342423913353449125925341756615871358508731521557026114366
444331064667665623244155236812366396427328522481655365283852878951317624280585607560532023416160531
356100663130344012403254770320017162464663432371564992618187659799885888814177114132251720346235260
335255502200627412653317161700362276552642826696514719212976746877872577501286045667131741121615252
432342461115176570456177856545043415022291419917157172495246595828707718465323075462216735351424603
052014513044443630413221560860665817858636725794462229844935048786680202681024216170152252502634130
020320466436640542335164431658623622013245024685776848188641088056885388363367731111662103514422342
243313636453126262451331254501288620001007675817755413767166283281358125642646705337503156330264143
322052011503256425407127570260816542455843672731851386601352556410113100252536057366522542640013024
122243505446640664755657664365330525660042002157156113773258264460845541635501120634115505113335223
400325240315062161366031440416477741684364564857378773141186841270761406731633364031611642530523212
033035153153462216052744630367426463087060858055232515783208471176613722677126341566006053644200442
235202335236553544540304237444375273744171216804617062750281644425671760635002322246613220112112312
424551125551101552103143451563560236361153202606118476772167626160624534653670012136420410040115003
433115113101302300404521334255672566234211040710503455555073775502462316364453256460002143312311050
113231445145014641142635155170110005103066306721236312425606163376276006132006331601341420241440202
124001123533434316253543204160331101266365020140476416102251147360064153500112411356634011134351330
004034442142153246064264662030666771573157310340000724745700036734054206654603230345313434140344030
321402405400252420216445132115124427151235102635446313422013474177203430646340442422400002232222342

60
2022/d08/ex2/ex2.py Executable file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env python
import dataclasses
import sys
from collections.abc import Iterator
@dataclasses.dataclass
class Point:
x: int
y: int
def solve(input: list[list[int]]) -> int:
def up(p: Point) -> Iterator[Point]:
x = p.x
while (x := x - 1) >= 0:
yield Point(x, p.y)
def down(p: Point) -> Iterator[Point]:
x = p.x
while (x := x + 1) < len(input):
yield Point(x, p.y)
def left(p: Point) -> Iterator[Point]:
y = p.y
while (y := y - 1) >= 0:
yield Point(p.x, y)
def right(p: Point) -> Iterator[Point]:
y = p.y
while (y := y + 1) < len(input[0]):
yield Point(p.x, y)
def visible_trees(p: Point, neighbours: Iterator[Point]) -> Iterator[Point]:
height = input[p.x][p.y]
for n in neighbours:
yield n
if height <= input[n.x][n.y]:
break
def score(p: Point) -> int:
score = 1
for neighbours in (up, down, left, right):
score *= len(list(visible_trees(p, neighbours(p))))
return score
scores = [
[score(Point(x, y)) for y in range(len(input[x]))] for x in range(len(input))
]
return max(map(max, scores))
def main() -> None:
input = [[int(c) for c in line] for line in sys.stdin.read().splitlines()]
print(solve(input))
if __name__ == "__main__":
main()

99
2022/d08/ex2/input Normal file
View file

@ -0,0 +1,99 @@
404310113342042430523206553054613026452223317713352040314520532034136044035422242052404520323121423
120022131035104020413264056552251024207112052153646716635163015540126546334604634352130343204203212
114444043404350333465355412140536532335724647116565610572546474626506052303052063065052530351012030
212240414554223531015555065165021520410363021033163304514155663733352116600665403462501544352500033
324144130111224105125305614015103771206061460425505011635353313773273343325122626415203015344311232
113245430241150240464126012614135430370003075735010164413125456766205302613425655024334411031155403
441245002130523346005210411662277072426351216071200446765701447237330324567025204602342520044502121
214045553122025654652221354616163561012500762010384874262074144065234401637722263412266464251452314
420554052436551160102504245073471242400253801258004372606111667766236046654353552356241642235535400
230444034403653045355316273427144521155460537330375254634265217831710133466511570022515061342332314
215330305636623403455371527067601715250678103470620507380025232122252031461570076036342312211025412
152531154664324333510065716166433534816054420634864077383448250450766671674224724566465113516230555
214332343546262452365454705617472281546633211772075171047360414071436550632720021764434561305210301
111443005611044613477737043746056557178022072372401206038184673647422320773366100676006211551510212
133313521334520014277727658110473505777283286103991788280475205774027243280366061162025220015443455
155404252050544375401645717463634684248661783289138494563725362778686756122861113531553244022156404
101264441110131756443257557872335606716942314559648922296471179061870856246756574324520340610552502
552345216103355364312158072306454410946149736569151493634487672710443362287688034427626033035266435
032116136355447236216431376728258285654613995348237354962282338329733408310145842020562245054642441
224363655451243141363087456580326751652471257429153117112342588642859256252141665722503521125022565
203656402177710126565314466702484652152875481365818345352121487714962821315157703464343750046056642
556042122224622324270101114885994351144861589211656115175234527658398149331284812500044434453264665
333402317273401421323318415855844499641764213948757537893227517242385296970317342824206711503004500
341045445002443367084450562962259886574728664877676766689484955395194385797583863611332250124025511
530441155216276525375870173389743919985448259629562325456237285529598688811050240824703721074602060
320021441136216827813607342916845194696625696785568454728524752386544154132864251748117643644612016
356236015127477502168551236473444979246675949449587984562945859468987964451443376640177554302403555
521032161450418348764563917129159623593365988455562286236885389659621188427923285451742156213351116
161327255665606701821864175493119539272489637754729983857664245665796727199928145136462257620362465
324256011240774073378929732467468357478524626995373968674237767743387885552216853467388044247756643
342275312602047038565615877434724873572784389557789777857693948752846554139723438258054200025014665
051242516637708146638198373275222758924486548779744574439965559638384373152521363134036477637737533
643152555635778437871298236855485688298578349488385645847564753765967563925377954286572876073532255
052252355510464443878597562686999366353446874577654743895687373779864325297372128203202721414431575
003626454683627045725543215594452394956436955758673339474559874959389629573279978978206522053161354
627516237702078544682942119397343558584845947957956986767739349699889797692213624665730454802067156
200457151510133551651137637224323537736454384779467644967339586954746668462789875763614758041306350
335154370506881211537946276895249658859666595888979499974679997597784647296794417997582782381025574
417013603706282285595672445598476844553485594594694865577988599545878778554434654896767653370600163
151076753567166396584462282676939868463579768879699478585698835334464668886671938943507604626741055
316424603540766436474549593426596456855354984858479546978868766769885893482223982736645038024543705
004215748043040351658449499986735473677885875695647795765956883964888467568526739498861211513560376
277541056808037332121324298298467888439897499685669789559864468637637659443647357982947036001076457
301501750655275336942638945358964763536465468989488667796645858846573465972686416787340853151245644
712676410408042175645992725856638455389978885467555675756857577896385395967245722641824067046404672
127263578732604583334129426765554856475867757556696788664596649687355745252374587569983167874135470
372567082480204357437552435378439546954796599589886886999448548554873566373887522667838861416626063
504053336576743468622556295365645349984889785678558796559694969799447958394395964464961434514564147
064634031154473874813798648457894764786965996797567598577449556796733655629459883126531875350855667
400242382466736377478123774763774843994888788596767997699956676943467867843525339987287625323107646
574363428370183567442332638863797996446594766875586998755697498794589458457898531351415551433522556
412167557672308712367589426397979758975789888776586986996554696687355758873775853874897420702317101
770303703378804536976332878328747387584568459887597696678765458466869646823658768383568726667515101
452233157246118285247999798264378649666585777768676869779576767654376979948352929633336613662437752
445720367378533569416697754293496373467957556998785966978548459859767957423835655379853826412537652
743314715884636853952588482996464655775879867757788958998964888966363788982234644268286248732605364
421252340411081749754123523897998635856478675875579877695848758757598799352859935972592464121200154
240060534144421235633966383826944588955556569877797756857998594637769364656894472644372534212151333
030406408132303595787173445575575686448657959549968444868588895645466777395756386243943041550563144
273114334761050642652349846942498578754978859865947785499768464999467827979546133588431400553072571
545140256541065084978642274659978546685549897854997987959494546787556536925383718147440765477035264
213551156551872738118554524476466757347767578664548796858878373885346253453663631131187832513353720
031302225003187455433275258559853476398764745884957978785699446894568423546775622669458815340346707
622067430504086108575211657573256737949367744989999457768543933556446848583667956928715425470730162
242111564658304088777519536233444248435478893367596684875833738467857765957226924427228481325217576
364055473230737526383625952385867723976677963779587753646766774437577338682519314383352032477133503
321304645532064452197673316923724578983475574649393989787463485775923766996461422676353215802454115
233105015700144131749152169592849597799334664843639757588555464399727868723189114371617811204424461
200116461554525361612654498296559659356483355845588365383658756592935783639172813284867400343573434
063667611325433422432889612288938865635845895684777885359355942764253445159748552521586663111276320
602227464555468655756429585352455445365885785394937349896765628578363566623445953670505237256637361
565061226117741561827365137945864958743698627664964883854386585344264536463794276685706331233201162
022104543457075344433186746554898622888662879483534875732366545699493494249416960261188772111451356
436620604222365732527426815953936962792895744565572436255439745543587676567692850112721235162354066
131524412733104587364628695894553856954929535227798584623638988936368718849285573225440174764501540
542435561362065510830610739646131257342496749563278998673868857392617956152266780580611770145235126
403602071007077303754367568233894922487258962426625788335236293726841614813262288087316403413301325
653365633721421431240238511233988284565865357732839935658824268124333235971843281322245237465244640
306442137750441441547377231538498282333378867895866774269433657996442236932105133474315101276433026
026362613577242456258634050671963436933444863559863424588962566983574869370120588454323506304034454
324415624033560610606167505705335537131257342423913353449125925341756615871358508731521557026114366
444331064667665623244155236812366396427328522481655365283852878951317624280585607560532023416160531
356100663130344012403254770320017162464663432371564992618187659799885888814177114132251720346235260
335255502200627412653317161700362276552642826696514719212976746877872577501286045667131741121615252
432342461115176570456177856545043415022291419917157172495246595828707718465323075462216735351424603
052014513044443630413221560860665817858636725794462229844935048786680202681024216170152252502634130
020320466436640542335164431658623622013245024685776848188641088056885388363367731111662103514422342
243313636453126262451331254501288620001007675817755413767166283281358125642646705337503156330264143
322052011503256425407127570260816542455843672731851386601352556410113100252536057366522542640013024
122243505446640664755657664365330525660042002157156113773258264460845541635501120634115505113335223
400325240315062161366031440416477741684364564857378773141186841270761406731633364031611642530523212
033035153153462216052744630367426463087060858055232515783208471176613722677126341566006053644200442
235202335236553544540304237444375273744171216804617062750281644425671760635002322246613220112112312
424551125551101552103143451563560236361153202606118476772167626160624534653670012136420410040115003
433115113101302300404521334255672566234211040710503455555073775502462316364453256460002143312311050
113231445145014641142635155170110005103066306721236312425606163376276006132006331601341420241440202
124001123533434316253543204160331101266365020140476416102251147360064153500112411356634011134351330
004034442142153246064264662030666771573157310340000724745700036734054206654603230345313434140344030
321402405400252420216445132115124427151235102635446313422013474177203430646340442422400002232222342

96
2022/d09/ex1/ex1.py Executable file
View file

@ -0,0 +1,96 @@
#!/usr/bin/env python
import dataclasses
import enum
import sys
from typing import NamedTuple
def sign(n: int) -> int:
if n > 0:
return 1
elif n < 0:
return -1
return 0
class Direction(enum.Enum):
UP = "U"
DOWN = "D"
LEFT = "L"
RIGHT = "R"
@dataclasses.dataclass
class Move:
dir: Direction
len: int
@classmethod
def from_input(cls, line: str) -> "Move":
d, l = line.split()
return cls(Direction(d), int(l))
class Point(NamedTuple):
x: int
y: int
def move(self, direction: Direction) -> "Point":
match direction:
case Direction.UP:
dx, dy = 1, 0
case Direction.DOWN:
dx, dy = -1, 0
case Direction.LEFT:
dx, dy = 0, -1
case Direction.RIGHT:
dx, dy = 0, 1
return Point(self.x + dx, self.y + dy)
@dataclasses.dataclass
class Rope:
points: list[Point] = dataclasses.field(init=False)
_tail_positions: set[Point] = dataclasses.field(init=False)
def __init__(self, *, length: int = 2) -> None:
assert length > 0
self.points = [Point(0, 0)] * length
self._tail_positions = {self.points[-1]}
def move(self, move: Move) -> None:
for _ in range(move.len):
self.points[0] = self.points[0].move(move.dir)
for i in range(1, len(self.points)):
self.points[i] = self.__move_tail(self.points[i - 1], self.points[i])
self._tail_positions.add(self.points[-1])
@staticmethod
def __move_tail(head: Point, tail: Point) -> Point:
delta_x, delta_y = head.x - tail.x, head.y - tail.y
if abs(delta_x) <= 1 and abs(delta_y) <= 1:
return tail
dx, dy = sign(delta_x), sign(delta_y)
return Point(tail.x + dx, tail.y + dy)
def solve(input: list[str]) -> int:
moves = map(Move.from_input, input)
rope = Rope(length=2)
for move in moves:
rope.move(move)
return len(rope._tail_positions)
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

2000
2022/d09/ex1/input Normal file

File diff suppressed because it is too large Load diff

96
2022/d09/ex2/ex2.py Executable file
View file

@ -0,0 +1,96 @@
#!/usr/bin/env python
import dataclasses
import enum
import sys
from typing import NamedTuple
def sign(n: int) -> int:
if n > 0:
return 1
elif n < 0:
return -1
return 0
class Direction(enum.Enum):
UP = "U"
DOWN = "D"
LEFT = "L"
RIGHT = "R"
@dataclasses.dataclass
class Move:
dir: Direction
len: int
@classmethod
def from_input(cls, line: str) -> "Move":
d, l = line.split()
return cls(Direction(d), int(l))
class Point(NamedTuple):
x: int
y: int
def move(self, direction: Direction) -> "Point":
match direction:
case Direction.UP:
dx, dy = 1, 0
case Direction.DOWN:
dx, dy = -1, 0
case Direction.LEFT:
dx, dy = 0, -1
case Direction.RIGHT:
dx, dy = 0, 1
return Point(self.x + dx, self.y + dy)
@dataclasses.dataclass
class Rope:
points: list[Point] = dataclasses.field(init=False)
_tail_positions: set[Point] = dataclasses.field(init=False)
def __init__(self, *, length: int = 2) -> None:
assert length > 0
self.points = [Point(0, 0)] * length
self._tail_positions = {self.points[-1]}
def move(self, move: Move) -> None:
for _ in range(move.len):
self.points[0] = self.points[0].move(move.dir)
for i in range(1, len(self.points)):
self.points[i] = self.__move_tail(self.points[i - 1], self.points[i])
self._tail_positions.add(self.points[-1])
@staticmethod
def __move_tail(head: Point, tail: Point) -> Point:
delta_x, delta_y = head.x - tail.x, head.y - tail.y
if abs(delta_x) <= 1 and abs(delta_y) <= 1:
return tail
dx, dy = sign(delta_x), sign(delta_y)
return Point(tail.x + dx, tail.y + dy)
def solve(input: list[str]) -> int:
moves = map(Move.from_input, input)
rope = Rope(length=10)
for move in moves:
rope.move(move)
return len(rope._tail_positions)
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

2000
2022/d09/ex2/input Normal file

File diff suppressed because it is too large Load diff

42
2022/d10/ex1/ex1.py Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
import dataclasses
import sys
from collections.abc import Iterable, Iterator
@dataclasses.dataclass
class CPU:
x: int = dataclasses.field(default=1, init=False)
cycle: int = dataclasses.field(default=0, init=False)
def execute(self, instructions: Iterable[str]) -> Iterator[int]:
for instr in instructions:
yield from self.execute_once(instr)
def execute_once(self, instruction: str) -> Iterator[int]:
if instruction == "noop":
yield from self.__do_cycle()
else:
assert instruction.startswith("addx")
yield from self.__do_cycle(2)
self.x += int(instruction.split()[1])
def __do_cycle(self, cycles: int = 1) -> Iterator[int]:
for _ in range(cycles):
self.cycle += 1
yield self.cycle
def solve(input: list[str]) -> int:
cpu = CPU()
return sum(cycle * cpu.x for cycle in cpu.execute(input) if (cycle % 40) == 20)
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

143
2022/d10/ex1/input Normal file
View file

@ -0,0 +1,143 @@
noop
noop
noop
addx 5
addx 1
addx 4
addx 1
noop
addx 4
noop
addx 1
addx 4
addx 8
addx -7
addx 3
addx 1
noop
addx 4
addx 2
addx 5
addx -1
noop
addx -37
noop
noop
addx 3
addx 2
addx 13
addx 12
addx -15
addx -2
addx 2
addx -11
addx 18
addx 2
addx -15
addx 16
addx 5
addx 2
addx 5
noop
noop
noop
addx 3
addx -2
addx -38
noop
addx 3
addx 4
noop
noop
noop
noop
noop
addx 5
addx 5
noop
noop
addx 21
addx -17
addx 6
noop
noop
noop
noop
addx 5
noop
noop
noop
noop
noop
addx 3
addx 5
addx -38
noop
noop
addx 5
addx -2
addx 1
addx 7
noop
addx 22
addx -18
addx -11
addx 27
addx -13
addx 2
addx 5
addx -8
addx 9
addx 2
noop
addx 7
noop
addx 1
noop
addx -38
noop
addx 2
addx 5
addx -3
noop
addx 8
addx 11
addx -6
noop
addx 24
addx -31
addx 10
addx 2
addx 5
addx 3
noop
addx 2
addx -29
addx 21
addx 11
addx 5
addx -39
addx 4
addx -2
addx 2
addx 7
noop
addx -1
addx 2
noop
addx 4
noop
addx 1
addx 2
addx 5
addx 2
noop
noop
addx -6
addx 9
addx -18
addx 25
addx 3
noop
addx -17
noop

65
2022/d10/ex2/ex2.py Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python
import dataclasses
import sys
from collections.abc import Iterable, Iterator
@dataclasses.dataclass
class CPU:
x: int = dataclasses.field(default=1, init=False)
cycle: int = dataclasses.field(default=0, init=False)
def execute(self, instructions: Iterable[str]) -> Iterator[int]:
for instr in instructions:
yield from self.execute_once(instr)
def execute_once(self, instruction: str) -> Iterator[int]:
if instruction == "noop":
yield from self.__do_cycle()
else:
assert instruction.startswith("addx")
yield from self.__do_cycle(2)
self.x += int(instruction.split()[1])
def __do_cycle(self, cycles: int = 1) -> Iterator[int]:
for _ in range(cycles):
self.cycle += 1
yield self.cycle
@dataclasses.dataclass
class CRT:
width: int = dataclasses.field(default=40, init=False)
height: int = dataclasses.field(default=6, init=False)
pixels: list[list[bool]] = dataclasses.field(init=False)
def __post_init__(self):
self.pixels = [[False for _ in range(self.width)] for _ in range(self.height)]
def do_pixel(self, cycle: int, x_reg: int) -> None:
cycle -= 1 # Simpler modulo computation
x, y = cycle % 40, cycle // 40 % 6
self.pixels[y][x] = abs(x - x_reg) <= 1
def draw(self) -> str:
return "\n".join(
"".join("#" if pixel else " " for pixel in line) for line in self.pixels
)
def solve(input: list[str]) -> str:
cpu = CPU()
crt = CRT()
for cycle in cpu.execute(input):
crt.do_pixel(cycle, cpu.x)
return crt.draw()
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()

143
2022/d10/ex2/input Normal file
View file

@ -0,0 +1,143 @@
noop
noop
noop
addx 5
addx 1
addx 4
addx 1
noop
addx 4
noop
addx 1
addx 4
addx 8
addx -7
addx 3
addx 1
noop
addx 4
addx 2
addx 5
addx -1
noop
addx -37
noop
noop
addx 3
addx 2
addx 13
addx 12
addx -15
addx -2
addx 2
addx -11
addx 18
addx 2
addx -15
addx 16
addx 5
addx 2
addx 5
noop
noop
noop
addx 3
addx -2
addx -38
noop
addx 3
addx 4
noop
noop
noop
noop
noop
addx 5
addx 5
noop
noop
addx 21
addx -17
addx 6
noop
noop
noop
noop
addx 5
noop
noop
noop
noop
noop
addx 3
addx 5
addx -38
noop
noop
addx 5
addx -2
addx 1
addx 7
noop
addx 22
addx -18
addx -11
addx 27
addx -13
addx 2
addx 5
addx -8
addx 9
addx 2
noop
addx 7
noop
addx 1
noop
addx -38
noop
addx 2
addx 5
addx -3
noop
addx 8
addx 11
addx -6
noop
addx 24
addx -31
addx 10
addx 2
addx 5
addx 3
noop
addx 2
addx -29
addx 21
addx 11
addx 5
addx -39
addx 4
addx -2
addx 2
addx 7
noop
addx -1
addx 2
noop
addx 4
noop
addx 1
addx 2
addx 5
addx 2
noop
noop
addx -6
addx 9
addx -18
addx 25
addx 3
noop
addx -17
noop