From ed8d7871843a4c4aa3d24c5e733db1c7226eec1d Mon Sep 17 00:00:00 2001 From: Felix Albrigtsen Date: Mon, 5 Dec 2022 11:41:05 +0100 Subject: [PATCH] Upload day05 --- README.md | 2 +- day05/input.txt | 513 +++++++++++++++++++++++++++++++++++++++++++ day05/input_demo.txt | 9 + day05/solution.lua | 157 +++++++++++++ 4 files changed, 680 insertions(+), 1 deletion(-) create mode 100644 day05/input.txt create mode 100644 day05/input_demo.txt create mode 100644 day05/solution.lua diff --git a/README.md b/README.md index 1f89620..9e28c15 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ These are my attempts on [AoC 2022](https://adventofcode.com/2022/) | [02](day02) | ⭐ | ⭐ | C | | [03](day03) | ⭐ | ⭐ | Rust | | [04](day04) | ⭐ | ⭐ | Haskell | -| 05 | | | | +| [05](day05) | ⭐ | ⭐ | Lua | | 06 | | | | | 07 | | | | | 08 | | | | diff --git a/day05/input.txt b/day05/input.txt new file mode 100644 index 0000000..e83836d --- /dev/null +++ b/day05/input.txt @@ -0,0 +1,513 @@ + [P] [C] [C] + [W] [B] [G] [V] [V] + [V] [T] [Z] [J] [T] [S] + [D] [L] [Q] [F] [Z] [W] [R] + [C] [N] [R] [H] [L] [Q] [F] [G] +[F] [M] [Z] [H] [G] [W] [L] [R] [H] +[R] [H] [M] [C] [P] [C] [V] [N] [W] +[W] [T] [P] [J] [C] [G] [W] [P] [J] + 1 2 3 4 5 6 7 8 9 + +move 2 from 4 to 9 +move 5 from 2 to 9 +move 1 from 5 to 1 +move 3 from 1 to 4 +move 2 from 4 to 6 +move 7 from 6 to 9 +move 5 from 3 to 9 +move 1 from 8 to 6 +move 3 from 2 to 3 +move 12 from 9 to 3 +move 4 from 9 to 7 +move 15 from 3 to 9 +move 1 from 1 to 5 +move 2 from 6 to 5 +move 18 from 9 to 1 +move 6 from 8 to 1 +move 1 from 8 to 7 +move 5 from 7 to 2 +move 6 from 1 to 2 +move 7 from 9 to 6 +move 6 from 1 to 3 +move 5 from 3 to 9 +move 3 from 9 to 1 +move 1 from 7 to 9 +move 4 from 2 to 1 +move 5 from 6 to 3 +move 1 from 3 to 4 +move 1 from 5 to 9 +move 2 from 9 to 6 +move 5 from 5 to 9 +move 10 from 1 to 8 +move 4 from 3 to 8 +move 3 from 4 to 9 +move 4 from 6 to 9 +move 14 from 8 to 6 +move 1 from 3 to 8 +move 14 from 9 to 4 +move 6 from 1 to 6 +move 1 from 8 to 2 +move 3 from 5 to 8 +move 1 from 8 to 9 +move 1 from 8 to 1 +move 5 from 4 to 9 +move 1 from 8 to 4 +move 3 from 9 to 4 +move 3 from 7 to 5 +move 7 from 6 to 3 +move 7 from 4 to 1 +move 3 from 9 to 1 +move 7 from 2 to 3 +move 1 from 4 to 8 +move 8 from 6 to 2 +move 2 from 7 to 4 +move 1 from 7 to 4 +move 1 from 7 to 9 +move 1 from 5 to 9 +move 1 from 9 to 4 +move 1 from 4 to 2 +move 8 from 4 to 9 +move 1 from 4 to 2 +move 5 from 9 to 4 +move 2 from 6 to 9 +move 1 from 6 to 9 +move 1 from 8 to 1 +move 13 from 3 to 2 +move 1 from 3 to 9 +move 2 from 6 to 8 +move 1 from 8 to 1 +move 14 from 1 to 7 +move 4 from 2 to 1 +move 2 from 9 to 5 +move 3 from 9 to 7 +move 1 from 8 to 2 +move 4 from 1 to 5 +move 1 from 4 to 7 +move 3 from 9 to 1 +move 7 from 7 to 4 +move 14 from 2 to 8 +move 3 from 1 to 7 +move 3 from 5 to 4 +move 2 from 1 to 9 +move 11 from 8 to 9 +move 3 from 7 to 8 +move 3 from 8 to 6 +move 6 from 4 to 3 +move 2 from 6 to 8 +move 8 from 4 to 3 +move 3 from 8 to 7 +move 2 from 8 to 2 +move 2 from 3 to 9 +move 1 from 6 to 8 +move 5 from 2 to 7 +move 10 from 9 to 7 +move 1 from 8 to 5 +move 3 from 5 to 2 +move 6 from 7 to 5 +move 19 from 7 to 3 +move 9 from 5 to 9 +move 6 from 2 to 6 +move 2 from 7 to 3 +move 29 from 3 to 8 +move 2 from 7 to 9 +move 5 from 8 to 1 +move 12 from 9 to 6 +move 1 from 3 to 8 +move 1 from 2 to 7 +move 1 from 3 to 1 +move 10 from 6 to 1 +move 1 from 6 to 7 +move 9 from 1 to 9 +move 2 from 1 to 2 +move 12 from 9 to 4 +move 7 from 6 to 3 +move 8 from 3 to 7 +move 5 from 7 to 6 +move 19 from 8 to 3 +move 10 from 4 to 6 +move 1 from 4 to 6 +move 6 from 8 to 6 +move 1 from 4 to 2 +move 6 from 6 to 3 +move 3 from 2 to 7 +move 13 from 6 to 3 +move 1 from 9 to 1 +move 6 from 1 to 8 +move 1 from 6 to 5 +move 1 from 5 to 4 +move 3 from 7 to 1 +move 2 from 1 to 3 +move 11 from 3 to 8 +move 1 from 4 to 3 +move 3 from 8 to 4 +move 1 from 7 to 5 +move 3 from 8 to 9 +move 2 from 9 to 2 +move 7 from 8 to 3 +move 1 from 7 to 9 +move 1 from 1 to 4 +move 32 from 3 to 4 +move 1 from 5 to 9 +move 2 from 8 to 3 +move 2 from 6 to 4 +move 1 from 9 to 4 +move 1 from 9 to 2 +move 3 from 3 to 1 +move 1 from 8 to 6 +move 1 from 6 to 2 +move 1 from 9 to 3 +move 1 from 1 to 7 +move 1 from 8 to 7 +move 2 from 3 to 8 +move 1 from 8 to 4 +move 1 from 1 to 2 +move 2 from 4 to 8 +move 1 from 1 to 8 +move 26 from 4 to 6 +move 3 from 8 to 5 +move 3 from 7 to 6 +move 7 from 6 to 3 +move 18 from 6 to 8 +move 16 from 8 to 9 +move 1 from 5 to 1 +move 2 from 8 to 3 +move 3 from 9 to 8 +move 3 from 6 to 4 +move 2 from 5 to 4 +move 1 from 6 to 4 +move 2 from 7 to 2 +move 2 from 3 to 9 +move 4 from 8 to 3 +move 1 from 1 to 2 +move 6 from 9 to 7 +move 2 from 2 to 5 +move 12 from 3 to 1 +move 9 from 9 to 2 +move 10 from 1 to 3 +move 2 from 5 to 9 +move 8 from 4 to 7 +move 13 from 7 to 6 +move 6 from 6 to 5 +move 4 from 5 to 3 +move 2 from 5 to 4 +move 8 from 4 to 3 +move 1 from 7 to 2 +move 15 from 2 to 7 +move 8 from 3 to 7 +move 1 from 1 to 6 +move 7 from 7 to 1 +move 5 from 1 to 6 +move 7 from 3 to 2 +move 3 from 1 to 6 +move 12 from 7 to 9 +move 12 from 9 to 8 +move 1 from 7 to 1 +move 2 from 9 to 5 +move 1 from 1 to 9 +move 4 from 4 to 2 +move 4 from 8 to 4 +move 2 from 7 to 2 +move 4 from 6 to 5 +move 4 from 8 to 9 +move 1 from 8 to 4 +move 5 from 5 to 3 +move 5 from 2 to 4 +move 5 from 9 to 5 +move 1 from 3 to 6 +move 1 from 7 to 8 +move 12 from 3 to 9 +move 4 from 2 to 6 +move 7 from 4 to 9 +move 13 from 6 to 4 +move 3 from 6 to 9 +move 4 from 4 to 2 +move 1 from 3 to 4 +move 21 from 9 to 7 +move 4 from 2 to 1 +move 3 from 5 to 4 +move 8 from 7 to 6 +move 2 from 7 to 2 +move 11 from 4 to 2 +move 1 from 9 to 7 +move 1 from 5 to 7 +move 1 from 1 to 8 +move 5 from 2 to 5 +move 1 from 3 to 5 +move 2 from 4 to 9 +move 3 from 4 to 8 +move 3 from 1 to 8 +move 1 from 9 to 6 +move 8 from 7 to 8 +move 9 from 6 to 5 +move 1 from 9 to 6 +move 1 from 6 to 4 +move 3 from 7 to 5 +move 1 from 6 to 9 +move 12 from 5 to 1 +move 2 from 5 to 8 +move 1 from 9 to 6 +move 2 from 7 to 6 +move 9 from 1 to 8 +move 1 from 6 to 9 +move 1 from 9 to 2 +move 1 from 4 to 2 +move 2 from 6 to 7 +move 5 from 8 to 3 +move 2 from 7 to 4 +move 16 from 8 to 5 +move 2 from 3 to 8 +move 7 from 5 to 1 +move 3 from 3 to 8 +move 7 from 5 to 7 +move 4 from 5 to 2 +move 6 from 7 to 9 +move 2 from 9 to 6 +move 2 from 9 to 2 +move 1 from 6 to 8 +move 12 from 2 to 6 +move 2 from 9 to 6 +move 1 from 5 to 2 +move 3 from 5 to 4 +move 9 from 2 to 6 +move 6 from 8 to 3 +move 1 from 7 to 5 +move 1 from 6 to 7 +move 1 from 7 to 8 +move 1 from 5 to 8 +move 5 from 1 to 2 +move 3 from 4 to 5 +move 4 from 6 to 8 +move 5 from 2 to 9 +move 5 from 8 to 4 +move 1 from 1 to 4 +move 9 from 8 to 4 +move 1 from 2 to 3 +move 3 from 6 to 8 +move 4 from 9 to 2 +move 2 from 6 to 4 +move 2 from 3 to 1 +move 4 from 4 to 7 +move 6 from 4 to 5 +move 10 from 6 to 8 +move 4 from 1 to 9 +move 4 from 7 to 5 +move 3 from 3 to 9 +move 6 from 9 to 8 +move 2 from 2 to 9 +move 8 from 4 to 3 +move 2 from 2 to 7 +move 1 from 4 to 9 +move 6 from 3 to 8 +move 2 from 7 to 8 +move 6 from 5 to 9 +move 5 from 5 to 6 +move 2 from 5 to 9 +move 7 from 9 to 5 +move 2 from 1 to 9 +move 6 from 5 to 8 +move 1 from 5 to 1 +move 2 from 3 to 6 +move 1 from 3 to 6 +move 4 from 9 to 5 +move 1 from 3 to 4 +move 1 from 1 to 2 +move 1 from 2 to 1 +move 1 from 6 to 8 +move 14 from 8 to 5 +move 6 from 5 to 1 +move 16 from 8 to 3 +move 2 from 8 to 2 +move 10 from 6 to 7 +move 1 from 6 to 9 +move 2 from 2 to 9 +move 2 from 7 to 3 +move 1 from 8 to 5 +move 3 from 9 to 1 +move 4 from 9 to 5 +move 9 from 3 to 8 +move 2 from 3 to 6 +move 5 from 3 to 8 +move 1 from 4 to 2 +move 12 from 8 to 4 +move 1 from 8 to 9 +move 4 from 5 to 9 +move 7 from 7 to 1 +move 10 from 5 to 2 +move 2 from 5 to 2 +move 1 from 6 to 5 +move 2 from 5 to 2 +move 5 from 2 to 6 +move 4 from 9 to 6 +move 6 from 4 to 9 +move 2 from 3 to 4 +move 6 from 4 to 7 +move 6 from 7 to 5 +move 10 from 1 to 5 +move 4 from 1 to 2 +move 4 from 6 to 3 +move 6 from 9 to 7 +move 2 from 4 to 9 +move 7 from 7 to 6 +move 1 from 9 to 7 +move 2 from 9 to 8 +move 2 from 8 to 2 +move 1 from 2 to 5 +move 3 from 8 to 4 +move 4 from 2 to 7 +move 3 from 4 to 7 +move 2 from 3 to 5 +move 2 from 3 to 2 +move 18 from 5 to 3 +move 6 from 3 to 1 +move 8 from 3 to 1 +move 8 from 7 to 9 +move 9 from 2 to 5 +move 3 from 2 to 3 +move 7 from 3 to 7 +move 3 from 6 to 4 +move 1 from 7 to 1 +move 7 from 6 to 7 +move 1 from 2 to 9 +move 1 from 4 to 2 +move 13 from 7 to 2 +move 10 from 5 to 3 +move 1 from 2 to 9 +move 7 from 1 to 5 +move 8 from 9 to 5 +move 1 from 9 to 5 +move 1 from 9 to 8 +move 1 from 8 to 2 +move 8 from 5 to 3 +move 18 from 3 to 5 +move 2 from 4 to 1 +move 3 from 2 to 5 +move 27 from 5 to 1 +move 17 from 1 to 5 +move 2 from 2 to 3 +move 1 from 6 to 5 +move 2 from 2 to 5 +move 1 from 6 to 4 +move 1 from 6 to 9 +move 2 from 3 to 5 +move 17 from 5 to 6 +move 1 from 9 to 3 +move 6 from 2 to 4 +move 1 from 3 to 2 +move 3 from 4 to 9 +move 1 from 2 to 9 +move 1 from 4 to 7 +move 3 from 5 to 2 +move 2 from 5 to 1 +move 1 from 5 to 2 +move 1 from 7 to 3 +move 18 from 1 to 4 +move 1 from 3 to 1 +move 5 from 4 to 2 +move 1 from 5 to 1 +move 9 from 2 to 7 +move 1 from 4 to 5 +move 1 from 2 to 9 +move 8 from 6 to 2 +move 13 from 4 to 2 +move 2 from 4 to 9 +move 1 from 5 to 2 +move 1 from 6 to 8 +move 6 from 7 to 5 +move 1 from 8 to 4 +move 1 from 7 to 6 +move 1 from 6 to 1 +move 7 from 6 to 5 +move 1 from 7 to 9 +move 6 from 9 to 3 +move 2 from 9 to 7 +move 2 from 5 to 7 +move 4 from 7 to 8 +move 4 from 5 to 4 +move 1 from 6 to 7 +move 3 from 3 to 8 +move 6 from 5 to 9 +move 2 from 3 to 5 +move 4 from 4 to 7 +move 1 from 3 to 1 +move 2 from 2 to 3 +move 6 from 9 to 6 +move 1 from 7 to 1 +move 19 from 2 to 4 +move 2 from 5 to 6 +move 2 from 8 to 9 +move 2 from 1 to 2 +move 2 from 2 to 5 +move 2 from 4 to 3 +move 4 from 6 to 2 +move 1 from 7 to 8 +move 6 from 1 to 8 +move 3 from 5 to 1 +move 5 from 2 to 5 +move 1 from 6 to 7 +move 9 from 8 to 1 +move 2 from 3 to 6 +move 4 from 6 to 5 +move 1 from 6 to 2 +move 9 from 5 to 2 +move 3 from 4 to 6 +move 12 from 4 to 6 +move 1 from 9 to 4 +move 1 from 3 to 1 +move 3 from 4 to 8 +move 1 from 3 to 6 +move 6 from 6 to 2 +move 1 from 4 to 5 +move 3 from 6 to 2 +move 4 from 1 to 5 +move 1 from 5 to 1 +move 2 from 8 to 9 +move 7 from 6 to 3 +move 1 from 3 to 1 +move 1 from 8 to 1 +move 3 from 8 to 9 +move 4 from 3 to 5 +move 3 from 7 to 3 +move 5 from 3 to 7 +move 1 from 9 to 1 +move 4 from 9 to 2 +move 15 from 2 to 7 +move 14 from 1 to 7 +move 5 from 5 to 1 +move 9 from 7 to 2 +move 1 from 9 to 6 +move 1 from 7 to 4 +move 1 from 4 to 6 +move 2 from 6 to 2 +move 9 from 2 to 5 +move 4 from 2 to 4 +move 4 from 7 to 5 +move 6 from 5 to 9 +move 7 from 1 to 8 +move 6 from 2 to 8 +move 1 from 1 to 2 +move 3 from 9 to 5 +move 18 from 7 to 8 +move 2 from 4 to 6 +move 2 from 4 to 6 +move 3 from 7 to 6 +move 3 from 5 to 3 +move 1 from 2 to 6 +move 5 from 6 to 8 +move 29 from 8 to 1 +move 2 from 3 to 5 +move 25 from 1 to 6 +move 2 from 9 to 5 +move 1 from 7 to 8 +move 6 from 8 to 2 +move 1 from 9 to 1 +move 15 from 6 to 8 +move 1 from 3 to 8 +move 14 from 8 to 7 +move 5 from 1 to 3 +move 1 from 6 to 2 +move 2 from 5 to 7 +move 10 from 6 to 2 +move 4 from 5 to 7 +move 6 from 5 to 1 +move 2 from 1 to 4 +move 19 from 7 to 9 diff --git a/day05/input_demo.txt b/day05/input_demo.txt new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/day05/input_demo.txt @@ -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 diff --git a/day05/solution.lua b/day05/solution.lua new file mode 100644 index 0000000..eb9927c --- /dev/null +++ b/day05/solution.lua @@ -0,0 +1,157 @@ +Stack = {} + +function Stack:new() + local o = {} + setmetatable(o, self) + self.__index = self + return o +end + +function Stack:push(value) + table.insert(self, value) +end + +function Stack:pop() + return table.remove(self) +end + +function Stack:top() + return self[#self] +end + +function Stack:reverse() + local newStack = Stack:new() + while #self > 0 do + newStack:push(self:pop()) + end + return newStack +end + +function Split(s, delimiter) + result = {}; + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match); + end + return result; +end + +function deepcopy(orig, copies) + copies = copies or {} + local orig_type = type(orig) + local copy + if orig_type == 'table' then + if copies[orig] then + copy = copies[orig] + else + copy = {} + copies[orig] = copy + for orig_key, orig_value in next, orig, nil do + copy[deepcopy(orig_key, copies)] = deepcopy(orig_value, copies) + end + setmetatable(copy, deepcopy(getmetatable(orig), copies)) + end + else -- number, string, boolean, etc + copy = orig + end + return copy +end + +-- Read input data + +if arg[1] == nil then + print("Usage: "..arg[0].." ") + os.exit(1) +end +file = io.open(arg[1], "r") +if file == nil then + print("File not found: "..arg[1]) + os.exit(1) +end + +-- Parse input data + +inputLines = file:read("all") +file:close() + +parts = Split(inputLines, "\n\n") +initialState = Split(parts[1], "\n") +moves = Split(parts[2], "\n") + +-- Initialize stacks +stackCount = (#initialState[1] + 1) // 4 +stacks = {} +for i = 1, stackCount do + stacks[i] = Stack:new() +end + +-- Fill stacks +for i = 1, #initialState-1 do + for j = 1, stackCount do + letter = string.sub(initialState[i], 4 * j - 2, 4 * j - 2) + if letter ~= " " then + stacks[j]:push(letter) + end + end +end + +for i = 1, #stacks do + stacks[i] = stacks[i]:reverse() +end + +stacks2 = deepcopy(stacks) + +print("Initial state:") +for i = 1, stackCount do + print("Stack "..i..": "..table.concat(stacks[i], " ")) +end + +-- Perform moves + +-- Part 1 + +for i = 1, #moves do + move = Split(moves[i], " ") + count = tonumber(move[2]) + from = tonumber(move[4]) + to = tonumber(move[6]) + + for j = 1, count do + stacks[to]:push(stacks[from]:pop()) + end +end + +part1 = "" +for i = 1, stackCount do + part1 = part1..stacks[i]:top() +end + +print("Part 1: "..part1) + +-- print("Final state:") +-- for i = 1, stackCount do +-- print("Stack "..i..": "..table.concat(stacks[i], " ")) +-- end + +-- Part 2 +for i = 1, #moves do + move = Split(moves[i], " ") + count = tonumber(move[2]) + from = tonumber(move[4]) + to = tonumber(move[6]) + + temp = Stack:new() + for j = 1, count do + temp:push(stacks2[from]:pop()) + end + -- print("Move "..i..": "..table.concat(temp, " ")) + for j = 1, count do + stacks2[to]:push(temp:pop()) + end +end + +part2 = "" +for i = 1, stackCount do + part2 = part2..stacks2[i]:top() +end + +print("Part 2: "..part2)