Re:Advent of Code 2021 with PostgreSQL
by vimsee on Tuesday December 07, 2021 @05:20AM
(#29470892)
I think its awesome that many people find ways to solve these with different rules making the challenge harder.
As a person that is a couple of years into programming, I am also going in with som personal rules.
I try to solve this years AoC with as few if statements as possible using only Pythons standard library.
I have completed day 1 to day 4 with 0 if statements. https://github.com/emilbratt/adventofcode/tree/main/2021
Having said that, I don`t agree on changing the input files as I see many people do.
I don`t know if it is feasible to solve in SQL without doing so, so take it with a grain of salt.
For instance, take day 4s input.
I`ve seen many split the random numbers into one file and the bingo board numbers into another. I`ve also seen people using their IDE to remove all leading white space for the single digits for the bingo board numbers to make it easier to parse programmatically.
In my opinion, this defeats a major purpose of the challenge. It is in fact a programming challenge.
[ Parent
| on HN ]
You can use regexes to help you split the file into pieces (this is in PostgreSQL), I expect other dbms's to have similar functions available.
SELECT
lineno::int AS line,
((lineno-3)/6)::smallint AS card,
((lineno-3)%6)::smallint AS y,
(col - 1)::smallint AS x,
value::smallint AS value
FROM
regexp_split_to_table($1, '\n') WITH ORDINALITY AS sub(line, lineno)
CROSS JOIN
regexp_split_to_table(ltrim(line, ' '), '(\s+|,)') WITH ORDINALITY AS sub2(value, col)
WHERE
line != ''
AND value != ''I did not know about regexp_split_to_table() or regex functions in general in dbms, but that is very neat.
I tried to complete all of them in Spreadsheet, so far only Day 5 is quite tough and Google Sheet could not handle it..
https://twitter.com/chenyuio/status/1467750483494273025 (Day6- in separate sheet to avoid spoilers)