Add 2021-13 in Nim and perform code formatting

Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
akp 2021-12-13 23:14:05 +00:00
parent 92fccdec98
commit a5475968b8
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
5 changed files with 101 additions and 18 deletions

View file

@ -2,13 +2,21 @@
"day": 13,
"dir": "challenges/2021/13-transparentOrigami",
"implementations": {
"Nim": {
"part.1.avg": 0.0015621956039999975,
"part.1.max": 0.003880651,
"part.1.min": 0.00031427,
"part.2.avg": 0.002502785954,
"part.2.max": 0.005894322,
"part.2.min": 0.000524984
},
"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
"part.1.avg": 0.0020371160507202148,
"part.1.max": 0.007749795913696289,
"part.1.min": 0.0005323886871337891,
"part.2.avg": 0.005764168262481689,
"part.2.max": 0.01631307601928711,
"part.2.min": 0.0014383792877197266
}
},
"numRuns": 1000

View file

@ -0,0 +1,79 @@
import std/tables
import std/strutils
import std/sequtils
type
Point = (int, int)
Sheet = Table[Point, bool]
Instruction = object
axis: string
n: int
proc parse(instr: string): (Sheet, seq[Instruction]) =
let
sp = instr.strip.split("\n\n")
sheetPoints = sp[0]
rawInstructions = sp[1]
var sheet = Sheet()
for point in sheetPoints.splitlines:
let x = point.split(",")
sheet[(parseInt(x[0]), parseInt(x[1]))] = true
var instructions: seq[Instruction]
for line in rawInstructions.splitlines:
let x = line.split(" ")[^1].split("=")
instructions.add(Instruction(axis: x[0], n: parseInt(x[1])))
return (sheet, instructions)
proc print(sheet: Sheet) =
let
keysSeq = toSeq(sheet.keys)
maxX = max(map(keysSeq, (proc(p: Point): int = p[0])))
maxY = max(map(keysSeq, (proc(p: Point): int = p[1])))
var lines: seq[string]
for y in 0..maxY:
var ll: string
for x in 0..maxX:
if sheet.hasKey((x, y)):
ll.add("██")
else:
ll.add(" ")
lines.add(ll)
for x in lines:
echo x
proc applyInstruction(sheet: Sheet, instruction: Instruction): Sheet =
for p in sheet.keys:
var v: int
case instruction.axis
of "x":
v = p[0]
of "y":
v = p[1]
else:
raise newException(ValueError, "unknown axis")
if v > instruction.n:
if instruction.axis == "x":
result[(instruction.n - (p[0] - instruction.n), p[1])] = true
else: # assume y
result[(p[0], instruction.n - (p[1] - instruction.n))] = true
else:
result[p] = true
proc partOne*(instr: string): int =
var (sheet, instructions) = parse(instr)
sheet = apply_instruction(sheet, instructions[0])
return sheet.len
proc partTwo*(instr: string): string =
var (sheet, instructions) = parse(instr)
for instruction in instructions:
sheet = applyInstruction(sheet, instruction)
sheet.print
return "work it out yourself"

View file

@ -19,7 +19,7 @@ class Instruction:
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(",")
@ -28,12 +28,12 @@ def parse(instr: str) -> Tuple[Sheet, List[Instruction]]:
ins = []
for line in instructions.splitlines():
m = INSTRUCTION_REGEX.match(line)
ins.append(Instruction(
m.group(1),
int(m.group(2)),
ins.append(
Instruction(
m.group(1),
int(m.group(2)),
)
)
)
return sheet, ins
@ -52,9 +52,6 @@ def print_sheet(sheet: Sheet):
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":
@ -66,7 +63,7 @@ def apply_instruction(sheet: Sheet, instruction: Instruction) -> Sheet:
elif instruction.axis == "y":
return (p[0], instruction.n - (p[1] - instruction.n))
raise ValueError(f"unknown axis {instruction.axis}")
return p
new_sheet = {}
@ -77,7 +74,6 @@ def apply_instruction(sheet: Sheet, instruction: Instruction) -> Sheet:
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
sheet, instructions = parse(instr)

View file

@ -22,7 +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. :( |
| 13 - Transparent Origami | Complete | [Python](13-transparentOrigami/py), [Nim](13-transparentOrigami/nim) | I got stuck for hours on an intermittent off-by-one error. :( |
<!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

After

Width:  |  Height:  |  Size: 50 KiB

Before After
Before After