Optimise 2022-09

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-09 20:50:12 +00:00
parent 4ea4d25176
commit 4b9273b751
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
4 changed files with 17 additions and 18 deletions

View file

@ -3,21 +3,21 @@
"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
"part.1.avg": 0.002931661109000006,
"part.1.max": 0.007730762,
"part.1.min": 0.000887947,
"part.2.avg": 0.004008995328999999,
"part.2.max": 0.011666485,
"part.2.min": 0.001218443
},
"Python": {
"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
"part.1.avg": 0.015390190362930298,
"part.1.max": 0.03382277488708496,
"part.1.min": 0.010709762573242188,
"part.2.avg": 0.0444308967590332,
"part.2.max": 0.14687561988830566,
"part.2.min": 0.03348994255065918
}
},
"numRuns": 250
"numRuns": 1000
}

View file

@ -1,4 +1,3 @@
import std/sequtils
import std/strutils
import std/tables
@ -65,7 +64,7 @@ proc getNextMoveDelta(currentPosition, preceedingPosition: Vector): Vector =
proc runWithLength(instructions: seq[Instruction], ropeLength: int): int =
var
positions: seq[Vector] = newSeq[Vector](ropeLength)
tailVisited: seq[Vector] = [(0, 0)].toSeq()
tailVisited: Table[Vector, int] = {(0, 0): 0}.toTable()
for (direction, magnitude) in instructions:
for _ in countup(0, magnitude - 1):
@ -84,7 +83,7 @@ proc runWithLength(instructions: seq[Instruction], ropeLength: int): int =
let last = positions[positions.len-1]
if not tailVisited.contains(last):
tailVisited.add(last)
tailVisited[last] = 0
return len(tailVisited)

View file

@ -66,7 +66,7 @@ def get_next_move_delta(
def run_with_length(instructions: List[Instruction], length: int) -> int:
tail_visited: List[Vector] = [(0, 0)]
tail_visited: Dict[Vector, None] = {(0, 0): None}
positions: List[Vector] = [(0, 0) for _ in range(length)]
@ -86,7 +86,7 @@ def run_with_length(instructions: List[Instruction], length: int) -> int:
continue
if positions[-1] not in tail_visited:
tail_visited.append(positions[-1])
tail_visited[positions[-1]] = None
return len(tail_visited)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Before After
Before After