diff --git a/inputs/2023/21.txt b/inputs/2023/21.txt new file mode 100644 index 0000000..2363d5c --- /dev/null +++ b/inputs/2023/21.txt @@ -0,0 +1,131 @@ +................................................................................................................................... +....#...........#...#...........................#....................................#....##........#..........#.....#............. +..........#...##..#.....................#.#.#..................................#.#..#..#............#........#..................... +....#................##.....................#..........#........................##.#...#..#......#...#......#....#................. +.#..#.................................#.................................................#..#............#..#.##.#.#.........###.... +..#...#.#.......#......#...........#................#............................................##.........#...#...#....##........ +................#.#..........#..........##..#..............................#...#................#.#......##.......#............#... +........#.#...#..#.##.#.....#......................#.#.............................#....#......#.#.#...#........#.................. +.#...##.........#..#..........#....#.....#...........................................#........#.......#.......#...........#........ +.........##............#.......#..#.......#...#.#.....................................#..###.......###.........#.....#...#.....#... +.#..#.......#....#..#............#.....#............................#................#.........##...................#..#.#..#...... +.#.................###..#...........#.........#................##....#...........#...........#..................................... +.....##.........#..#..........#.....##....#................#..##...................#......#.....#..#.....#.#....................... +..........#......#..............#..#...#..............................................#...........#.#........#............#.#...... +...........#.#......#.....#.#.....#.....................#...........#....#.............#.#......##...#......#.....##............... +..............#...##....##.....#......#...............#.#.#...#.....#.................#.#.............#...............#....#.#.#... +.##.#........#.........#.#.#....#....#.......#.........................#...............#..........#.....##....#...............##... +.....#.......##............#.........#....#.................#.................#.......#...............#......................#...#. +...............#..........................#........#..#.......##..#....#..#...#................#....#...#.........#.#.#..#......#.. +..........#..#..#..#.##.........#......#.......................#.....#.........#......................#...#.##......#....#....#.... +....#.........#......#................#..#.............##.#......................#...........#..#.............#....##........#..... +.............#...#.................#...#...........#...#.#......#......#.......................#.................#....#.#....##.... +.......#.#.....#..........#.....#.##............#.#......#........#...#.......##................#................#.......#......... +.#.....................#..#.....#.#...........................#..................#...........#....#......##..............#.....##.. +.....#..............#....#.....................#..##...........................#..................#...................#.........#.. +....#..#.##........#.....#.........................................#......#.......................#....#....#...#...#....#...#..... +...................#....###....#............#..#.............#.#......#........##.................#...#....#.....#.##.............. +......#...#........#....#.....#................#...............................#........................##..#..#......##........#.. +.#.....#..........#....#........#................#....#...#..#..#.............#..#..........................#.........#..........#. +.......................#.....#.................#.....................................##.#.........#..................#.....#....#.. +.#..................#....#....#...........#....#...#..#..#.............#....#....#####....#.........#.......#..#..###...........##. +....#.......#.#............#.#.................##.............#.#.#....#...##...#...#...............##.###...........##......#...#. +.....#...#...#.........#.....................#....##..#........#......#.................#.#...................#.#..............#... +........##.....##.........#.#......................................#..........#.....#.#.....#.#..............#..........#.......... +........#....#.........#..#..............##.....#.....#.....#............#............#....................#.....#.....#.......#.#. +......##...........................#............#.....#.#..............#...........#.......................................#....... +.#.......#...#....#...............................#....##...#..........#.##........##....#................##...........#........... +.#.##.........#.................#................#.#..#.#................#.................................#....................... +.#..#..............#.##.........##.....................................#.....#........#.#..##..#..##............#..........#...#... +....#.##...........###.........#.....#........##...#...#......##..#...............#.#.......#...#.#..........#..........#.......... +..........#.....#.....................#..##..#.#.##.............#..............#.............#......##...........##....#.......#... +.....#.......#..................#..........#.......#........#..........##.....#....#...........................#..............#.... +.........#.......................##...............#......#.....#...#..........#.#..#...................#...................#.##.... +.........#..............................##......##...#..............#...##......#..............#.....#.......................#..... +....#............................#...#..#.#.......#.............#...............#.....#..................#........#.........#...... +..#.....................##........#......#...#....#.#..............#...............#.........#...#....#.###..........#....#..#..#.. +...........#...#.................#.........#..##.#......#.#.................#..#.......#..............................#..........#. +.....#.#.................##.#.....#....#.........#.....#..#...#.......##..#................................##.................#.... +.......#..............#.......#................#.......#..............##.....#...#...#............................................. +.#......#.................#......#.......#.........#...........#...#.......#...#...#......#.......#.#.....#...#..................#. +..#...#...............#.......#.#...................#.....#.#........................#......#....##.#.............................. +...##....#......................................#......##.......................#..#...##...............................#...#...... +.##..........................................#...............#.......................##.....#...............#.#.................... +..#..#...................................##..#.....................................#........##.....#....###......##............#... +...#............#.......#.........#..#.....#.......#.........##........................#...#...#.#..#....#.#.##....#........##.#... +.....#...........#.#....#..............#.....#..............#.................#.......#....#................#....#................. +....#............#......#.............#....#.#...##..#....#.#...#......#.......#...........#..........#.......#.................... +...##..............#......#...#........#.........#...#....#............#..#..........#.#..#.......#...#.........#.....#............ +.............#.##.........#...#....##.#...#...........##...........##..#....#.......#............##................#............... +.............#..##...#.#.#.#..#.#............#...#......#...#.....#.........###....#..............#............#.#.....#........##. +..........#.................#.....#.........#.#......#.#...........#.......#...............#..#..........#........#.....##.......#. +....................#................#......................#..............#........##...#......#....#...........#.#....#.......... +..................#.....#.#.#....#...#..........................#...#...........................................#....#...#......... +......#..........#.#..#........#.....#.......................#.........#......#.............#..............................#....... +...........##....#..#........#...#............#...............#.#.##.#.#.#............#...#........#.....#..#.#...#......#......... +.................................................................S................................................................. +.....#........#......#...........#..#............#..............#........###........#......#............#.....#....#...#........... +..................................#..#................#........#.........#...#.#.......#..#..........#...........#......#.#........ +............#.........###........#........##........................#...#.....#......#..##..#.......#..###..............#.......... +..........#..............................................#...............#..............#............#.#...#..............#........ +...........#...#..#...##...........#............#.....#.#..#..#........#..#...............#...............#.....#.....#.##......... +.....................#....#..........#..#..................#..#.........#......#..................#.................#..#........... +............#....#.#...............#.....#..........##..............#...#.#....#....#......#....................................... +.#...........#............#..#..#....................................#.......#......#.....#....#.............#..................... +..#.#...........#.............#...............#...#.....#.......#.............#.........#.#.........#..........#................... +..............#................##.#.#.#.#......#...#...........#....#.#.....................#...........##.#..#.............#.##... +...#...............#..........................##........#..#.....................#...........#...#...#.#...#......##............... +........#..........#...#..........#...#.....#............#.......................#.#..........#...#..........................##.... +.......#..............#.............#.......##.#..#...#........#.....#..#.....#....#............................#........#...#..... +..........#.............#...........#........#..#...###....#..........#...#....#..............#.##.............#.........#...#.##.. +.........................#.#..........#.....#.........##.#..............#.....##.........#.##.....#.....#.......................... +....#....#.................#..........#...#.....##.#...#....#.........##.....#.##.............................................#.... +...#....#.#............#...........................#......................#..#..#...###............#...#........................... +..............#............#................#...#.#..........................#....................#...#.#............#.....#.....#. +.......#.......#........#..................#.#....#...#..............#......#...............#.......##...#.................#....... +.....#.#..............................#.#...#..................#...........#.##................#...................#..#.#.......... +.....#..........#..........#.#.....#.........#....................#..#..#.......#......................##................#.....#... +....#..................................#....#........#.........#..........#...#.........#..#..........#.............#.......#..#... +.#.................#........#..#.#......#..#.#.....#..#.........#......#......#.#........#.....#.#....#............#.......#.##.... +...............#................#..........#...#.#...#...#....#.#..#...........#....#..............................#............#.. +.........#...........#.........#................................#...#..#........................................#....#.#........... +...#..................#...........#........#................#.#....#............#...#........................#...#................. +.#..............##.....#.......#......#..............#..........................#.#...........#............#...........#....#...... +........#....#......................#................#........#...................#.........................#.#....#............##. +...........#.........#.#...........##......##..#.#..#.....................#.#......#...........#.............#..................... +...........#...##.........#...........................#......##.......#......#...............#.#...........#.##...#..#..#..#..#.... +....#...##..#.#..#.#....#...........#.........#.....#.........#.#.....................#..................#..........#........##.#.. +.....##....#..........#.#...........#....#..............#................#.......#....................##......#.................... +.#..#..#........#....................#..#.#....#........##.....##.#......#.....#......#....#.....................#......#..###.#... +.........#...#...............#.........#..#............#.#....#.........#.............#...#.................#..............##...... +...##........#.#.#.....##..#........................#....#...#...........#..........#.#............#.#...#.............#........... +...##..............#......##.................#...........#..........#..............##......................#...#.....#....#..#..... +........#........#........#.................###.................................#....................##.#.#...#...............#..#. +.......#.......................................#..........#..........................#.#............#.........#..#..##............. +.........#.........................#................#................#...#.......#..#..#.......#.........###....................... +........#.....#......###.#...............................#........#....#.....#..##.............................#..#...#........#... +...........#.#....#..........#.#...#..........####.#......####.#....................#.............................#................ +...##.............#..#........#................#......................#...#......#..#........#......#..................#...#.#...#. +.#........#.......#.......#.......#................#........#..#.....#...........#.........#.......#.#........#........##...#..#... +..........#.......#............#.........................#.................#....#...........##.........#...#................#....#. +.........##.#.....#...#..##..............................#..................................##......##..#.......................... +........#......###..#....#....#.##....#..............#........#...#.........................................#........#...........#. +.......#.....#.#..#....#..........#...#.#..#....................#........#..............#......#..#..#....#............#.......#... +...........#.##..#......##.#...............#.............#.##.............#.......................#...........##................... +.###.....................#.#..........................................#.#.................#........#............#.........#........ +.....#....#......#..#.....#............#..............................................#....##.......#........#..#..........#....... +.......#......................................................##...#.................#.......................##.................... +......#.#....#...#...............#.#...#..#...............##......#..#............#.....#.......#.................#..##..#.#.....#. +............#........#..#.........#....#......##.#........#....#..#...........................#...............#............##...... +.....#....#.##.......................#....................#.#.................................................#....##....#...#..... +..............###..#.....#........#.........##.#...........#.#.#...#.............#......#...............#.##...........##.......... +........................#..#.........#.....#.......#.........#.....#................#.#.#..........................#.#......##..... +.......##........#......#........#.##.#.#........#......................................#......#...#.#...#.......#..#.....#........ +.#.....####..........#......#.......................#.............#...........#.....#.......#.#.....................#.............. +...#.....#...##.............#...............#..##..................#................#..........#...#........#.......#.............. +.#.........#..#...#.#....#....#.......#...........#...............#...............#...#........#..........................#........ +...............#....#........###.......#....................................#.................#...........#.........#..#........... +.......#......#..#.....#.........#.........#............#.......................#..#........#...#..#.....#.......#.............#... +........................#.............#........#..........#.................#......##.#..#...#.............#...........#...#....#.. +....#.....#....#......#...#...#...................#..##.................#..........##.##...............#..........##............... +................................................................................................................................... diff --git a/solutions/solutions.cabal b/solutions/solutions.cabal index 88d4cd7..b7a3845 100644 --- a/solutions/solutions.cabal +++ b/solutions/solutions.cabal @@ -1084,3 +1084,8 @@ executable sln_2023_20 import: day main-is: 2023/20.hs build-depends: containers + +executable sln_2023_21 + import: day + main-is: 2023/21.hs + build-depends: array diff --git a/solutions/src/2023/21.hs b/solutions/src/2023/21.hs new file mode 100644 index 0000000..0778401 --- /dev/null +++ b/solutions/src/2023/21.hs @@ -0,0 +1,49 @@ +{-# Language QuasiQuotes, TemplateHaskell, BangPatterns, BlockArguments, LambdaCase, ImportQualifiedPost #-} +{-| +Module : Main +Description : Day 21 solution +Copyright : (c) Eric Mertens, 2023 +License : ISC +Maintainer : emertens@gmail.com + + + +The input cases are real super special for part 2, +don't bother thinking about them too hard, just +extrapolate and check. + +-} +module Main (main) where + +import Advent (getInputArray, times, ordNub, arrIx) +import Advent.Coord (Coord(..), cardinal) +import Data.Array.Unboxed (UArray, bounds, assocs, amap) + +main :: IO () +main = + do input <- getInputArray 2023 21 + let start = head [ start | (start, 'S') <- assocs input] + let input' = amap (\x -> x == 'S' || x == '.') input :: UArray Coord Bool + + print $ length $ times 64 (ordNub . concatMap (step input')) [start] + + let t0 = length $ times (65+131*0) (ordNub . concatMap (step input')) [start] + let t1 = length $ times (65+131*1) (ordNub . concatMap (step input')) [start] + let t2 = length $ times (65+131*2) (ordNub . concatMap (step input')) [start] + + let d01 = t1 - t0 + let d12 = t2 - t1 + let d01_12 = d12 - d01 + + let f x = t0 + (t1-t0) * x + x * (x-1) `quot` 2 * d01_12 + print (f (26501365 `quot` 131)) + +step :: UArray Coord Bool -> Coord -> [Coord] +step input here = + [ here' + | here' <- cardinal here + , let (_, C ymax xmax) = bounds input + , let modIt (C y x) = C (y `mod` (ymax+1)) (x `mod` (xmax+1)) + , let hereLogical = modIt here' + , True <- arrIx input hereLogical + ]