Add 2021-13 in Python
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
cc86ef7de9
commit
92fccdec98
6 changed files with 123 additions and 0 deletions
2
challenges/2021/13-transparentOrigami/README.md
Normal file
2
challenges/2021/13-transparentOrigami/README.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
# [Day 13: Transparent Origami](https://adventofcode.com/2021/day/13)
|
||||
|
15
challenges/2021/13-transparentOrigami/benchmark.json
Normal file
15
challenges/2021/13-transparentOrigami/benchmark.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"day": 13,
|
||||
"dir": "challenges/2021/13-transparentOrigami",
|
||||
"implementations": {
|
||||
"Python": {
|
||||
"part.1.avg": 0.002360786199569702,
|
||||
"part.1.max": 0.004988908767700195,
|
||||
"part.1.min": 0.0005254745483398438,
|
||||
"part.2.avg": 0.006699836015701294,
|
||||
"part.2.max": 0.014143228530883789,
|
||||
"part.2.min": 0.001451253890991211
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
12
challenges/2021/13-transparentOrigami/info.json
Normal file
12
challenges/2021/13-transparentOrigami/info.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "6,10\n0,14\n9,10\n0,3\n10,4\n4,11\n6,0\n6,12\n4,1\n0,13\n10,12\n3,4\n3,0\n8,4\n1,10\n2,14\n8,10\n9,0\n\nfold along y=7\nfold along x=5",
|
||||
"expected": "17"
|
||||
}
|
||||
],
|
||||
"two": []
|
||||
}
|
||||
}
|
93
challenges/2021/13-transparentOrigami/py/__init__.py
Normal file
93
challenges/2021/13-transparentOrigami/py/__init__.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
import re
|
||||
from typing import Dict, Tuple, List
|
||||
from aocpy import BaseChallenge
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
Point = Tuple[int, int]
|
||||
Sheet = Dict[Point, bool]
|
||||
|
||||
|
||||
INSTRUCTION_REGEX = re.compile(r"fold along (x|y)=(\d+)")
|
||||
|
||||
|
||||
@dataclass
|
||||
class Instruction:
|
||||
axis: str
|
||||
n: int
|
||||
|
||||
|
||||
def parse(instr: str) -> Tuple[Sheet, List[Instruction]]:
|
||||
sheetPoints, instructions = instr.strip().split("\n\n")
|
||||
|
||||
sheet = {}
|
||||
for point in sheetPoints.splitlines():
|
||||
x, y = point.split(",")
|
||||
sheet[(int(x), int(y))] = True
|
||||
|
||||
ins = []
|
||||
for line in instructions.splitlines():
|
||||
m = INSTRUCTION_REGEX.match(line)
|
||||
ins.append(Instruction(
|
||||
m.group(1),
|
||||
int(m.group(2)),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
return sheet, ins
|
||||
|
||||
|
||||
def print_sheet(sheet: Sheet):
|
||||
max_x = max(x for x, y in sheet)
|
||||
max_y = max(y for x, y in sheet)
|
||||
lines = []
|
||||
for y in range(max_y + 1):
|
||||
ll = ""
|
||||
for x in range(max_x + 1):
|
||||
ll += "██" if sheet.get((x, y), False) else " "
|
||||
lines.append(ll)
|
||||
for x in lines:
|
||||
print(x, flush=True)
|
||||
|
||||
|
||||
def apply_instruction(sheet: Sheet, instruction: Instruction) -> Sheet:
|
||||
|
||||
# max_val = max((x for x, _ in sheet) if instruction.axis == "x" else (y for _, y in sheet))
|
||||
|
||||
def mod_point(p: Point) -> Point:
|
||||
v = p[0]
|
||||
if instruction.axis == "y":
|
||||
v = p[1]
|
||||
|
||||
if v > instruction.n:
|
||||
if instruction.axis == "x":
|
||||
return (instruction.n - (p[0] - instruction.n), p[1])
|
||||
elif instruction.axis == "y":
|
||||
return (p[0], instruction.n - (p[1] - instruction.n))
|
||||
raise ValueError(f"unknown axis {instruction.axis}")
|
||||
|
||||
return p
|
||||
|
||||
new_sheet = {}
|
||||
for point in sheet:
|
||||
new_sheet[mod_point(point)] = True
|
||||
|
||||
return new_sheet
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
sheet, instructions = parse(instr)
|
||||
sheet = apply_instruction(sheet, instructions[0])
|
||||
return sum(sheet.values())
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> str:
|
||||
sheet, instructions = parse(instr)
|
||||
for instruction in instructions:
|
||||
sheet = apply_instruction(sheet, instruction)
|
||||
print_sheet(sheet)
|
||||
return "work it out yourself"
|
|
@ -22,6 +22,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
|
|||
| 10 - Syntax Scoring | Complete | [Python](10-syntaxScoring/py) | I can't say I've ever broken something so thoroughly that it has a syntax error on *every* line... |
|
||||
| 11 - Dumbo Octopus | Complete | [Python](11-dumboOctopus/py), [Nim](11-dumboOctopus/nim) | Cellular automata my beloved <3 |
|
||||
| 12 - Passage Pathing | Complete | [Go](12-passagePathing/go) | I couldn't tell you how it works, but it does kinda work and I think I have a vague idea (external help was used). |
|
||||
| 13 - Transparent Origami | Complete | [Python](13-transparentOrigami/py) | I got stuck for hours on an intermittent off-by-one error. :( |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 50 KiB |
Loading…
Add table
Add a link
Reference in a new issue