From 725fd929be8495fa8db0f797fa6ab5ec6690ed5c Mon Sep 17 00:00:00 2001 From: shu Date: Wed, 18 Dec 2019 15:02:42 +0100 Subject: [PATCH] Day 18: Distance calculation works --- 2019/day18/Helpers.hs | 49 ++++++++++++++++++ 2019/day18/day18.hs | 117 ++++++++++++++++++++++++++++++++++++++++++ 2019/day18/input | 81 +++++++++++++++++++++++++++++ 2019/day18/testinput1 | 5 ++ 4 files changed, 252 insertions(+) create mode 100644 2019/day18/Helpers.hs create mode 100644 2019/day18/day18.hs create mode 100644 2019/day18/input create mode 100644 2019/day18/testinput1 diff --git a/2019/day18/Helpers.hs b/2019/day18/Helpers.hs new file mode 100644 index 0000000..694e9eb --- /dev/null +++ b/2019/day18/Helpers.hs @@ -0,0 +1,49 @@ +module Helpers + ( v2x + , v2y + , drawMap + , drawLine + , unitVecs + ) where + +import Data.List +import Data.List.Split +import qualified Data.Map as M +import Linear.V2 + +v2x :: V2 a -> a +v2x (V2 x _) = x + +v2y :: V2 a -> a +v2y (V2 _ y) = y + +unitVecs :: [V2 Int] +unitVecs = take 4 $ iterate perp (V2 0 1) + +drawLine :: V2 Int -> [V2 Int] +drawLine (V2 0 0) = [] +drawLine (V2 a b) + | abs a > abs b = V2 (a - signum a) b : drawLine (V2 (a - signum a) b) + | otherwise = V2 a (b - signum b) : drawLine (V2 a (b - signum b)) +--getBounds :: M.Map (V2 Int) Integer -> (Int, Int, Int, Int) +getBounds m = (getMinX, getMinY, getMaxX, getMaxY) + where + getMaxX = getFromMap maximum v2x + getMaxY = getFromMap maximum v2y + getMinX = getFromMap minimum v2x + getMinY = getFromMap minimum v2y + getFromMap f1 f2 = M.foldrWithKey (\k _ result -> f1 [f2 k, result]) 0 m + +drawMap :: M.Map (V2 Int) Char -> String +drawMap m = + intercalate "\n" $ + transpose $ + chunksOf + (abs (abs y1 - abs y2) + 1) + [ M.findWithDefault ' ' (V2 x y) m + | x <- [x1 .. x2] + , y <- [y1 .. y2] + ] + where + (x1, y1, x2, y2) = getBounds m + diff --git a/2019/day18/day18.hs b/2019/day18/day18.hs new file mode 100644 index 0000000..963fe5c --- /dev/null +++ b/2019/day18/day18.hs @@ -0,0 +1,117 @@ +{-# LANGUAGE LambdaCase #-} +module Main where + +import Data.List +import Data.List.Split +import Data.List.Utils +import qualified Data.Map.Strict as M +import Data.Maybe +import Helpers +import Linear.V2 + +type DungeonMap = M.Map (V2 Int) Char + +main :: IO () +main = do + map <- updateScreenBuffer M.empty (0, 0) <$> readFile "testinput1" + putStrLn $ drawMap map + let spos = findPos '@' map + let epos = findPos 'e' map + print $ getNeighbors map spos + print $ getNeighborsMap map (getNeighbors map spos) + print $ distanceFromTo map '@' 'd' + +updateScreenBuffer :: DungeonMap -> (Int, Int) -> String -> DungeonMap +updateScreenBuffer buf _ [] = buf +updateScreenBuffer buf (x, y) (a:as) = updateScreenBuffer bufNew coord as + where + bufNew = + if a /= '\n' + then M.insert (V2 x y) a buf + else buf + coord = + if a /= '\n' + then (x + 1, y) + else (0, y + 1) + +getNeighbors :: DungeonMap -> V2 Int -> DungeonMap +getNeighbors m coord = + M.fromList + [ (u + coord, M.findWithDefault '#' (u + coord) m) + | u <- unitVecs + , M.findWithDefault '#' (u + coord) m /= '#' + ] + +getNeighborsMap :: DungeonMap -> DungeonMap -> DungeonMap +getNeighborsMap m n = M.unions [getNeighbors m x | (x, _) <- M.toList n] + +distanceFromTo :: DungeonMap -> Char -> Char -> Int +distanceFromTo dmap schar echar = + distanceFromTo' dmap (M.singleton spos schar) spos epos 1 + where + spos = findPos schar dmap + epos = findPos echar dmap + distanceFromTo' mMap nMap start end n = + if M.member end newNMap + then n + else distanceFromTo' mMap newNMap start end (n + 1) + where + newNMap = M.union nMap (getNeighborsMap mMap nMap) + +findPos :: Char -> DungeonMap -> V2 Int +findPos c = head . M.keys . M.filter (`elem` [c]) + +-- countBlocks :: ScreenBuffer -> String +-- countBlocks = show . length . M.filter (== 2) +-- getIntersections :: ScreenBuffer -> ScreenBuffer +-- getIntersections buf = M.filterWithKey f buf +-- where +-- f k v = v == 35 && and [buf M.!? (k + u) == Just 35 | u <- unitVecs] +-- +-- getTurns :: ScreenBuffer -> ScreenBuffer +-- getTurns buf = M.filterWithKey f buf M.\\ getIntersections buf +-- where +-- f k v = +-- v == 35 && +-- or +-- [ buf M.!? (k + u) == Just 35 && buf M.!? (k + perp u) == Just 35 +-- | u <- unitVecs +-- ] +-- countIntAlign :: ScreenBuffer -> String +-- countIntAlign m = show $ sum [x * y | (V2 x y, _) <- M.toList m] +toRelPath :: [V2 Int] -> [V2 Int] +toRelPath path = zipWith (-) (tail path) path + +toCMD :: V2 Int -> [V2 Int] -> [String] +toCMD _ [] = [] +toCMD u (x:xs) + | crossZ u x < 0 = ("R" ++ (show . abs . sum) x) : toCMD (perp u) xs + | crossZ u x >= 0 = ("L" ++ (show . abs . sum) x) : toCMD (perp (u * (-1))) xs + +-- getCMD :: ScreenBuffer -> [String] +-- getCMD buf = toCMD dir (toRelPath path) +-- where +-- (pos, dir) = roboPos buf +-- path = pos : getPath buf [] [pos] +-- getPath :: ScreenBuffer -> [V2 Int] -> [V2 Int] -> [V2 Int] +-- getPath _ acc [] = acc +-- getPath m acc currs = +-- let a = nextTurn \\ acc +-- in getPath m (acc ++ a) a +-- where +-- curr = head currs +-- candidates = +-- M.filterWithKey +-- (\k _ -> v2x (k - curr) == 0 || v2y (k - curr) == 0) +-- (getTurns m) +-- isConnected = +-- M.filterWithKey +-- (\k _ -> +-- and +-- [ m M.!? x `elem` [Just 35, Just 94] +-- | x <- map (+ curr) $ drawLine (k - curr) +-- ]) +-- nextTurn = +-- map fst $ +-- M.toList $ M.filterWithKey (\k _ -> M.member k m) $ isConnected candidates +--nur keys^ im lambda diff --git a/2019/day18/input b/2019/day18/input new file mode 100644 index 0000000..3278a5d --- /dev/null +++ b/2019/day18/input @@ -0,0 +1,81 @@ +################################################################################# +#.........#...#....h....Q.......#.......#.#...........#...#...#.................# +#.###.###.#.#.###.#############.#.###T#.#.#.#####.###.#.#.#.#.#####.###########.# +#..a#.#.#...#.#...#.#...#.....#.#.#.#.#.#.......#.#...#.#.#.#.....#.#.....#.....# +#####.#.#####.#.###.#.#.#.#.###.#.#.#.###.#######.#.#####.#.#####.#.#.###.#.###.# +#...#.#.......#.#.....#...#.#...#...#...#.#...#...#.#.....#.#.#...#...#...#.#...# +#.#.#.#.#######.#.#########.#.###.#####.###.#.#.###.#.#####.#.#.#######L###.#.### +#.#...#.#.......#.#.......#...#.#.#.....#...#...#.....#.......#.....#.....#.#...# +#.#####.#.#######.#####.#######.#.#.###.#.#.###########.#######.###.#####.#.###.# +#...#.#.#...#.........#...#.....#.#...#.#.#.#...#.......#.....#...#...#...#.#...# +#.#.#.#.###.#########.###.#.#.###.###.#.#.#.#.#.#.#######.###.#######.#.###.##### +#.#.#.#.#...#...#...#...#...#.......#.#.#.#.#.#...#.#.....#.#.......#g#.#.#.....# +###.#.#.#.###.#.#.#.###.###########.#.###.###.#####.#.#####.#######.#.#.#.#####.# +#...#.....#...#z..#.#.#...........#.#...#.#...#.....#.#.........#...#.#.....#...# +#.#########.#######.#.#.#########.#.###.#.#.#####.#.#.#####.###.#.###.#####.#.### +#...#.......#.#...#.#...#...#.....#.#...#.#...#...#.#.#...#...#.......#...Z.#...# +#.#.#.#######.#.#.#.#####.#.###.#####.#.#.###.#.###.#.#.#.#####################.# +#.#.#...#.#.....#.#....j..#...#.....#.#.#...#...#.#.#...#.#.........#.........#.# +###.###.#.#.#####.###########.#####.#.#.###.#####.#.#####.#.#####.#.#.#######.#.# +#...#...#...#.....#.........#.....#...#.#.......#.#...#.#...#...#.#.#.#.......#.# +#K###.#######.###.#.#############.#####.#.#####.#.###.#.#####.#.###.#.#.#######.# +#.....#.......#.....#.#.......#..e#...#.#...#.......#.#.....#.#.....#.#.#.......# +#########.#.#########.#.#####.#.###.###.#.#.#######.#.#.###.#########.#.###.##### +#....n....#.#...#...#...#.....#.#.....#.#.#.....#.#.#.#.#...#.........#.....#...# +#.###.#####.#.#.#.#.#####.#####.#.###.#.#.#####.#.#.#.#.#.#####.#######.#####.#.# +#.#...#.....#.#...#d....#.#.....#...#...#.#...#.#...#...#.....#.....#...#.....#.# +###.###.#####.#####.###.#.#.#.#####.#####.#.#.#.#.###########.#.###.#####.#####.# +#...#.......#.#.....#.#.#.#.#.#.#...#...#...#.#.#...........#.#...#...#.......#.# +#.###########.#.#####.#.#.#.#.#.#.#####.#######.#############.###.###.#.#######.# +#.....#.......#.....#...#.#.#.#.#.....#.#.....#.......#.......#.....#...#...#...# +#.###.#.###########.#.###.###.#.#####.#.#.###.#######.#.#################.#.#.#.# +#...#...#...#.....#.#.#.#u....#...#...#.#.#..v........#.#...........#.....#r..#.# +###.#####.#.#.#.###.#.#.#######.#.#.###.#.###.#########.#######.###.#.#########.# +#...#.....#...#...#.#.......#...#.#.....#...#.#...#.....#.......#.D.#.......#...# +#.###############.#.#######.#.###.#####.#.#.###.#.#.#####.#######.###.#####.##### +#.......#.......#.#.#.........#...#.....#.#.....#.#.#...#...M.#.#.#.......#.....# +#######.#.#####.#.#.#.#############.#####.#######.#.#.#.#####.#.#.#####.#######.# +#.#.....#.....#...#m#.#.....#...#...#...#.#.....#.#...#.#.....#.#.....#.#.......# +#.#.#########.###.#.###.###.#.#.#.#####.#.###.#.#.#####.#.#####.#####.###.#####.# +#.............#...#.V.....#...#...............#.#.........#...............#.....# +#######################################.@.####################################### +#.....................#.............................#...#.......#.....#.#.......# +#.#####.#.###########.###.###.#########.#.#########.###.#.###.#.###.#.#.#.#####.# +#...#.#.#.#...#.#...#...#.#...#.......#.#.#...#.....#...#.#.#.#.....#...#.....#.# +###.#.#.###.#.#.#.#.###.###.###.###.###.#.#.#.#.#####.###.#.#.#############.###.# +#.....#.....#.#...#.........#.....#.....#.#.#...#.......#...#...#...#.......#...# +#.#########.#.#.#########.#######.#######.###.###.#####.###.###.#.#.#.#######.#.# +#.#.......#.#.#...#...#...#...#...#...#.#...#...#.#...#.....#.#...#.#...#....k#.# +#.#.#####.###.#####.#.#####.#.#.###.#.#.###.###.#.#.#.#######.#####.#.###.#####.# +#.#.#...#.#...#...O.#...#...#.#.....#.#.#...#...#...#.......#....o#...#...#.....# +#.#.#.#.#.#.###.#######.#B###.#######.#.#.###.#############.#####.#####.###.##### +#.#...#.#.#...#...#...#.#.#.#.#.....#.#.#.#...........#.....#...#.#...#.#...#...# +#####.#.#.###.###.#.#.#.#.#.#.#####.#.#.#.###########.#####.#.#.#.#.#.#.###.###.# +#...#.#.#.....#.#.#.#.#...#.#.#...#...#.#.#...#.......#...#...#.#...#.#...#...#.# +#.#.###.#####.#.#.#.#.#####.#.#.#.#.###.#.#.#.#####.###.#.#####U###.#.###.###.#.# +#.#.....#...#.#.#c..#...#.#...#.#.#...#.#...#.#...#.#...#...#.....#.#...#.#...#.# +#.#######.###.#.#######C#.#.###E#.###.#.#####.#.#.#.###.###.#####.###.#.#.#.###.# +#.#...#...#...#...#...#.#.#.#...#.#.....#.....#.#.#.....#.......#.....#.#.#...#.# +#.#.#.###.#.###.#.#.#.#.#.#.###.#.#######.###.#.#.#######.#####.#####.###.###.#.# +#...#...#.......#...#.#.#.....#.#.#.....#...#.#.#...#...#.#...#.#...#.#...#.#...# +#.#####.###############.#####.#.#.#.###J#.#.###.###.###.#.#.#.#.#.#.#.#.###.###.# +#.#...#...#.......I.#.#p..#.....#...#.#.#.#...#.#.#...#...#.#.#.#.#..b#.#.......# +#.#.#.###.#.#######.#.#.#.###########.#.#.###.#.#.###.#####.###.#.#####.#.#####.# +#...#.#...#.#.......#.#.#.....#.......#.#.#...#.#...#.......#.#t#.#...#.#.#...#.# +#.#####.###.#.#####.#.###.###.###.###.#.###.###.#.###########.#.###.#.#.###.#.#.# +#.#.....#...#y..#...#...#...#.P.#...#...#...#...#.#.....#.....#.....#.#...#.#.#.# +#.#.#####.#####.#.#####W###.###.###.#####.###.###.#.#.###.#.#.#######.#.#.#.#.### +#.#.....#i..#.#.#.#.....#...#...#.#...#.#.....#.....#...#.#.#.#.....#.#.#...#...# +#R#####.###F#.#.###.#########.###.###.#.###############.#.#.#.#N#.###.#########.# +#.#...#...#...#...#.#..s......#...#...#.#...............#.#.#.#.#.....#...#.....# +###.#.###.###.###.#.#####.#######.#.###.#.#############.#.#.###.#######.#.#.###.# +#...#.#.....#.#.#.#.#.....#.......#.#...#.....#.......#.#.#.#...#...#...#...#...# +#.###.#.###.#.#.#Y#.#.#####.#.###.#.#.#.#.###.#######.#.#.#.#.###.#.#.#######.### +#.#.#.#.#...#.#.#..w#...#.#.#...#.#...#.#.#.#.......#...#.#...#...#.#...#...#...# +#.#.#.#.#####.#.#######.#A#.###.#######.#.#.#######.#####.#####.###.###H#.#####.# +#.#.....#...#......f..S.#.#.#.#.......#.#...#...#.#.#...#...#...#.#.#...#.....#.# +#.#######.#.#############.#.#.#######.#.###.#.#.#.#.#.#.###.#.###.#.#.###.#.#.#.# +#...#..x#.#.......#.....#...#...#..l..#.#.#.#.#.#.#...#...#.#.#...#...#.#.#.#.#.# +#G#.#.#.#.#####.###.#.###.#####.#.#####.#.#.#.#.#.#######.#.#.#.#.#####.#.#.###.# +#.#...#.......#.....#...........#.......#.....#.........#..q..#.#.........#...X.# +################################################################################# diff --git a/2019/day18/testinput1 b/2019/day18/testinput1 new file mode 100644 index 0000000..86078d6 --- /dev/null +++ b/2019/day18/testinput1 @@ -0,0 +1,5 @@ +######################## +#f.D.E.e.............@.# +######################.# +#d.....................# +########################