2022-09 nim

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-09 17:07:46 +00:00
parent 9c3e041e7e
commit 7310a62308
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
5 changed files with 112 additions and 13 deletions

View file

@ -2,13 +2,21 @@
"day": 9,
"dir": "challenges/2022/09-ropeBridge",
"implementations": {
"Nim": {
"part.1.avg": 0.03409326195600002,
"part.1.max": 0.065706291,
"part.1.min": 0.027704718,
"part.2.avg": 0.005110148128000001,
"part.2.max": 0.01591038,
"part.2.min": 0.00411792
},
"Python": {
"part.1.avg": 0.40573710823059084,
"part.1.max": 0.5149297714233398,
"part.1.min": 0.3336000442504883,
"part.2.avg": 0.08495945644378662,
"part.2.max": 0.13427042961120605,
"part.2.min": 0.0677499771118164
"part.1.avg": 0.40838007831573486,
"part.1.max": 0.5554301738739014,
"part.1.min": 0.3740715980529785,
"part.2.avg": 0.08457976627349853,
"part.2.max": 0.13773822784423828,
"part.2.min": 0.07479453086853027
}
},
"numRuns": 250

View file

@ -0,0 +1,95 @@
import std/sequtils
import std/strutils
import std/tables
type
Vector = (int, int)
Instruction = (char, int)
const
LEFT = 'L'
RIGHT = 'R'
UP = 'U'
DOWN = 'D'
OFFSETS = {
RIGHT: (1, 0),
LEFT: (-1, 0),
UP: (0, 1),
DOWN: (0, -1),
}.toTable()
proc parse(instr: string): seq[Instruction] =
for x in instr.strip().splitlines():
result.add((x[0], parseInt(x[2..<x.len])))
proc `+`(a, b: Vector): Vector =
return (a[0] + b[0], a[1] + b[1])
proc `-`(a, b: Vector): Vector =
return (a[0] - b[0], a[1] - b[1])
proc getNextMoveDelta(currentPosition, preceedingPosition: Vector): Vector =
let (dx, dy) = preceedingPosition - currentPosition
if -1 <= dx and dx <= 1 and -1 <= dy and dy <= 1:
return (0, 0)
if dy > 0 and dx == 0:
# head upwards of tail
return OFFSETS[UP]
elif dy < 0 and dx == 0:
# head downwards of tail
return OFFSETS[DOWN]
elif dy == 0 and dx > 0:
# head to the right of tail
return OFFSETS[RIGHT]
elif dy == 0 and dx < 0:
# head to the left of tail
return OFFSETS[LEFT]
elif dy > 0 and dx > 0:
# head diagonally up-right of the tail
return OFFSETS[UP] + OFFSETS[RIGHT]
elif dy > 0 and dx < 0:
# head diagonally up-left of the tail
return OFFSETS[UP] + OFFSETS[LEFT]
elif dy < 0 and dx > 0:
# head diagonally down-right of the tail
return OFFSETS[DOWN] + OFFSETS[RIGHT]
elif dy < 0 and dx < 0:
# head diagonally down-left of the tail
return OFFSETS[DOWN] + OFFSETS[LEFT]
return (0, 0)
proc runWithLength(instructions: seq[Instruction], ropeLength: int): int =
var
positions: seq[Vector] = newSeq[Vector](ropeLength)
tailVisited: seq[Vector] = [(0, 0)].toSeq()
for (direction, magnitude) in instructions:
for _ in countup(0, magnitude - 1):
positions[0] = positions[0] + OFFSETS[direction]
var delta: Vector
for posNum in countup(1, positions.len - 1):
delta = getNextMoveDelta(positions[posNum], positions[posNum-1])
if delta == (0, 0):
break
positions[posNum] = positions[posNum] + delta
if delta == (0, 0):
continue
let last = positions[positions.len-1]
if not tailVisited.contains(last):
tailVisited.add(last)
return len(tailVisited)
proc partOne*(instr: string): int =
return runWithLength(parse(instr), 2)
proc partTwo*(instr: string): int =
return runWithLength(parse(instr), 10)

View file

@ -21,10 +21,6 @@ def parse(instr: str) -> List[Instruction]:
return [(x[0], int(x[2:])) for x in instr.strip().splitlines()]
def apply_delta(coord: Vector, delta: Vector) -> Vector:
return (coord[0] + delta[0], coord[1] + delta[1])
def vector_sum(a: Vector, b: Vector) -> Vector:
return (a[0] + b[0], a[1] + b[1])
@ -76,14 +72,14 @@ def run_with_length(instructions: List[Instruction], length: int) -> int:
for (direction, magnitude) in instructions:
for _ in range(magnitude):
positions[0] = apply_delta(positions[0], OFFSETS[direction])
positions[0] = vector_sum(positions[0], OFFSETS[direction])
for pos_num in range(1, len(positions)):
delta = get_next_move_delta(positions[pos_num], positions[pos_num - 1])
if delta is None:
break
positions[pos_num] = apply_delta(positions[pos_num], delta)
positions[pos_num] = vector_sum(positions[pos_num], delta)
if delta is None:
# Python scope nastiness coming in useful for once

View file

@ -22,5 +22,5 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
| 06 - Tuning Trouble | ★ ★ | [Python](06-tuningTrouble/py), [Nim](06-tuningTrouble/nim) | This is the first year I've not repeatedly forgotten about the existence of sets, and it's coming in quite handy. |
| 07 - No Space Left On Device | ★ ★ | [Python](07-noSpaceLeftOnDevice/py) | Turns out that fake file systems are prone to very subtle and infuriating bugs. |
| 08 - Treetop Tree House | ★ ★ | [Python](08-treetopTreeHouse/py) | Magical coordinate dictionary tuple things do be magical. |
| 09 - Rope Bridge | ★ ★ | [Python](09-ropeBridge/py) | Does this count as this year's first cellular automata? |
| 09 - Rope Bridge | ★ ★ | [Python](09-ropeBridge/py), [Nim](09-ropeBridge/nim) | Does this count as this year's first cellular automata? |

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 61 KiB

Before After
Before After