From 16e6c33658cd4a64ab511111c179d48dccee976b Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Tue, 18 Dec 2018 12:59:33 -0800 Subject: [PATCH] Day 18 - a really slow solution (~2 mins). --- README.md | 2 +- app/Main.hs | 2 +- input/18.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Day18.hs | 45 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 input/18.txt diff --git a/README.md b/README.md index c5fa42c..56e43d2 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Now located in this repository's wiki. | 14 | 22.3 | | 15 | < 1 | | 16 | ~ 15 | -| 17 | | +| 17 | 120 | | 18 | | | 19 | | | 20 | | diff --git a/app/Main.hs b/app/Main.hs index a766866..68ae951 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -30,4 +30,4 @@ import qualified Day24 import qualified Day25 main :: IO () -main = Day17.main +main = Day18.main diff --git a/input/18.txt b/input/18.txt new file mode 100644 index 0000000..b116f92 --- /dev/null +++ b/input/18.txt @@ -0,0 +1,50 @@ +..|.#...||..||.#|#..|...#.#..#.|#.|...|#|.#.|.||#. +.|#....##.#||.......|..|...|..#.#...#...|.#....... +..#......||..|.#||####.#....#...#..#||..|#..|#.|.| +.||..|.#...##..#..|.#....##...#...#####....#.||.## +.....|.|.#|.##....#.#..#.|..|#|.||.||....#|..|#.|| +.|||#...||....#......###.#|..||..#.|..|.#....|..#| +.#.|#.|#......###...||...||.##|..#.#..#.#....#..|| +.......#...|...|###|.|#.....##.#||.|.....|.#.|.|#. +|....||||#|#||.#...|#.#.||...|....|...|...#|.#.... +#|..#.|.#|..|.#...#|.#|..|...|..|#.#....|..|.#.|#| +#.#.|.|.....|..|...#.#.....#.##...|#.#..#.#|.##..| +#..#...#|....||......#...|.##...#.|.|||.|..##...#. +#..|.|#....||#...#.#|...#|...#.#.|#||..#..|..|.... +||...|||...||#..#...#|.....#...........#|.#...#.#. +...||..#|###||.|.#|#|#.||..|.#...#|......|#|#..#|| +#.|.|.|#.||.#.|..#|.|.#|#.###.|......||.||..|#.... +.###.|#|#.#..##...|.####.....|#..#.#...#.#...##.|. +.........#....|.#...|.#..|....||..||.#|......##.## +##|..#.#....#..#....|#.......##.||.||....||#..#... +...#.|.|.|.|..#|......|.##|.||...|#|..|.|..#...|.. +...||.....|#..#|.#...#|.#....|...#.|.....#|.||.#|. +.|#||.....||...|.|.|...|#..##.||#|......#|...|#... +#.....|...|#.#.............##..|.|||..#.....#|#... +.#|...#....|.||##|..#....||#.#...#..|....|#|#..... +..||.#..#.#|.||..#..#..##.#|#..#.#.#.........|...# +||..#|..##.....|#..|..|.||..|#.##.#..#..##...###.# +#|.......#|..###..|||.#|.#.......||..........|.|.. +##.##..|...#|##..##....||...|#|...|.|.#.####.....# +#|.|....|#...#....|...#|.#.#...#.|..#.|...##.|#.#| +..#|#||....|.|.....#...|....#.||##....|...|.|#||#. +..#...|.||........|..|.|.|#...|...#.|####.#.#...|. +...|#|..|..||.#|#...|.#|.|...#|...#....#...###.|.. +.####....##|......|.#...##.|.|###.....#|...|...|.. +.|...||...|...#..|#.#..#|..##......||..|........#. +|.|.#...#.|.#..||||..##...|.|.....#.#.#...|......| +|...#..#|#.||#...|.|...|..|.||#.||........#....... +|......#|.|....||#.||.....|..|#|.|..#|.||#.##.#..# +.#||.|##..###.......##..|......|.....|#|.##..###.# +..#.....#..|#...|......|#...#..|.#|.#....||||..... +.|#.#.#.........................|...#..|..|.#.|... +#..##||.|..#|..###......|....#...|.|..#|..#.....|. +|....||......|..#....|.....|##.#.|...|....#|||.||. +..#.###.#..|.|#.#...|..|...|....|#|........#...##. +.||..|||###..#.#..|.#.|....##..|..#|.|...##|..||#| +.|.|....|.|||......|.||#.|........#...#..#.#..#|.| +...|#..#.||...||...|..||||..||..|#|..|....|##.|..| +..|....|..|#.......|#..|...#..#.#..|.#.....#.#..#| +...|.#|..#.......|##..|.|.###|.|.|.||#..#.#|...#.| +|.||...|#..|.#..||#|.||...#|..#.|.#..........||||| +..##|...#|#...#..#.....#...|#.|..|##...#.|...#.... diff --git a/src/Day18.hs b/src/Day18.hs index 1c0fde9..685b78c 100644 --- a/src/Day18.hs +++ b/src/Day18.hs @@ -1,6 +1,47 @@ module Day18 (main) where +import Data.Maybe (catMaybes) +import Data.Matrix (Matrix, fromLists, nrows, ncols, (!), safeGet, setElem) + +type Landscape = Matrix Char +type Coordinate = (Int, Int) -- row, col + +stepCoord :: Landscape -> Coordinate -> Landscape -> Landscape +stepCoord oldscape (r, c) newscape = + let neighbours = catMaybes $ map (flip (uncurry safeGet) oldscape) [(r', c') | r' <- [r-1 .. r+1], c' <- [c-1 .. c+1], (r', c') /= (r, c)] + newAcre = case oldscape ! (r, c) of + '.' -> if (length $ filter (== '|') neighbours) >= 3 then '|' else '.' + '|' -> if (length $ filter (== '#') neighbours) >= 3 then '#' else '|' + '#' -> if '|' `elem` neighbours && '#' `elem` neighbours then '#' else '.' + in setElem newAcre (r, c) newscape + +stepScape :: Landscape -> Landscape +stepScape oldscape = foldr (stepCoord oldscape) oldscape [(r, c) | r <- [1 .. nrows oldscape], c <- [1 .. ncols oldscape]] + +part1 :: Landscape -> Int +part1 scape = + let newscape = iterate stepScape scape !! 10 + woodedCount = sum $ fmap (fromEnum . (== '|')) newscape + lumberyardCount = sum $ fmap (fromEnum . (== '#')) newscape + in woodedCount * lumberyardCount + +part2 :: Landscape -> Int +part2 scape = + let resVals = map getResVal $ iterate stepScape scape + (i, i') = (461, 489) -- cycleIndices (drop 1 resVals) (drop 2 resVals) 1 2 + cyclePos = (1000000000 - i) `mod` (i' - i) + i + in getResVal $ iterate stepScape scape !! cyclePos -- resVals !! cyclePos + where + getResVal scape = + let woodedCount = sum $ fmap (fromEnum . (== '|')) scape + lumberyardCount = sum $ fmap (fromEnum . (== '#')) scape + in woodedCount * lumberyardCount + -- theoretically this works but it was just faster to print out the first 500 values + cycleIndices (t:ts) (h:_:hs) ti hi = + if t == h then (ti, hi) else cycleIndices ts hs (ti + 1) (hi + 2) + main :: IO () main = do - input <- readFile "input/18.txt" - print input \ No newline at end of file + input <- fromLists . lines <$> readFile "input/18.txt" + print $ part1 input + print $ part2 input \ No newline at end of file