Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-05 11:36:24 +00:00
parent 4a6817db89
commit 823e6ebe40
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
6 changed files with 104 additions and 0 deletions

View file

@ -0,0 +1 @@
# [Day 5: Supply Stacks](https://adventofcode.com/2022/day/5)

View file

@ -0,0 +1,15 @@
{
"day": 5,
"dir": "challenges/2022/05-supplyStacks",
"implementations": {
"Python": {
"part.1.avg": 0.0073598167896270755,
"part.1.max": 0.03172612190246582,
"part.1.min": 0.005206584930419922,
"part.2.avg": 0.00753035569190979,
"part.2.max": 0.04000711441040039,
"part.2.min": 0.005285978317260742
}
},
"numRuns": 1000
}

View file

@ -0,0 +1,17 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": " [D] \n[N] [C] \n[Z] [M] [P]\n 1 2 3 \n\nmove 1 from 2 to 1\nmove 3 from 1 to 3\nmove 2 from 2 to 1\nmove 1 from 1 to 2\n",
"expected": "CMZ"
}
],
"two": [
{
"input": " [D] \n[N] [C] \n[Z] [M] [P]\n 1 2 3 \n\nmove 1 from 2 to 1\nmove 3 from 1 to 3\nmove 2 from 2 to 1\nmove 1 from 1 to 2\n",
"expected": "MCD"
}
]
}
}

View file

@ -0,0 +1,70 @@
from typing import *
from aocpy import BaseChallenge
from queue import LifoQueue as Stack
from dataclasses import dataclass
import re
@dataclass
class MoveInstruction:
n: int
source: int
destination: int
instruction_regexp = re.compile(r"move (\d+) from (\d+) to (\d+)")
def parse(instr: str) -> Tuple[List[Stack], List[MoveInstruction]]:
raw_initial_state, raw_instructions = instr.rstrip().split("\n\n")
state_lines = raw_initial_state.splitlines()
state_lines = state_lines[:-1] # the last line only contains digits that
# we don't need to look at
state_lines = [[line[i : i + 3] for i in range(0, len(line), 4)] for line in state_lines]
state: List[Stack] = []
for _ in range(len(state_lines[0])):
state.append(Stack())
for line in state_lines[::-1]:
for i, item in enumerate(line):
if item != " ":
state[i].put_nowait(item[1])
instructions: List[MoveInstruction] = []
for line in raw_instructions.strip().splitlines():
instructions.append(
MoveInstruction(*map(int, instruction_regexp.match(line).groups()))
)
return state, instructions
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> str:
state, instructions = parse(instr)
for instruction in instructions:
for _ in range(instruction.n):
x = state[instruction.source - 1].get_nowait()
state[instruction.destination - 1].put_nowait(x)
return "".join(crate.get_nowait() for crate in state)
@staticmethod
def two(instr: str) -> str:
state, instructions = parse(instr)
for instruction in instructions:
items = []
for _ in range(instruction.n):
items.append(state[instruction.source - 1].get_nowait())
for item in items[::-1]:
state[instruction.destination - 1].put_nowait(item)
return "".join(crate.get_nowait() for crate in state)

View file

@ -14,6 +14,7 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
| 02 - Rock Paper Scissors | ★ ★ | [Python](02-rockPaperScissors/py), [Nim](02-rockPaperScissors/nim) | Programmatically playing Rock Paper Scissors |
| 03 - Rucksack Reorganization | ★ ★ | [Python](03-rucksackReorganization/py), [Nim](03-rucksackReorganization/nim) | Sets and intersections |
| 04 - Camp Cleanup | ★ ★ | [Python](04-campCleanup/py), [Nim](04-campCleanup/nim) | More sets and more intersections! |
| 05 - Supply Stacks | ★ ★ | [Python](05-supplyStacks/py) | Believe it or not, this one involved stacks. |
<!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 61 KiB

Before After
Before After