diff --git a/challenges/2024/04-ceresSearch/README.md b/challenges/2024/04-ceresSearch/README.md new file mode 100644 index 0000000..adb5ed9 --- /dev/null +++ b/challenges/2024/04-ceresSearch/README.md @@ -0,0 +1,5 @@ +# [Day 4: Ceres Search](https://adventofcode.com/2024/day/4) + +Part two is: +* less than 1976 +* also, somewhat obviously, less than 1978 \ No newline at end of file diff --git a/challenges/2024/04-ceresSearch/main.py b/challenges/2024/04-ceresSearch/main.py new file mode 100644 index 0000000..aa456e2 --- /dev/null +++ b/challenges/2024/04-ceresSearch/main.py @@ -0,0 +1,81 @@ +import sys +import gridutil.grid as grid +import gridutil.coord as coord + + +def parse(instr: str) -> grid.Grid: + return grid.parse(instr.upper()) + + +def one(instr: str): + wordsearch = parse(instr) + + seq_starts = list( + map(lambda x: x[0], filter(lambda x: x[1] == "X", wordsearch.items())) + ) + detected_sequences = set() + + for start_pos in seq_starts: + for xdir in [-1, 0, 1]: + for ydir in [-1, 0, 1]: + + if xdir == 0 and ydir == 0: + continue + + delta = coord.Coordinate(xdir, ydir) + + ok = True + for i, v in enumerate("XMAS"): + if not ok: + break + + g = wordsearch.get(coord.add(start_pos, coord.mult(delta, i)), "-") + ok = g == v + + if ok: + detected_sequences.add((start_pos, delta)) + + return len(detected_sequences) + + +def check_cross_adjacents(s: str) -> bool: + return s == "SM" or s == "MS" + + +def two(instr: str): + wordsearch = parse(instr) + + seq_starts = list( + map(lambda x: x[0], filter(lambda x: x[1] == "A", wordsearch.items())) + ) + detected_sequences = set() + + for start_pos in seq_starts: + + a = wordsearch.get(coord.add(start_pos, (-1, -1)), "") + wordsearch.get( + coord.add(start_pos, (1, 1)), "" + ) + b = wordsearch.get(coord.add(start_pos, (-1, 1)), "") + wordsearch.get( + coord.add(start_pos, (1, -1)), "" + ) + + if check_cross_adjacents(a) and check_cross_adjacents(b): + detected_sequences.add(start_pos) + + return len(detected_sequences) + + +def _debug(*args, **kwargs): + kwargs["file"] = sys.stderr + print(*args, **kwargs) + + +if __name__ == "__main__": + if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]: + print("Missing day argument", file=sys.stderr) + sys.exit(1) + inp = sys.stdin.read().strip() + if sys.argv[1] == "1": + print(one(inp)) + else: + print(two(inp)) diff --git a/challenges/2024/04-ceresSearch/tests.json b/challenges/2024/04-ceresSearch/tests.json new file mode 100644 index 0000000..9c92e6f --- /dev/null +++ b/challenges/2024/04-ceresSearch/tests.json @@ -0,0 +1,14 @@ +{ + "1": [ + { + "is": "18", + "input": "MMMSXXMASM\nMSAMXMSMSA\nAMXSXMAAMM\nMSAMASMSMX\nXMASAMXAMM\nXXAMMXXAMA\nSMSMSASXSS\nSAXAMASAAA\nMAMMMXMMMM\nMXMXAXMASX\n\n" + } + ], + "2": [ + { + "is": "9", + "input": "MMMSXXMASM\nMSAMXMSMSA\nAMXSXMAAMM\nMSAMASMSMX\nXMASAMXAMM\nXXAMMXXAMA\nSMSMSASXSS\nSAXAMASAAA\nMAMMMXMMMM\nMXMXAXMASX\n\n" + } + ] +} \ No newline at end of file diff --git a/challenges/2024/README.md b/challenges/2024/README.md index 3d13da2..735acb0 100644 --- a/challenges/2024/README.md +++ b/challenges/2024/README.md @@ -4,7 +4,7 @@ Solutions to the [2024 Advent of Code](https://adventofcode.com/2024)! --- -Total stars: **6 ★** +Total stars: **8 ★** ![Benchmark graph](./benchmark-graph.png) @@ -16,4 +16,5 @@ A day denoted with a star means it has a visualisation. |-------------------------------------|--------|----------------------|-------| | 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. | | 02 - Red-Nosed Reindeer | ★ ★ | Python || -| 03 - Mull It Over | ★ ★ | Python | The first instance of Advent of Parsing this year! | \ No newline at end of file +| 03 - Mull It Over | ★ ★ | Python | The first instance of Advent of Parsing this year! | +| 04 - Ceres Search | ★ ★ | Python | When it says a cross, it does not mean a plus. | \ No newline at end of file diff --git a/challenges/2024/benchmark-graph.png b/challenges/2024/benchmark-graph.png index 9b0aa14..a0f44ed 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 66053fb..4b1a70b 100644 --- a/challenges/2024/benchmarks.jsonl +++ b/challenges/2024/benchmarks.jsonl @@ -4,3 +4,5 @@ {"day": 2, "part": 2, "runner": "py", "min": 0.02489948272705078, "max": 0.0319674015045166, "avg": 0.026371052265167238, "n": 100} {"day": 3, "part": 1, "runner": "py", "min": 0.02116227149963379, "max": 0.03153491020202637, "avg": 0.022634525299072266, "n": 100} {"day": 3, "part": 2, "runner": "py", "min": 0.02115607261657715, "max": 0.03121805191040039, "avg": 0.022722084522247315, "n": 100} +{"day": 4, "part": 1, "runner": "py", "min": 0.17342424392700195, "max": 0.3778045177459717, "avg": 0.18848238706588746, "n": 100} +{"day": 4, "part": 2, "runner": "py", "min": 0.05280470848083496, "max": 0.06299543380737305, "avg": 0.05627016305923462, "n": 100}