Skip to content

Instantly share code, notes, and snippets.

@mrwilson
Created December 2, 2020 10:22
Show Gist options
  • Save mrwilson/493ec7f4e5e3e1a922057ac1b2b20182 to your computer and use it in GitHub Desktop.
Save mrwilson/493ec7f4e5e3e1a922057ac1b2b20182 to your computer and use it in GitHub Desktop.
-- Preprocessing the input into a csv e.g. 1,3,a,abcde
-- Creating table:
CREATE TABLE aoc_2(lower int4, upper int4, letter char, password varchar);
--Part 1:
SELECT
count(*)
FROM
aoc_2
WHERE
(char_length(password) - char_length(replace(password, letter, ''))) <@ int4range(lower,upper, '[]');
-- Part 2:
SELECT
count(*)
FROM aoc_2
WHERE
(substring(password, lower,1) = letter)::integer
+ (substring(password,upper,1) = letter)::integer
= 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment