Day 11 solution

This commit is contained in:
Natty 2023-12-12 04:43:33 +01:00
parent db50deea96
commit 408fe56371
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
18 changed files with 520 additions and 2 deletions

2
day11a/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.stack-work/
*~

28
day11a/day11a.cabal Normal file
View File

@ -0,0 +1,28 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack
name: day11a
version: 0.1.0.0
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
author: Natty
maintainer: natty.sh.git@gmail.com
copyright: 2023 Natty
build-type: Simple
executable day11a
main-is: Main.hs
other-modules:
Paths_day11a
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, cond
, containers
, megaparsec
, text
default-language: Haskell2010

140
day11a/input.txt generated Normal file
View File

@ -0,0 +1,140 @@
............................................#.........#.......#.................#.......#........#..........................................
......................................#.........................................................................#.........................#.
#........................................................................................................................#..................
.....#...................................................#..................................................................................
.......................................................................#..................#..............#...........#......................
............#........#...................#..........................................................#........................#..............
...............................#......................................................................................................#.....
................................................#.....#.........#................................................................#..........
#........#.......................................................................................#........................#.................
..........................................................#...........................#.....................................................
.......................................................................#....................................#...............................
............#.................#.....#..............#..........................#.............................................................
....................#........................#............................................#............#...............#....................
...#....................................................#...................................................................................
.................................#...............................#...................#...........................#............#.............
.................................................................................................#..........................................
................................................#...........................#...............#...............................................
.#........#.............#...........................................................................................#............#..........
................#....................#......................................................................#.............................#.
................................#............#.....................#........................................................................
.....................#................................................................#......................................#..............
........................................#..........#.......#..................................#........................................#....
.....#..........................................................#..............#.......................#.............#......................
..............................#...........................................................#........................................#........
.........#.......................................................................................................#..........................
......................#............................................#........................................................................
.......................................#.....#...................................#...............#...........#.........#....................
..........................................................................#.............#...................................#...............
#......#.................#.........................................................................................#........................
...............#........................................#.....#.....................#...............#....................................#..
...................................#..........................................#..........................................#.......#..........
....................................................................#.......................#...............................................
.................................................#..........................................................................................
.........................................#................................#...........................................................#.....
#..............................#..........................#..........................#..........................#...........................
.....#..............#...........................................................#........................#....................#.............
...............#............................................................................................................................
.............................................#.....#.................................................#......................................
............................................................................................................................................
.....................................#.................#......#.................................#...........................................
..........................................#.................................#.............#.................................#...............
.................................................................................#................................#..............#..........
..........#...........#....................................................................................#................................
...............#..............#.....................................#................................#....................................#.
#.....................................#.......#.......#...................................................................#.................
............................................................................................................................................
............................................................................................#...............................................
........#...........#.............#.............................................#.........................#........#........................
.............................#.............#..................................................................................#.............
.......................................................#..........#...................................#.......#......................#......
.....#.................................................................................#........#...........................................
...........................................................................#................................................................
..............#..........................#..................................................................................................
..#.........................#.......#................#.....................................#.........................#......................
...................................................................................#......................................#.................
..................#.............................#.............#....................................#.........#........................#.....
............................................................................................................................................
.......................................................#..............................#.........................................#...........
.............................#...................................................#............#............................................#
............................................................................................................................................
...................................#...........#................#..............................................#........#..............#....
.#......#...........#.....#.............#..................#........................................#.......................................
............................................................................................#................................#..............
............................................................................................................................................
....#........#...................#....................#............#......#.......#.......................................................#.
..............................................#..............#.................................#....................#.......................
............................................................................................................................................
#......................#.....................................................#.............................................#................
........#................................#..........................................#.......................#...............................
.......................................................................................................#...........................#........
..................#...........#................#............................................................................................
............#.....................................................................................#.......................................#.
......................#.....................................................................................................................
....................................................................#..........................................#............................
..........................................#...........#...................................#.............#...................................
...#................................................................................................................#........#..............
...............................#..............................#.................#...........................#...............................
......................................#...........#.........................................................................................
.................#...........................#........................#..............................................................#......
........................#.............................................................................#.....................................
..........#.......................#..............................#....................#.........#.......................#...................
..#.........................................................................................................................................
.................................................................................#........#..................#....................#.........
............................................................................................................................................
............................................................................................................................................
...............................#......................#..............................................................................#......
.......................#..................#.........................#.......#....................#..........................................
....#.........................................................................................................#.................#...........
..............#...................#...........#..............#......................#.......................................................
............................................................................................................................................
..............................#..............................................................#.....................................#........
.....................................#.......................................#.........#..........#....................#....................
......#...........................................#.................#.......................................................................
#...........#.......#..................................#..............................................#......................#..............
................................#...................................................................................#.......................
............................................#................#..............................................................................
..........................................................................#..................................#..............................
.......#....................................................................................#..........................#.........#..........
..#...........#......................................................................#..................................................#...
...........................................................#.....#..........................................................................
..................#...............#................................................................#........................................
............................................................................................................................................
#......................#...............................#....................#..................................#..........#........#........
..........................................................................................#.................................................
...........#................#..............................................................................................................#
...........................................................#........................#.............#.........................................
................................................#...........................................................................................
...................#.....#..........#......................................................................#.....#...................#......
........#......................#............................................................#.........................#.....................
..............#..............................................................#.........#....................................................
....................................................................#..........................................................#............
#..............................................#............................................................................................
.....#........................................................................................................#.............................
......................#................#.........................#........................#.........................................#.......
............................................#......#...............................#..............#.........................................
.................................#..........................................................................................................
..........................#...................................#.........#...................................................................
..........#.................................................................................................................#...............
#...............#...........................................................#..........................................................#....
..............................................................................................#..................................#..........
......................#....................#........................................#.............................#........................#
................................................#......#....................................................................................
.......................................................................#..............................#......#..............................
.........................#.................................................................#.....#........................#.................
.#..................#..........#......................................................#.....................................................
..........#..............................................#........#.........................................................................
.......................................#.............................................................................#..............#.......
............................#.................#...................................#........................................................#
.....................................................#..........................................................................#...........
....#..........#.........................................................................................#......#...........................
......................#.....................................................................................................................
...........#.............................#.............................#................................................#...................
..................#..................................................................#......................................................
.............................#.........................................................................#.....................#.........#....
........#..................................................................#..............#.................................................
#........................................................#.........#........................................................................
.......................................#........#.............................................................#.............................
...................#..............#................................................................#.....#.........#........................
.............#.....................................................................#.....................................#............#.....
......#...................................................................#.....................................................#...........

