diff --git a/5/README.md b/5/README.md new file mode 100644 index 0000000..2189389 --- /dev/null +++ b/5/README.md @@ -0,0 +1,8 @@ +# [Day 5](https://adventofcode.com/2022/day/6) +:gift::gift::gift::gift::gift: + +Today's language: **Snek** (Python) + +```shell +python3 crates.txt +``` diff --git a/5/crates.txt b/5/crates.txt new file mode 100644 index 0000000..85131c9 --- /dev/null +++ b/5/crates.txt @@ -0,0 +1,511 @@ + [L] [M] [M] + [D] [R] [Z] [C] [L] + [C] [S] [T] [G] [V] [M] +[R] [L] [Q] [B] [B] [D] [F] +[H] [B] [G] [D] [Q] [Z] [T] [J] +[M] [J] [H] [M] [P] [S] [V] [L] [N] +[P] [C] [N] [T] [S] [F] [R] [G] [Q] +[Z] [P] [S] [F] [F] [T] [N] [P] [W] + 1 2 3 4 5 6 7 8 9 + +move 7 from 3 to 9 +move 5 from 8 to 9 +move 3 from 9 to 5 +move 6 from 9 to 2 +move 9 from 9 to 3 +move 3 from 7 to 3 +move 8 from 2 to 3 +move 9 from 3 to 1 +move 11 from 3 to 8 +move 5 from 6 to 9 +move 1 from 6 to 3 +move 1 from 2 to 7 +move 1 from 4 to 8 +move 1 from 3 to 9 +move 4 from 4 to 3 +move 6 from 8 to 3 +move 2 from 8 to 2 +move 4 from 9 to 3 +move 3 from 2 to 5 +move 2 from 5 to 4 +move 5 from 3 to 4 +move 11 from 1 to 4 +move 1 from 7 to 6 +move 1 from 3 to 5 +move 2 from 1 to 9 +move 1 from 1 to 4 +move 7 from 5 to 8 +move 21 from 4 to 6 +move 6 from 6 to 2 +move 6 from 8 to 9 +move 5 from 8 to 5 +move 2 from 2 to 7 +move 4 from 3 to 7 +move 1 from 2 to 6 +move 1 from 2 to 5 +move 2 from 2 to 7 +move 4 from 3 to 7 +move 1 from 4 to 6 +move 9 from 5 to 3 +move 7 from 3 to 4 +move 7 from 7 to 3 +move 7 from 4 to 1 +move 8 from 3 to 5 +move 1 from 3 to 5 +move 3 from 8 to 2 +move 2 from 2 to 9 +move 13 from 9 to 4 +move 5 from 5 to 3 +move 4 from 7 to 6 +move 1 from 7 to 4 +move 2 from 4 to 2 +move 3 from 3 to 4 +move 2 from 5 to 2 +move 6 from 1 to 7 +move 1 from 2 to 8 +move 1 from 3 to 8 +move 1 from 1 to 6 +move 1 from 3 to 4 +move 1 from 2 to 6 +move 24 from 6 to 1 +move 3 from 2 to 3 +move 3 from 3 to 5 +move 2 from 8 to 6 +move 2 from 5 to 4 +move 3 from 5 to 1 +move 7 from 4 to 8 +move 3 from 8 to 9 +move 2 from 9 to 5 +move 2 from 6 to 3 +move 1 from 9 to 8 +move 5 from 7 to 5 +move 2 from 3 to 1 +move 1 from 7 to 1 +move 7 from 4 to 7 +move 2 from 4 to 8 +move 6 from 8 to 6 +move 3 from 6 to 9 +move 10 from 5 to 1 +move 7 from 7 to 1 +move 1 from 4 to 9 +move 1 from 6 to 3 +move 2 from 9 to 7 +move 1 from 4 to 2 +move 1 from 9 to 5 +move 1 from 8 to 5 +move 39 from 1 to 8 +move 1 from 2 to 5 +move 2 from 6 to 9 +move 3 from 9 to 5 +move 3 from 1 to 6 +move 1 from 7 to 2 +move 1 from 3 to 2 +move 2 from 6 to 2 +move 3 from 2 to 3 +move 1 from 6 to 2 +move 1 from 1 to 8 +move 3 from 1 to 2 +move 3 from 2 to 4 +move 2 from 4 to 5 +move 2 from 3 to 8 +move 8 from 5 to 2 +move 8 from 8 to 2 +move 15 from 2 to 7 +move 1 from 1 to 5 +move 25 from 8 to 7 +move 2 from 2 to 4 +move 2 from 4 to 3 +move 1 from 8 to 4 +move 2 from 4 to 6 +move 1 from 2 to 1 +move 26 from 7 to 2 +move 15 from 2 to 1 +move 7 from 8 to 9 +move 10 from 1 to 6 +move 10 from 7 to 2 +move 1 from 8 to 1 +move 5 from 9 to 8 +move 1 from 8 to 9 +move 2 from 6 to 9 +move 3 from 7 to 1 +move 1 from 7 to 1 +move 5 from 9 to 2 +move 1 from 3 to 1 +move 9 from 6 to 3 +move 1 from 6 to 1 +move 4 from 2 to 4 +move 3 from 4 to 8 +move 1 from 4 to 1 +move 9 from 3 to 1 +move 1 from 7 to 6 +move 9 from 2 to 5 +move 14 from 1 to 6 +move 1 from 3 to 8 +move 5 from 2 to 6 +move 8 from 1 to 8 +move 6 from 6 to 8 +move 14 from 6 to 7 +move 1 from 1 to 7 +move 10 from 5 to 4 +move 11 from 8 to 5 +move 15 from 7 to 1 +move 4 from 5 to 6 +move 4 from 8 to 9 +move 6 from 5 to 3 +move 1 from 6 to 9 +move 1 from 1 to 6 +move 1 from 5 to 8 +move 2 from 6 to 2 +move 6 from 1 to 5 +move 1 from 5 to 8 +move 2 from 5 to 4 +move 9 from 2 to 9 +move 13 from 9 to 8 +move 1 from 2 to 1 +move 1 from 4 to 8 +move 3 from 3 to 1 +move 2 from 4 to 5 +move 2 from 1 to 5 +move 1 from 9 to 3 +move 17 from 8 to 1 +move 3 from 3 to 2 +move 4 from 5 to 1 +move 2 from 2 to 4 +move 1 from 6 to 1 +move 1 from 2 to 8 +move 4 from 4 to 6 +move 1 from 5 to 9 +move 5 from 6 to 8 +move 1 from 5 to 4 +move 1 from 5 to 6 +move 3 from 8 to 6 +move 8 from 4 to 5 +move 32 from 1 to 7 +move 11 from 7 to 6 +move 8 from 5 to 3 +move 3 from 8 to 7 +move 6 from 3 to 9 +move 4 from 3 to 8 +move 5 from 8 to 2 +move 1 from 8 to 5 +move 11 from 6 to 3 +move 1 from 5 to 2 +move 2 from 8 to 6 +move 12 from 7 to 8 +move 2 from 6 to 2 +move 2 from 6 to 4 +move 5 from 2 to 5 +move 8 from 7 to 2 +move 2 from 7 to 1 +move 2 from 7 to 6 +move 5 from 5 to 4 +move 5 from 4 to 7 +move 5 from 8 to 2 +move 2 from 9 to 7 +move 5 from 8 to 4 +move 2 from 7 to 3 +move 2 from 9 to 3 +move 3 from 7 to 9 +move 1 from 1 to 8 +move 2 from 6 to 1 +move 2 from 9 to 8 +move 1 from 7 to 8 +move 1 from 2 to 5 +move 1 from 7 to 9 +move 7 from 4 to 3 +move 3 from 3 to 6 +move 5 from 8 to 6 +move 3 from 9 to 5 +move 16 from 3 to 1 +move 2 from 9 to 1 +move 7 from 1 to 8 +move 1 from 1 to 2 +move 5 from 8 to 2 +move 12 from 1 to 4 +move 1 from 3 to 5 +move 1 from 2 to 9 +move 1 from 9 to 4 +move 4 from 6 to 5 +move 5 from 6 to 1 +move 1 from 6 to 5 +move 1 from 1 to 4 +move 1 from 4 to 7 +move 1 from 3 to 7 +move 9 from 4 to 6 +move 2 from 7 to 8 +move 1 from 3 to 4 +move 2 from 8 to 9 +move 4 from 8 to 4 +move 4 from 2 to 8 +move 2 from 9 to 7 +move 2 from 7 to 8 +move 10 from 2 to 4 +move 1 from 2 to 1 +move 5 from 4 to 7 +move 1 from 1 to 3 +move 3 from 8 to 7 +move 6 from 7 to 2 +move 3 from 2 to 7 +move 1 from 6 to 7 +move 5 from 5 to 8 +move 4 from 1 to 3 +move 4 from 3 to 1 +move 8 from 4 to 2 +move 1 from 3 to 2 +move 2 from 7 to 2 +move 2 from 6 to 3 +move 4 from 7 to 2 +move 4 from 5 to 7 +move 14 from 2 to 7 +move 3 from 2 to 1 +move 3 from 8 to 2 +move 1 from 5 to 7 +move 6 from 2 to 4 +move 2 from 2 to 7 +move 2 from 3 to 6 +move 6 from 8 to 2 +move 4 from 6 to 4 +move 2 from 6 to 9 +move 4 from 4 to 2 +move 2 from 4 to 8 +move 10 from 7 to 2 +move 18 from 2 to 6 +move 2 from 2 to 6 +move 2 from 9 to 2 +move 2 from 8 to 5 +move 1 from 2 to 9 +move 1 from 2 to 9 +move 1 from 5 to 7 +move 1 from 2 to 6 +move 2 from 9 to 2 +move 6 from 7 to 3 +move 7 from 6 to 8 +move 5 from 7 to 2 +move 1 from 7 to 4 +move 1 from 5 to 7 +move 4 from 8 to 7 +move 5 from 2 to 3 +move 1 from 7 to 5 +move 2 from 2 to 8 +move 9 from 4 to 3 +move 13 from 6 to 8 +move 10 from 3 to 1 +move 1 from 5 to 2 +move 3 from 6 to 8 +move 5 from 1 to 2 +move 1 from 1 to 8 +move 2 from 4 to 3 +move 17 from 8 to 6 +move 5 from 6 to 3 +move 3 from 1 to 2 +move 9 from 6 to 5 +move 2 from 6 to 8 +move 5 from 5 to 9 +move 3 from 9 to 8 +move 3 from 1 to 3 +move 3 from 7 to 5 +move 6 from 5 to 8 +move 7 from 2 to 4 +move 1 from 6 to 3 +move 1 from 1 to 5 +move 4 from 4 to 5 +move 2 from 2 to 9 +move 3 from 1 to 3 +move 4 from 5 to 8 +move 1 from 4 to 5 +move 6 from 8 to 7 +move 1 from 5 to 2 +move 4 from 9 to 2 +move 2 from 5 to 9 +move 2 from 1 to 8 +move 2 from 4 to 9 +move 6 from 7 to 5 +move 3 from 5 to 2 +move 3 from 2 to 5 +move 10 from 8 to 3 +move 2 from 8 to 5 +move 3 from 2 to 5 +move 6 from 5 to 1 +move 4 from 5 to 6 +move 1 from 7 to 5 +move 23 from 3 to 7 +move 2 from 5 to 9 +move 2 from 1 to 5 +move 2 from 6 to 3 +move 6 from 3 to 1 +move 1 from 1 to 7 +move 4 from 3 to 1 +move 1 from 8 to 5 +move 2 from 9 to 2 +move 3 from 3 to 8 +move 2 from 6 to 8 +move 12 from 1 to 3 +move 1 from 9 to 7 +move 3 from 5 to 9 +move 9 from 3 to 8 +move 1 from 1 to 7 +move 1 from 9 to 4 +move 3 from 3 to 6 +move 3 from 2 to 1 +move 3 from 8 to 6 +move 1 from 4 to 2 +move 1 from 2 to 9 +move 1 from 2 to 7 +move 20 from 7 to 5 +move 3 from 7 to 3 +move 3 from 1 to 3 +move 5 from 8 to 1 +move 5 from 1 to 5 +move 4 from 5 to 2 +move 3 from 2 to 6 +move 3 from 8 to 7 +move 1 from 2 to 6 +move 2 from 8 to 6 +move 2 from 7 to 5 +move 2 from 3 to 6 +move 12 from 5 to 1 +move 6 from 5 to 7 +move 12 from 6 to 8 +move 4 from 9 to 3 +move 4 from 5 to 8 +move 3 from 1 to 5 +move 4 from 7 to 4 +move 3 from 5 to 9 +move 7 from 1 to 6 +move 1 from 1 to 3 +move 6 from 7 to 6 +move 1 from 1 to 3 +move 10 from 3 to 6 +move 10 from 6 to 2 +move 2 from 9 to 5 +move 4 from 6 to 5 +move 9 from 6 to 1 +move 16 from 8 to 7 +move 3 from 8 to 7 +move 1 from 8 to 1 +move 7 from 2 to 1 +move 1 from 5 to 9 +move 1 from 6 to 1 +move 2 from 2 to 1 +move 3 from 1 to 4 +move 1 from 6 to 8 +move 7 from 4 to 1 +move 1 from 8 to 2 +move 22 from 1 to 8 +move 18 from 7 to 9 +move 6 from 5 to 2 +move 2 from 2 to 7 +move 2 from 1 to 5 +move 4 from 7 to 6 +move 1 from 5 to 6 +move 2 from 8 to 2 +move 3 from 2 to 6 +move 1 from 5 to 6 +move 15 from 9 to 6 +move 6 from 9 to 5 +move 1 from 9 to 8 +move 1 from 2 to 9 +move 5 from 5 to 9 +move 9 from 8 to 6 +move 3 from 2 to 7 +move 12 from 8 to 9 +move 1 from 7 to 5 +move 1 from 5 to 7 +move 3 from 7 to 1 +move 17 from 6 to 3 +move 1 from 2 to 6 +move 2 from 1 to 4 +move 16 from 6 to 4 +move 7 from 4 to 6 +move 1 from 5 to 7 +move 8 from 4 to 5 +move 9 from 9 to 8 +move 16 from 3 to 7 +move 1 from 1 to 5 +move 3 from 5 to 1 +move 5 from 6 to 2 +move 3 from 1 to 7 +move 3 from 6 to 7 +move 3 from 9 to 3 +move 5 from 8 to 5 +move 11 from 5 to 7 +move 2 from 3 to 7 +move 1 from 2 to 1 +move 1 from 3 to 6 +move 17 from 7 to 9 +move 1 from 3 to 2 +move 3 from 4 to 6 +move 1 from 1 to 2 +move 1 from 6 to 4 +move 14 from 7 to 6 +move 15 from 9 to 6 +move 4 from 8 to 7 +move 1 from 4 to 7 +move 7 from 9 to 5 +move 5 from 2 to 9 +move 7 from 5 to 1 +move 3 from 1 to 7 +move 29 from 6 to 4 +move 1 from 2 to 4 +move 18 from 4 to 2 +move 3 from 1 to 4 +move 1 from 1 to 7 +move 18 from 2 to 4 +move 3 from 6 to 5 +move 15 from 4 to 1 +move 1 from 5 to 1 +move 1 from 5 to 4 +move 9 from 4 to 1 +move 5 from 1 to 3 +move 9 from 1 to 5 +move 2 from 4 to 3 +move 5 from 5 to 6 +move 3 from 7 to 9 +move 7 from 7 to 5 +move 6 from 4 to 6 +move 2 from 3 to 7 +move 6 from 5 to 8 +move 2 from 8 to 4 +move 1 from 8 to 9 +move 9 from 6 to 2 +move 3 from 9 to 3 +move 1 from 2 to 1 +move 6 from 7 to 4 +move 2 from 2 to 8 +move 3 from 9 to 5 +move 5 from 4 to 8 +move 1 from 6 to 9 +move 1 from 3 to 1 +move 1 from 3 to 4 +move 1 from 6 to 5 +move 1 from 9 to 3 +move 10 from 8 to 7 +move 3 from 9 to 2 +move 7 from 2 to 4 +move 6 from 5 to 7 +move 4 from 5 to 8 +move 7 from 3 to 2 +move 3 from 7 to 1 +move 9 from 1 to 5 +move 5 from 7 to 9 +move 7 from 1 to 4 +move 11 from 4 to 2 +move 4 from 8 to 3 +move 5 from 4 to 7 +move 4 from 4 to 1 +move 1 from 3 to 6 +move 12 from 7 to 4 +move 2 from 1 to 8 +move 5 from 9 to 7 +move 7 from 5 to 6 +move 1 from 1 to 4 +move 1 from 9 to 8 +move 1 from 4 to 7 +move 1 from 8 to 9 +move 5 from 7 to 9 +move 2 from 7 to 5 +move 2 from 6 to 3 +move 5 from 2 to 7 +move 1 from 7 to 8 +move 1 from 1 to 6 +move 3 from 5 to 1 diff --git a/5/day5.py b/5/day5.py new file mode 100644 index 0000000..8179a80 --- /dev/null +++ b/5/day5.py @@ -0,0 +1,56 @@ +from re import fullmatch +from sys import argv, exit + +re_instruction = r"move (\d+) from (\d+) to (\d+)\n" + +MAX_CRATES = 8 +STACK_COUNT = 9 + +def fill_stacks(cratestacks:list[str], lines:list[str]): + for _ in range(MAX_CRATES): + line = lines.pop(0).strip('\n') + if not '[' in line: return + for i in range(STACK_COUNT): + # if i * 4 + 1 > len(line): + # print(f"Error, i{i} len(line){len(line)}, line {line}") + if line[i*4] == '[': cratestacks[i] += line[i*4+1] + for i in range(len(cratestacks)): + cratestacks[i] = cratestacks[i][::-1] + +def crate_mover_9000(cratestacks: list[str], amount:int, fromStack:int, toStack:int): + # print("9000", cratestacks, amount, fromStack, toStack) + for _ in range(amount): + cratestacks[toStack-1] += cratestacks[fromStack-1][-1] + cratestacks[fromStack-1] = cratestacks[fromStack-1][:-1] + +def crate_mover_9001(cratestacks: list[str], amount:int, fromStack:int, toStack:int): + # print("9001", cratestacks, amount, fromStack, toStack, cratestacks[fromStack-1][(-amount):]) + cratestacks[toStack-1] += cratestacks[fromStack-1][(-amount):] + cratestacks[fromStack-1] = cratestacks[fromStack-1][:(-amount)] + +def main(filename): + with open(filename, 'r') as file: + lines = file.readlines() + cratestacks_1:list[str] = [ "" for _ in range(STACK_COUNT) ] + fill_stacks(cratestacks_1, lines) + cratestacks_2:list[str] = list(cratestacks_1) + for line in lines: + match = fullmatch(re_instruction, line) + if not match: continue + amount, fromStack, toStack = [ int(x) for x in match.groups() ] + # print(amount, fromStack, toStack) + crate_mover_9000(cratestacks_1, amount, fromStack, toStack); + crate_mover_9001(cratestacks_2, amount, fromStack, toStack); + top_crates_1 = "" + top_crates_2 = "" + for i in range(len(cratestacks_1)): + top_crates_1 += cratestacks_1[i][-1] + top_crates_2 += cratestacks_2[i][-1] + print(f"Using the CrateMover 9000, the crates at the top are: {top_crates_1}") + print(f"Using the CrateMover 9001, the crates at the top are: {top_crates_2}") + +if __name__ == '__main__': + if not len(argv) == 2: + print(f"Expected exactly one argument (filename), got {len(argv)-1}") + exit(1) + main(argv[1])