Skip to content

Commit 846f8e7

Browse files
committed
2024.04
1 parent 548d1a4 commit 846f8e7

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [Day 4: Ceres Search](https://adventofcode.com/2024/day/4)
2+
3+
Part two is:
4+
* less than 1976
5+
* also, somewhat obviously, less than 1978
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import sys
2+
import gridutil.grid as grid
3+
import gridutil.coord as coord
4+
5+
6+
def parse(instr: str) -> grid.Grid:
7+
return grid.parse(instr.upper())
8+
9+
10+
def one(instr: str):
11+
wordsearch = parse(instr)
12+
13+
seq_starts = list(
14+
map(lambda x: x[0], filter(lambda x: x[1] == "X", wordsearch.items()))
15+
)
16+
detected_sequences = set()
17+
18+
for start_pos in seq_starts:
19+
for xdir in [-1, 0, 1]:
20+
for ydir in [-1, 0, 1]:
21+
22+
if xdir == 0 and ydir == 0:
23+
continue
24+
25+
delta = coord.Coordinate(xdir, ydir)
26+
27+
ok = True
28+
for i, v in enumerate("XMAS"):
29+
if not ok:
30+
break
31+
32+
g = wordsearch.get(coord.add(start_pos, coord.mult(delta, i)), "-")
33+
ok = g == v
34+
35+
if ok:
36+
detected_sequences.add((start_pos, delta))
37+
38+
return len(detected_sequences)
39+
40+
41+
def check_cross_adjacents(s: str) -> bool:
42+
return s == "SM" or s == "MS"
43+
44+
45+
def two(instr: str):
46+
wordsearch = parse(instr)
47+
48+
seq_starts = list(
49+
map(lambda x: x[0], filter(lambda x: x[1] == "A", wordsearch.items()))
50+
)
51+
detected_sequences = set()
52+
53+
for start_pos in seq_starts:
54+
55+
a = wordsearch.get(coord.add(start_pos, (-1, -1)), "") + wordsearch.get(
56+
coord.add(start_pos, (1, 1)), ""
57+
)
58+
b = wordsearch.get(coord.add(start_pos, (-1, 1)), "") + wordsearch.get(
59+
coord.add(start_pos, (1, -1)), ""
60+
)
61+
62+
if check_cross_adjacents(a) and check_cross_adjacents(b):
63+
detected_sequences.add(start_pos)
64+
65+
return len(detected_sequences)
66+
67+
68+
def _debug(*args, **kwargs):
69+
kwargs["file"] = sys.stderr
70+
print(*args, **kwargs)
71+
72+
73+
if __name__ == "__main__":
74+
if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]:
75+
print("Missing day argument", file=sys.stderr)
76+
sys.exit(1)
77+
inp = sys.stdin.read().strip()
78+
if sys.argv[1] == "1":
79+
print(one(inp))
80+
else:
81+
print(two(inp))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"1": [
3+
{
4+
"is": "18",
5+
"input": "MMMSXXMASM\nMSAMXMSMSA\nAMXSXMAAMM\nMSAMASMSMX\nXMASAMXAMM\nXXAMMXXAMA\nSMSMSASXSS\nSAXAMASAAA\nMAMMMXMMMM\nMXMXAXMASX\n\n"
6+
}
7+
],
8+
"2": [
9+
{
10+
"is": "9",
11+
"input": "MMMSXXMASM\nMSAMXMSMSA\nAMXSXMAAMM\nMSAMASMSMX\nXMASAMXAMM\nXXAMMXXAMA\nSMSMSASXSS\nSAXAMASAAA\nMAMMMXMMMM\nMXMXAXMASX\n\n"
12+
}
13+
]
14+
}

challenges/2024/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Solutions to the [2024 Advent of Code](https://adventofcode.com/2024)!
44

55
---
66

7-
Total stars: **6**
7+
Total stars: **8**
88

99
![Benchmark graph](./benchmark-graph.png)
1010

@@ -16,4 +16,5 @@ A day denoted with a star means it has a visualisation.
1616
|-------------------------------------|--------|----------------------|-------|
1717
| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. |
1818
| 02 - Red-Nosed Reindeer | ★ ★ | Python ||
19-
| 03 - Mull It Over | ★ ★ | Python | The first instance of Advent of Parsing this year! |
19+
| 03 - Mull It Over | ★ ★ | Python | The first instance of Advent of Parsing this year! |
20+
| 04 - Ceres Search | ★ ★ | Python | When it says a cross, it does not mean a plus. |

challenges/2024/benchmark-graph.png

4.6 KB
Loading

challenges/2024/benchmarks.jsonl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
{"day": 2, "part": 2, "runner": "py", "min": 0.02489948272705078, "max": 0.0319674015045166, "avg": 0.026371052265167238, "n": 100}
55
{"day": 3, "part": 1, "runner": "py", "min": 0.02116227149963379, "max": 0.03153491020202637, "avg": 0.022634525299072266, "n": 100}
66
{"day": 3, "part": 2, "runner": "py", "min": 0.02115607261657715, "max": 0.03121805191040039, "avg": 0.022722084522247315, "n": 100}
7+
{"day": 4, "part": 1, "runner": "py", "min": 0.17342424392700195, "max": 0.3778045177459717, "avg": 0.18848238706588746, "n": 100}
8+
{"day": 4, "part": 2, "runner": "py", "min": 0.05280470848083496, "max": 0.06299543380737305, "avg": 0.05627016305923462, "n": 100}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy