Planet PostgreSQL

Planet PostgreSQL -

Greg Sabino Mullane: How to Solve Advent of Code 2022 Using Postgres - Day 13

Disclaimer This article will contain spoilers both on how I solved 2022 Day 13's challenge "Distress Signal" using SQL, as well as general ideas on how to approach the problem. I recommend trying to solve it yourself first, using your favorite language. This article is delayed from the actual puzzle's release. Also note that my solutions are seldom going to be the "best" solutions - they were solved as quickly as possible, and these articles will show my first solutions, with some minor reformatting and cleaning up. Hands on Tutorial We've also loaded a tutorial for Day 13's challenge if you want to try it with a pre-loaded data set. AOC Day 13 This puzzle was overall very straightforward. The tech we used this time: file_fdw to slurp in our puzzle input file The lag() function to allow us to view the previous row as we parse the file Using a sequence to do some counting The regexp_replace() function to sneakily remove our double-digit numbers The overlay() function to modify our strings in place as we are parsing them For this challenge, the goal was to put packets in order, in which each pair consisted of deeply nested arrays like so: [1,1,3,1,1] [1,1,5,1,1] [[1],[2,3,4]] [[1],4] [9] [[8,7,6]] [[4,4],4,4] [[4,4],4,4,4] [7,7,7,7] [7,7,7] [] [3] [[[]]] [[]] [1,[2,[3,[4,[5,6,7]]]],8,9] [1,[2,[3,[4,[5,6,0]]]],8,9] As usual, we create a custom schema and use file_fdw to access the input file: CREATE EXTENSION if not exists file_fdw; CREATE SERVER if not exists aoc2022 FOREIGN DATA WRAPPER file_fdw; DROP SCHEMA if exists aoc_signal CASCADE; CREATE SCHEMA aoc_signal; SET search_path = aoc_signal; DROP FOREIGN TABLE if exists aoc_day13; CREATE FOREIGN TABLE aoc_day13 (line text) SERVER aoc2022 options(filename '/tmp/aoc2022.day13.input'); DROP SEQUENCE if exists aoc; CREATE SEQUENCE aoc; There are specific rules for determining the correct order based on the numeric values, how to handle arrays compared to non-arrays, etc. While I suspec[...]

greg postgres sabino

相关推荐 去reddit讨论

热榜 Top10

eolink
eolink
观测云
观测云
LigaAI
LigaAI
Dify.AI
Dify.AI

推荐或自荐