34
day11a/package.yaml Normal file
View File

@ -0,0 +1,34 @@
name: day11a
version: 0.1.0.0
author: "Natty"
maintainer: "natty.sh.git@gmail.com"
copyright: "2023 Natty"
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
dependencies:
- base >= 4.7 && < 5
- megaparsec
- containers
- text
- cond
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
executables:
day11a:
main: Main.hs
source-dirs: src
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N

24
day11a/src/Main.hs Normal file
View File

@ -0,0 +1,24 @@
module Main (main) where
import Control.Conditional (ifM)
import Data.List (singleton, transpose)
manhattan :: Num a => (a, a) -> (a, a) -> a
manhattan (ax, ay) (bx, by) = abs (ax - bx) + abs (ay - by)
inflate :: [String] -> [String]
inflate = concatMap (ifM (all (=='.')) (replicate 2) singleton)
main :: IO ()
main = do
lns <- lines <$> getContents
print $ sum
. fmap sum
. (zipWith (fmap . manhattan) <*> takeWhile (not . null) . iterate (drop 1))
. concatMap (zip <$> repeat . fst <*> snd . fmap (fmap snd . filter ((=='#') . fst)))
. zip [0..]
. fmap (`zip` [0..])
. inflate
. transpose
. inflate
$ lns

6
day11a/stack.yaml Normal file
View File

@ -0,0 +1,6 @@
resolver: lts-21.21
packages:
- .
extra-deps: []

12
day11a/stack.yaml.lock Normal file
View File

@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages: []
snapshots:
- completed:
sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186
size: 640060
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml
original: lts-21.21

10
day11a/test.txt generated Normal file
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

2
day11b/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.stack-work/
*~

28
day11b/day11b.cabal Normal file
View File

@ -0,0 +1,28 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack
name: day11b
version: 0.1.0.0
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
author: Natty
maintainer: natty.sh.git@gmail.com
copyright: 2023 Natty
build-type: Simple
executable day11b
main-is: Main.hs
other-modules:
Paths_day11b
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, cond
, containers
, megaparsec
, text
default-language: Haskell2010

140
day11b/input.txt generated Normal file
View File

@ -0,0 +1,140 @@
............................................#.........#.......#.................#.......#........#..........................................
......................................#.........................................................................#.........................#.
#........................................................................................................................#..................
.....#...................................................#..................................................................................
.......................................................................#..................#..............#...........#......................
............#........#...................#..........................................................#........................#..............
...............................#......................................................................................................#.....
................................................#.....#.........#................................................................#..........
#........#.......................................................................................#........................#.................
..........................................................#...........................#.....................................................
.......................................................................#....................................#...............................
............#.................#.....#..............#..........................#.............................................................
....................#........................#............................................#............#...............#....................
...#....................................................#...................................................................................
.................................#...............................#...................#...........................#............#.............
.................................................................................................#..........................................
................................................#...........................#...............#...............................................
.#........#.............#...........................................................................................#............#..........
................#....................#......................................................................#.............................#.
................................#............#.....................#........................................................................
.....................#................................................................#......................................#..............
........................................#..........#.......#..................................#........................................#....
.....#..........................................................#..............#.......................#.............#......................
..............................#...........................................................#........................................#........
.........#.......................................................................................................#..........................
......................#............................................#........................................................................
.......................................#.....#...................................#...............#...........#.........#....................
..........................................................................#.............#...................................#...............
#......#.................#.........................................................................................#........................
...............#........................................#.....#.....................#...............#....................................#..
...................................#..........................................#..........................................#.......#..........
....................................................................#.......................#...............................................
.................................................#..........................................................................................
.........................................#................................#...........................................................#.....
#..............................#..........................#..........................#..........................#...........................
.....#..............#...........................................................#........................#....................#.............
...............#............................................................................................................................
.............................................#.....#.................................................#......................................
............................................................................................................................................
.....................................#.................#......#.................................#...........................................
..........................................#.................................#.............#.................................#...............
.................................................................................#................................#..............#..........
..........#...........#....................................................................................#................................
...............#..............#.....................................#................................#....................................#.
#.....................................#.......#.......#...................................................................#.................
............................................................................................................................................
............................................................................................#...............................................
........#...........#.............#.............................................#.........................#........#........................
.............................#.............#..................................................................................#.............
.......................................................#..........#...................................#.......#......................#......
.....#.................................................................................#........#...........................................
...........................................................................#................................................................
..............#..........................#..................................................................................................
..#.........................#.......#................#.....................................#.........................#......................
...................................................................................#......................................#.................
..................#.............................#.............#....................................#.........#........................#.....
............................................................................................................................................
.......................................................#..............................#.........................................#...........
.............................#...................................................#............#............................................#
............................................................................................................................................
...................................#...........#................#..............................................#........#..............#....
.#......#...........#.....#.............#..................#........................................#.......................................
............................................................................................#................................#..............
............................................................................................................................................
....#........#...................#....................#............#......#.......#.......................................................#.
..............................................#..............#.................................#....................#.......................
............................................................................................................................................
#......................#.....................................................#.............................................#................
........#................................#..........................................#.......................#...............................
.......................................................................................................#...........................#........
..................#...........#................#............................................................................................
............#.....................................................................................#.......................................#.
......................#.....................................................................................................................
....................................................................#..........................................#............................
..........................................#...........#...................................#.............#...................................
...#................................................................................................................#........#..............
...............................#..............................#.................#...........................#...............................
......................................#...........#.........................................................................................
.................#...........................#........................#..............................................................#......
........................#.............................................................................#.....................................
..........#.......................#..............................#....................#.........#.......................#...................
..#.........................................................................................................................................
.................................................................................#........#..................#....................#.........
............................................................................................................................................
............................................................................................................................................
...............................#......................#..............................................................................#......
.......................#..................#.........................#.......#....................#..........................................
....#.........................................................................................................#.................#...........
..............#...................#...........#..............#......................#.......................................................
............................................................................................................................................
..............................#..............................................................#.....................................#........
.....................................#.......................................#.........#..........#....................#....................
......#...........................................#.................#.......................................................................
#...........#.......#..................................#..............................................#......................#..............
................................#...................................................................................#.......................
............................................#................#..............................................................................
..........................................................................#..................................#..............................
.......#....................................................................................#..........................#.........#..........
..#...........#......................................................................#..................................................#...
...........................................................#.....#..........................................................................
..................#...............#................................................................#........................................
............................................................................................................................................
#......................#...............................#....................#..................................#..........#........#........
..........................................................................................#.................................................
...........#................#..............................................................................................................#
...........................................................#........................#.............#.........................................
................................................#...........................................................................................
...................#.....#..........#......................................................................#.....#...................#......
........#......................#............................................................#.........................#.....................
..............#..............................................................#.........#....................................................
....................................................................#..........................................................#............
#..............................................#............................................................................................
.....#........................................................................................................#.............................
......................#................#.........................#........................#.........................................#.......
............................................#......#...............................#..............#.........................................
.................................#..........................................................................................................
..........................#...................................#.........#...................................................................
..........#.................................................................................................................#...............
#...............#...........................................................#..........................................................#....
..............................................................................................#..................................#..........
......................#....................#........................................#.............................#........................#
................................................#......#....................................................................................
.......................................................................#..............................#......#..............................
.........................#.................................................................#.....#........................#.................
.#..................#..........#......................................................#.....................................................
..........#..............................................#........#.........................................................................
.......................................#.............................................................................#..............#.......
............................#.................#...................................#........................................................#
.....................................................#..........................................................................#...........
....#..........#.........................................................................................#......#...........................
......................#.....................................................................................................................
...........#.............................#.............................#................................................#...................
..................#..................................................................#......................................................
.............................#.........................................................................#.....................#.........#....
........#..................................................................#..............#.................................................
#........................................................#.........#........................................................................
.......................................#........#.............................................................#.............................
...................#..............#................................................................#.....#.........#........................
.............#.....................................................................#.....................................#............#.....
......#...................................................................#.....................................................#...........

34
day11b/package.yaml Normal file
View File

@ -0,0 +1,34 @@
name: day11b
version: 0.1.0.0
author: "Natty"
maintainer: "natty.sh.git@gmail.com"
copyright: "2023 Natty"
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
dependencies:
- base >= 4.7 && < 5
- megaparsec
- containers
- text
- cond
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
executables:
day11b:
main: Main.hs
source-dirs: src
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N

28
day11b/src/Main.hs Normal file
View File

@ -0,0 +1,28 @@
module Main (main) where
import Control.Conditional (ifM)
import Data.List (transpose)
manhattan :: Num a => (a, a) -> (a, a) -> a
manhattan (ax, ay) (bx, by) = abs (ax - bx) + abs (ay - by)
inflate :: [String] -> [String]
inflate = fmap (ifM (all ((||) <$> (=='.') <*> (=='V'))) (fmap (const 'V')) id)
inflationFactor :: Char -> Integer
inflationFactor 'V' = 1000000
inflationFactor _ = 1
main :: IO ()
main = do
lns <- lines <$> getContents
print $ sum
. fmap sum
. (zipWith (fmap . manhattan) <*> takeWhile (not . null) . iterate (drop 1))
. concatMap (zip <$> repeat . fst <*> snd . fmap (fmap snd . filter ((=='#') . fst)))
. (zip <$> (scanl1 (+) . fmap (inflationFactor . fst . head)) <*> id)
. fmap (zip <$> id <*> scanl1 (+) . fmap inflationFactor)
. inflate
. transpose
. inflate
$ lns

6
day11b/stack.yaml Normal file
View File

@ -0,0 +1,6 @@
resolver: lts-21.21
packages:
- .
extra-deps: []

12
day11b/stack.yaml.lock Normal file
View File

@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages: []
snapshots:
- completed:
sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186
size: 640060
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml
original: lts-21.21

10
day11b/test.txt generated Normal file
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

3
run.sh
View File

@ -29,7 +29,8 @@ case "$1" in
"day07a" | "day07b" | \
"day08a" | "day08b" | \
"day09a" | "day09b" | \
"day10a" | "day10b") stack run <input.txt
"day10a" | "day10b" | \
"day11a" | "day11b") stack run <input.txt
;;
# Default solutions -- Java
*) ./gradlew run <input.txt

View File

@ -29,7 +29,8 @@ case "$1" in
"day07a" | "day07b" | \
"day08a" | "day08b" | \
"day09a" | "day09b" | \
"day10a" | "day10b") stack run <test.txt
"day10a" | "day10b"| \
"day11a" | "day11b") stack run <test.txt
;;
# Default solutions -- Java
*) ./gradlew run <test.txt