mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Moderately cursed 2025 day 4 part 1
This commit is contained in:
11
2025/day04/README.md
Normal file
11
2025/day04/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Day 04: SQLite sql
|
||||
|
||||
Did you know SQLite does not have procedural extensions? I did not when I started this. Regardless,
|
||||
to run:
|
||||
|
||||
```console
|
||||
$ sqlite3 < solve.sql
|
||||
part1
|
||||
-----
|
||||
13
|
||||
```
|
||||
10
2025/day04/sample.txt
Normal file
10
2025/day04/sample.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
47
2025/day04/solve.sql
Normal file
47
2025/day04/solve.sql
Normal file
@@ -0,0 +1,47 @@
|
||||
-- First import raw data into a table so we can work
|
||||
CREATE TABLE raw_data(line_data VARCHAR NOT NULL);
|
||||
.import --csv './sample.txt' raw_data
|
||||
-- Then use the auto-incrementing ID to add a y coordinate
|
||||
CREATE TABLE grid_lines(
|
||||
y INTEGER PRIMARY KEY,
|
||||
line_data VARCHAR NOT NULL
|
||||
);
|
||||
INSERT INTO grid_lines(line_data)
|
||||
SELECT line_data
|
||||
FROM raw_data;
|
||||
-- Now create a table to hold the paper rolls
|
||||
CREATE TABLE rolls(
|
||||
x INTEGER NOT NULL,
|
||||
y INTEGER NOT NULL,
|
||||
PRIMARY KEY (x, y)
|
||||
);
|
||||
WITH RECURSIVE cte AS (
|
||||
SELECT y,
|
||||
1 x,
|
||||
line_data,
|
||||
substr(line_data, 1, 1) c
|
||||
FROM grid_lines
|
||||
UNION ALL
|
||||
SELECT y,
|
||||
x + 1,
|
||||
line_data,
|
||||
substr(line_data, x + 1, 1)
|
||||
FROM cte
|
||||
WHERE x <= length(line_data)
|
||||
)
|
||||
INSERT INTO rolls
|
||||
SELECT x,
|
||||
y
|
||||
FROM cte
|
||||
WHERE c = '@';
|
||||
-- Now compute part 1
|
||||
SELECT COUNT(*) as part1
|
||||
FROM rolls r
|
||||
WHERE (
|
||||
SELECT COUNT(*) - 1
|
||||
FROM rolls o
|
||||
WHERE o.x >= r.x - 1
|
||||
AND o.x <= r.x + 1
|
||||
AND o.y >= r.y - 1
|
||||
AND o.y <= r.y + 1
|
||||
) < 4;
|
||||
Reference in New Issue
Block a user