This commit is contained in:
akp 2024-12-04 16:47:03 +00:00
parent 548d1a42e7
commit 846f8e73c2
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
6 changed files with 105 additions and 2 deletions

View file

@ -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

View file

@ -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))

View file

@ -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"
}
]
}

View file

@ -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)
@ -17,3 +17,4 @@ 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! |
| 04 - Ceres Search | ★ ★ | Python | When it says a cross, it does not mean a plus. |

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 32 KiB

Before After
Before After

View file

@ -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}