After you finished hacking the plane, your laptop dies: you forgot to charge it! Rookie mistake, or the calculations do consume a lot of power. The plane has a power outlet, but somehow it's a non-standard one, you need an adapter. But first a cocktail! Then you empty your backback on the seat next to you and find out that you have dozens of different adapters, which you can stack together. None of them really work, and hopefully you don't blow another fuse, or set the plane on fire!
The adapters you have can transform an imaginary Jolts value to another Jolts value, up to 3 Jolts up. And you find out that your laptop charger jumps 3 Jolts more than the highest rated adapter you have, although right now you don't know either of these value ... And since your laptop died, you need to calculate this on paper. Here is the list of 90 adapters in your backpack. Good luck!
Task 1: If you plug all 90 adapters together (Hopefully the plane is long enough, and the flight attendant does not spot what you are doing), what is the distribution of Joltage differences between the outlet (with 0 Jolts - seriously, no juice to begin with?), all adapters, and your +3 Jolts charger? Find a set that uses all adapters, calculate the Joltage differences, and multiply the +1 with the +3 differences. Seriously?
Task 2: Someone things that 90 adapters are not enough. Figure out in how many ways the 90 adapters can be arranged. On paper, because your laptop is still dead.
Not much, just load the data. I changed the BIGINT to INT; sufficient for today.
DROP TABLE IF EXISTS day10; CREATE TABLE day10 ( id SERIAL NOT NULL PRIMARY KEY, value INT ); COPY day10 (value) FROM STDIN; 114 51 122 26 121 90 20 113 ... \.
And I'm curious:
SELECT COUNT(*) FROM day10; count ------- 90 (1 row) SELECT MAX(value) FROM day10; max ----- 140 (1 row) SELECT MIN(value) FROM day10; min ----- 1 (1 row)
90 adapters. Seriously? With a Jolt spread between 1 and 140. Again, hopefully this plane will not go up in flames.
WITH day10_calc AS ( SELECT value - LAG(CAST(value AS INT), 1, 0) OVER (ORDER BY value) AS difference FROM day10 )
This produces the differences:
difference ------------ 1 1 1 1 3 1 3
Based on the difference I have to find the ones which match the task:
WITH day10_calc AS ( SELECT value - LAG(CAST(value AS INT), 1, 0) OVER (ORDER BY value) AS difference FROM day10 ) SELECT COUNT(CASE WHEN difference = 1 THEN 1 END) * (1 + COUNT(CASE WHEN difference = 3 THEN 1 END)) FROM day10_calc;
And here is my result:
?column? ---------- 1690 (1 row)
The query for this is basically a three times self join, calculating all possibilities and return them. In the end select the max value from the list, because that is the highest number of possibilities.
WITH RECURSIVE day10_calc AS ( SELECT CAST(1 AS BIGINT) AS num1, CAST(0 AS BIGINT) AS num2, CAST(0 AS BIGINT) AS num3, CAST(0 AS BIGINT) AS value1, CAST(0 AS BIGINT) AS value2, CAST(0 AS BIGINT) AS value3 UNION ALL SELECT CASE WHEN nextadapter.value - c.value1 <= 3 THEN c.num1 ELSE 0 END + CASE WHEN nextadapter.value - c.value2 <= 3 THEN c.num2 ELSE 0 END + CASE WHEN nextadapter.value - c.value3 <= 3 THEN c.num3 ELSE 0 END AS num1, num1 AS num2, num2 AS num3, nextadapter.value AS value1, value1 AS value2, value2 AS value3 FROM day10_calc c CROSS JOIN LATERAL ( SELECT value FROM day10 WHERE value > value1 ORDER BY value LIMIT 1 ) AS nextadapter ) SELECT MAX(num1) FROM day10_calc;
The result is an insane high number:
max --------------- 5289227976704 (1 row)
But I still have no plan why I need to know in how many ways I can connect way too many adapters to a plane. But my head is swirling now.