Optimise the snot out of 2024.06

This commit is contained in:
akp 2024-12-07 00:07:02 +00:00
parent 617c90bb14
commit 45c9bcc113
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
5 changed files with 36 additions and 17 deletions

View file

@ -3,3 +3,28 @@
Part 2 is:
* higher than 456
* less than 1689
The first implementation of part 2 took 15 minutes to run.
* Turning `visited_sequence` in `trace` into a set instead of a list cut this to 60 seconds
* Starting immediately in front of the new obstacle when testing positions to put said new obstacle in cut this to 20 seconds
* Removing a load of maps and type wranging to turn the fully generic `gridutil.add` (and counterparts) into a more limited but much faster function took this to ~16 seconds
Before:
```py
def _coordmap(a: AnyCoordinate, b: AnyCoordinate, fn: Callable) -> AnyCoordinate:
at = type(a)
return at(*map(fn, zip(a, b)))
def add(a: AnyCoordinate, b: AnyCoordinate) -> AnyCoordinate:
return _coordmap(a, b, lambda x: x[0] + x[1])
```
After:
```py
def add(a: Coordinate, b: Coordinate) -> Coordinate:
return Coordinate(a.x + b.x, a.y + b.y)
```

View file

@ -63,11 +63,11 @@ def two(instr: str) -> int:
seq = trace(g, start_pos, 0)
known_blocks = set()
for (pos, _) in tqdm(seq, file=sys.stderr):
for (pos, dir) in tqdm(seq, file=sys.stderr):
assert pos in g, "pos off the rails"
g[pos] = "#"
try:
trace(g, start_pos, 0)
trace(g, coord.add(pos, coord.mult(dirs[dir].delta(), -1)), dir)
except LoopEncounteredException:
known_blocks.add(pos)
g[pos] = "."

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 43 KiB

Before After
Before After

View file

@ -8,5 +8,5 @@
{"day": 4, "part": 2, "runner": "py", "min": 0.05280470848083496, "max": 0.06299543380737305, "avg": 0.05627016305923462, "n": 100}
{"day": 5, "part": 1, "runner": "py", "min": 0.02001357078552246, "max": 0.030559301376342773, "avg": 0.02152919292449951, "n": 100}
{"day": 5, "part": 2, "runner": "py", "min": 0.02507805824279785, "max": 0.03197765350341797, "avg": 0.027084295749664308, "n": 100}
{"day": 6, "part": 1, "runner": "py", "min": 0.0671079158782959, "max": 0.0671079158782959, "avg": 0.0671079158782959, "n": 1}
{"day": 6, "part": 2, "runner": "py", "min": 61.63975167274475, "max": 61.63975167274475, "avg": 61.63975167274475, "n": 1}
{"day": 6, "part": 1, "runner": "py", "min": 0.05790352821350098, "max": 0.06762170791625977, "avg": 0.061776439348856606, "n": 6}
{"day": 6, "part": 2, "runner": "py", "min": 15.881408452987671, "max": 17.086341857910156, "avg": 16.64130985736847, "n": 6}

View file

@ -9,22 +9,16 @@ Coordinate3 = namedtuple("Coordinate3", ["x", "y", "z"])
AnyCoordinate = Coordinate | Coordinate3
def _coordmap(a: AnyCoordinate, b: AnyCoordinate, fn: Callable) -> AnyCoordinate:
at = type(a)
return at(*map(fn, zip(a, b)))
def add(a: Coordinate, b: Coordinate) -> Coordinate:
return Coordinate(a.x + b.x, a.y + b.y)
def add(a: AnyCoordinate, b: AnyCoordinate) -> AnyCoordinate:
return _coordmap(a, b, lambda x: x[0] + x[1])
def sub(a: Coordinate, b: Coordinate) -> Coordinate:
return Coordinate(a.x - b.x, a.y - b.y)
def sub(a: AnyCoordinate, b: AnyCoordinate) -> AnyCoordinate:
return _coordmap(a, b, lambda x: x[0] - x[1])
def mult(a: AnyCoordinate, b: Number) -> AnyCoordinate:
at = type(a)
return at(*map(lambda x: x * b, a))
def mult(a: Coordinate, b: Number) -> Coordinate:
return Coordinate(a.x * b, a.y * b)
def manhattan_dist(a: AnyCoordinate, b: AnyCoordinate) -> Number: