DEV Community

loading...

Discussion on: Advent of Code 2020 Solution Megathread - Day 3: Toboggan Trajectory

Collapse
clothierdroid profile image
David Clothier

SQL works for me ;)

0 lines of code. No loops, no boolean flag, only SQL.

Get this table naming 'day3':

table

3.1 Solution

SELECT COUNT(*) 
FROM   (SELECT id, 
               REPEAT(line, 32) toboggan 
        FROM   day3 
        HAVING MID(toboggan, ( ( id * 3 ) - 2 ), 1) = '#') found_trees
Enter fullscreen mode Exit fullscreen mode

3.2 Solution

SELECT ROUND(EXP(SUM(LN(x.a))),0) solution
FROM
(
SELECT COUNT(*) as a
FROM   (SELECT id, 
               REPEAT(line, 32) toboggan 
        FROM   day3 
        HAVING MID(toboggan, id, 1) = '#') found_trees 

UNION ALL
SELECT COUNT(*)
FROM   (SELECT id, 
               REPEAT(line, 32) toboggan 
        FROM   day3 
        HAVING MID(toboggan, ( ( id * 3 ) - 2 ), 1) = '#') found_trees
UNION ALL
SELECT COUNT(*)
FROM   (SELECT id, 
               REPEAT(line, 55) toboggan 
        FROM   day3 
        HAVING MID(toboggan, ( ( id * 5 ) - 4 ), 1) = '#') found_trees
UNION ALL
SELECT COUNT(*)
FROM   (SELECT id, 
               REPEAT(line, 75) toboggan 
        FROM   day3 
        HAVING MID(toboggan, ( ( id * 7 ) - 6), 1) = '#') found_trees
UNION ALL
SELECT COUNT(*)
FROM   (SELECT id, 
               REPEAT(line, 16) toboggan 
        FROM   day3 WHERE (id % 2) = 1
        HAVING MID(toboggan, ((id + 1) / 2), 1) = '#') found_trees
) AS x
Enter fullscreen mode Exit fullscreen mode