day11 part 1

This commit is contained in:
Gattix 2023-12-12 18:03:08 +01:00
parent c6e23b9dac
commit 621fcf2b23
4 changed files with 196 additions and 0 deletions

34
2023/day11/day11.hs Normal file
View File

@ -0,0 +1,34 @@
import Data.Function
import Data.Ix
import Control.Lens
import Data.List
import Data.Ord
import Linear.V2
main :: IO ()
main = interact $ show . day11 . parse . lines
day11 :: [V2 Int] -> Int
day11 = (`div` 2) . sum . f . (expand =<< findEmptyRows)
where f xs = [sum $ abs $ x-y | x<- xs, y<- xs]
parse :: [String] -> [V2 Int]
parse = map fst . filter ((=='#') . snd) . concat . zipWith (map . f) [0..] . map (zip [0..])
where f x (y,z) = (V2 y x, z)
expand :: [V2 Int] -> [V2 Int] -> [V2 Int]
expand [] input = input
expand (d:ds) input = expand (map (moveCoord d) ds) (map (moveCoord d) input)
findEmptyRows :: [V2 Int] -> [V2 Int]
findEmptyRows input = map flipV2 (findEmptyRows $ map flipV2 input) ++ findEmptyRows input
where findEmptyRows = deleteFirstsBy ((==) `on` view _x) b
b = range (V2 0 0, set _y 0 maxP)
maxP = maximumBy (comparing (view _x)) input
flipV2 (V2 x y) = V2 y x
moveCoord :: V2 Int -> V2 Int -> V2 Int
moveCoord x y = if y^.p >= x ^.p then y + signum x else y
where p = if x^._x == 0 then _y else _x

View File

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

140
2023/day11/input Normal file
View File

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

10
2023/day11/testinput Normal file
View File

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