2022-05
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
4a6817db89
commit
823e6ebe40
6 changed files with 104 additions and 0 deletions
1
challenges/2022/05-supplyStacks/README.md
Normal file
1
challenges/2022/05-supplyStacks/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 5: Supply Stacks](https://adventofcode.com/2022/day/5)
|
15
challenges/2022/05-supplyStacks/benchmark.json
Normal file
15
challenges/2022/05-supplyStacks/benchmark.json
Normal 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
|
||||
}
|
17
challenges/2022/05-supplyStacks/info.json
Normal file
17
challenges/2022/05-supplyStacks/info.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
70
challenges/2022/05-supplyStacks/py/__init__.py
Normal file
70
challenges/2022/05-supplyStacks/py/__init__.py
Normal 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)
|
|
@ -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 |
Loading…
Add table
Add a link
Reference in a new issue