Created
December 6, 2022 20:51
-
-
Save twopoint718/d2de952241b4a7e143edd7a5ccc08993 to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 4 in SQL (run with `\I program_04.sql` from psql console)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# Just reformats the input into Postgres inclusive range syntax in CSV: | |
# 2-4,6-8 => "[2,4]","[6,8]" | |
File.readlines('input_04.txt').each do |line| | |
r1, r2 = line.split(',') | |
a, b = r1.split('-') | |
x, y = r2.chomp.split('-') | |
puts "\"[#{a},#{b}]\",\"[#{x},#{y}]\"" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drop table if exists camp_cleanups; | |
create table camp_cleanups ( | |
id serial primary key, | |
r1 int4range not null, | |
r2 int4range not null | |
); | |
\copy camp_cleanups (r1, r2) from program './formatter.rb' with csv; | |
-- part 1 | |
select | |
sum( | |
case when (r1 <@ r2 or r1 @> r2) then 1 | |
else 0 | |
end | |
) | |
from camp_cleanups; | |
-- part 2 | |
select | |
sum( | |
case when r1 && r2 then 1 | |
else 0 | |
end | |
) | |
from camp_cleanups; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment