1
0
mirror of https://github.com/chylex/Advent-of-Code.git synced 2024-11-25 23:42:47 +01:00

Compare commits

..

No commits in common. "c2b2da271ba3c8a73f3b687e2b69b9e95bc5e6bf" and "7bd6da2c29c3f70817a1f4c9847da42a0209dd0b" have entirely different histories.

3 changed files with 10 additions and 26 deletions

View File

@ -1,4 +1,4 @@
CALL aoc_load_file_lines('01'); CALL aoc_text_file('01');
CREATE TABLE "01.characters" AS CREATE TABLE "01.characters" AS
SELECT r.row - 1 AS index, r.character SELECT r.row - 1 AS index, r.character
@ -9,7 +9,7 @@ CREATE UNIQUE INDEX "01.characters.index" ON "01.characters" (index);
UPDATE "01.output" UPDATE "01.output"
SET result = ( SET result = (
SELECT SUM(a.character::INT) AS result SELECT SUM(a.character::INTEGER) AS result
FROM "01.characters" a FROM "01.characters" a
INNER JOIN "01.characters" b ON a.index = (b.index + 1) % (SELECT MAX(index) + 1 FROM "01.characters") INNER JOIN "01.characters" b ON a.index = (b.index + 1) % (SELECT MAX(index) + 1 FROM "01.characters")
WHERE a.character = b.character WHERE a.character = b.character
@ -18,7 +18,7 @@ WHERE part = 1;
UPDATE "01.output" UPDATE "01.output"
SET result = ( SET result = (
SELECT SUM(a.character::INT) AS result SELECT SUM(a.character::INTEGER) AS result
FROM "01.characters" a FROM "01.characters" a
INNER JOIN "01.characters" b ON a.index = (b.index + (SELECT (MAX(index) + 1) / 2 FROM "01.characters")) % (SELECT MAX(index) + 1 FROM "01.characters") INNER JOIN "01.characters" b ON a.index = (b.index + (SELECT (MAX(index) + 1) / 2 FROM "01.characters")) % (SELECT MAX(index) + 1 FROM "01.characters")
WHERE a.character = b.character WHERE a.character = b.character

View File

@ -1,19 +1,13 @@
CALL aoc_load_file_lines('02'); CALL aoc_text_file('02');
CALL aoc_input_extract_cells('02', '\t', 'INT');
UPDATE "02.output" SET result = ( UPDATE "02.output" SET result = (
SELECT SUM(row.difference) SELECT SUM(row.difference)
FROM ( FROM (
SELECT MAX(value::INT) - MIN(value::INT) AS difference SELECT MAX(cell::INTEGER) - MIN(cell::INTEGER) AS difference
FROM "02.input.cells" FROM "02.input"
GROUP BY row CROSS JOIN REGEXP_SPLIT_TO_TABLE(input, '\t') cell
GROUP BY input
) row ) row
) WHERE part = 1; ) WHERE part = 1;
UPDATE "02.output" SET result = (
SELECT SUM(a.value / b.value)
FROM "02.input.cells" a
INNER JOIN "02.input.cells" b ON a.row = b.row AND a.value != b.value AND a.value % b.value = 0
) WHERE part = 2;
SELECT aoc_results('02'); SELECT aoc_results('02');

View File

@ -32,24 +32,14 @@ CREATE OR REPLACE FUNCTION aoc_results(day TEXT)
AS AS
$$ $$
BEGIN BEGIN
RETURN QUERY EXECUTE FORMAT('SELECT result FROM %I ORDER BY part', day || '.output'); RETURN QUERY EXECUTE FORMAT('SELECT CONCAT(''Part '', part, '' : '', result) FROM %I WHERE result IS NOT NULL ORDER BY part', day || '.output');
END END
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
CREATE OR REPLACE PROCEDURE aoc_load_file_lines(day TEXT) AS CREATE OR REPLACE PROCEDURE aoc_text_file(day TEXT) AS
$$ $$
BEGIN BEGIN
CALL aoc_setup_tables(day, 'input TEXT'); CALL aoc_setup_tables(day, 'input TEXT');
EXECUTE FORMAT('COPY %I FROM ''/aoc/%s/input.txt'' WITH DELIMITER E''\1''', day || '.input', day); EXECUTE FORMAT('COPY %I FROM ''/aoc/%s/input.txt'' WITH DELIMITER E''\1''', day || '.input', day);
END END
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
CREATE OR REPLACE PROCEDURE aoc_input_extract_cells(day TEXT, delimiter TEXT, value_type TEXT) AS
$$
BEGIN
EXECUTE FORMAT('CREATE TABLE %I AS
SELECT input.row, cell.col, cell.value::%s
FROM (SELECT ROW_NUMBER() OVER () AS row, input AS line FROM %I) input
CROSS JOIN REGEXP_SPLIT_TO_TABLE(input.line, %L) WITH ORDINALITY AS cell(value, col)', day || '.input.cells', value_type, day || '.input', delimiter);
END
$$ LANGUAGE plpgsql;