diff --git a/challenges/2024/06-guardGallivant/README.md b/challenges/2024/06-guardGallivant/README.md index 9bf820e..b15914b 100644 --- a/challenges/2024/06-guardGallivant/README.md +++ b/challenges/2024/06-guardGallivant/README.md @@ -2,4 +2,29 @@ Part 2 is: * higher than 456 -* less than 1689 \ No newline at end of file +* 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) +``` \ No newline at end of file diff --git a/challenges/2024/06-guardGallivant/main.py b/challenges/2024/06-guardGallivant/main.py index 12b030a..05c7378 100644 --- a/challenges/2024/06-guardGallivant/main.py +++ b/challenges/2024/06-guardGallivant/main.py @@ -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] = "." diff --git a/challenges/2024/benchmark-graph.png b/challenges/2024/benchmark-graph.png index cf7293b..7219472 100644 Binary files a/challenges/2024/benchmark-graph.png and b/challenges/2024/benchmark-graph.png differ diff --git a/challenges/2024/benchmarks.jsonl b/challenges/2024/benchmarks.jsonl index 2b38f29..3d1adfd 100644 --- a/challenges/2024/benchmarks.jsonl +++ b/challenges/2024/benchmarks.jsonl @@ -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} diff --git a/gridutil/coord.py b/gridutil/coord.py index ba973b8..9b4d7f2 100644 --- a/gridutil/coord.py +++ b/gridutil/coord.py @@ -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: