2024.04
This commit is contained in:
parent
548d1a42e7
commit
846f8e73c2
6 changed files with 105 additions and 2 deletions
5
challenges/2024/04-ceresSearch/README.md
Normal file
5
challenges/2024/04-ceresSearch/README.md
Normal 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
|
81
challenges/2024/04-ceresSearch/main.py
Normal file
81
challenges/2024/04-ceresSearch/main.py
Normal 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))
|
14
challenges/2024/04-ceresSearch/tests.json
Normal file
14
challenges/2024/04-ceresSearch/tests.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ Solutions to the [2024 Advent of Code](https://adventofcode.com/2024)!
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Total stars: **6 ★**
|
Total stars: **8 ★**
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
@ -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. |
|
| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. |
|
||||||
| 02 - Red-Nosed Reindeer | ★ ★ | Python ||
|
| 02 - Red-Nosed Reindeer | ★ ★ | Python ||
|
||||||
| 03 - Mull It Over | ★ ★ | Python | The first instance of Advent of Parsing this year! |
|
| 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 |
|
@ -4,3 +4,5 @@
|
||||||
{"day": 2, "part": 2, "runner": "py", "min": 0.02489948272705078, "max": 0.0319674015045166, "avg": 0.026371052265167238, "n": 100}
|
{"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": 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": 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}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue