Compare commits
2 Commits
b2881bf9a3
...
2920d0f474
| Author | SHA1 | Date | |
|---|---|---|---|
| 2920d0f474 | |||
| e7ad25a79d |
42
day14.hs
42
day14.hs
@@ -3,41 +3,40 @@
|
|||||||
|
|
||||||
import Data.Maybe (fromJust)
|
import Data.Maybe (fromJust)
|
||||||
import Data.Set (Set)
|
import Data.Set (Set)
|
||||||
import qualified Data.Set as S (fromList, insert, lookupMax, member, notMember, union)
|
import qualified Data.Set as S (fromList, insert, map, member, notMember, union)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as T (lines, splitOn, words)
|
import qualified Data.Text as T (lines, splitOn, words)
|
||||||
import qualified Data.Text.Read as T (decimal)
|
import qualified Data.Text.Read as T (decimal)
|
||||||
import Lib (readFile')
|
import Lib (readFile')
|
||||||
|
|
||||||
|
-- times can be improved greatly using Data.HashSet in unordered-containers, but i wont use it
|
||||||
-- ok this is it, this takes 4.54s, kms
|
|
||||||
-- this can probably be improved using hashtables in ST monad but will try that later
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- parse . T.lines <$> readFile' "day14.in"
|
input <- parse . T.lines <$> readFile' "day14.in"
|
||||||
let mx = fst . fromJust $ S.lookupMax input
|
let mx = maximum $ S.map snd input
|
||||||
putStr "Q1: "
|
putStr "Q1: "
|
||||||
print $ sand (0, 500) input 0 mx Q1
|
print $ sand (500, 0) input 0 mx Q1
|
||||||
putStr "Q2: "
|
putStr "Q2: "
|
||||||
print $ sand (0, 500) input 0 (mx + 2) Q2
|
print $ sand (500, 0) input 0 (mx + 2) Q2
|
||||||
|
|
||||||
|
type Coord = (Int, Int)
|
||||||
|
|
||||||
data Q = Q1 | Q2 deriving (Eq)
|
data Q = Q1 | Q2 deriving (Eq)
|
||||||
|
|
||||||
sand :: (Int, Int) -> Set (Int, Int) -> Int -> Int -> Q -> Int
|
sand :: Coord -> Set Coord -> Int -> Int -> Q -> Int
|
||||||
sand (y, x) rocks soFar mx q
|
sand (x, y) rocks soFar mx q
|
||||||
| (q == Q1 && y > mx) || S.member (y, x) rocks = soFar
|
| (q == Q1 && y > mx) || S.member (x, y) rocks = soFar
|
||||||
| q == Q2 && y + 1 == mx = sand (0, 500) (S.insert (y, x) rocks) (soFar + 1) mx q
|
| q == Q2 && y + 1 == mx = sand (500, 0) (S.insert (x, y) rocks) (soFar + 1) mx q
|
||||||
| S.notMember (y + 1, x) rocks = sand (y + 1, x) rocks soFar mx q
|
| S.notMember (x, y + 1) rocks = sand (x, y + 1) rocks soFar mx q
|
||||||
| S.notMember (y + 1, x - 1) rocks = sand (y + 1, x - 1) rocks soFar mx q
|
| S.notMember (x - 1, y + 1) rocks = sand (x - 1, y + 1) rocks soFar mx q
|
||||||
| S.notMember (y + 1, x + 1) rocks = sand (y + 1, x + 1) rocks soFar mx q
|
| S.notMember (x + 1, y + 1) rocks = sand (x + 1, y + 1) rocks soFar mx q
|
||||||
| otherwise = sand (0, 500) (S.insert (y, x) rocks) (soFar + 1) mx q
|
| otherwise = sand (500, 0) (S.insert (x, y) rocks) (soFar + 1) mx q
|
||||||
|
|
||||||
parse :: [Text] -> Set (Int, Int)
|
parse :: [Text] -> Set Coord
|
||||||
parse =
|
parse =
|
||||||
S.fromList
|
S.fromList
|
||||||
. concat
|
. concatMap
|
||||||
. map
|
|
||||||
( ranges
|
( ranges
|
||||||
. map
|
. map
|
||||||
( (\[x, y] -> (x, y))
|
( (\[x, y] -> (x, y))
|
||||||
@@ -48,14 +47,13 @@ parse =
|
|||||||
. T.words
|
. T.words
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
ranges :: [(Int, Int)] -> [(Int, Int)]
|
ranges :: [Coord] -> [Coord]
|
||||||
ranges xs =
|
ranges xs =
|
||||||
concat $
|
|
||||||
foldr
|
foldr
|
||||||
( \((a, b), (x, y)) z ->
|
( \((a, b), (x, y)) z ->
|
||||||
if a == x
|
if a == x
|
||||||
then zip [min b y .. max b y] (repeat a) : z
|
then zip (repeat a) [min b y .. max b y] ++ z
|
||||||
else zip (repeat b) [min a x .. max a x] : z
|
else zip [min a x .. max a x] (repeat b) ++ z
|
||||||
)
|
)
|
||||||
[]
|
[]
|
||||||
(zip xs (tail xs))
|
(zip xs (tail xs))
|
||||||
|
|||||||
86
day15.hs
Normal file
86
day15.hs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
|
|
||||||
|
import Data.List (nub, sort)
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T (lines, null, split)
|
||||||
|
import qualified Data.Text.Read as T (decimal, signed)
|
||||||
|
import Lib (readFile')
|
||||||
|
|
||||||
|
-- broh this is so slow ~ 20s, -O2 is less than 2s though???????
|
||||||
|
-- will optimize this later, along with last 10 days *sigh*
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
input <- map parse . T.lines <$> readFile' "day15.in"
|
||||||
|
putStr "Q1: "
|
||||||
|
print $ q1 input 2000000
|
||||||
|
putStr "Q2: "
|
||||||
|
print $ q2 input input 0 4000000
|
||||||
|
|
||||||
|
type Entity = (Int, Int, Int, Int, Int)
|
||||||
|
|
||||||
|
q1 :: [Entity] -> Int -> Int
|
||||||
|
q1 input n = mergeRanges getRanges
|
||||||
|
where
|
||||||
|
mergeRanges :: [(Int, Int)] -> Int
|
||||||
|
mergeRanges [(x0, x1)] = x1 - x0 + 1
|
||||||
|
mergeRanges ((x0, x1) : (x2, x3) : xs)
|
||||||
|
| x2 - x1 < 2 = mergeRanges $ (x0, max x1 x3) : xs
|
||||||
|
| otherwise = x1 - x0 + 1 + mergeRanges ((x2, x3) : xs)
|
||||||
|
|
||||||
|
getRanges :: [(Int, Int)]
|
||||||
|
getRanges =
|
||||||
|
sort $
|
||||||
|
foldr
|
||||||
|
( \(x, y, r, a, b) xs -> case r - abs (n - y) of
|
||||||
|
dx
|
||||||
|
| dx < 0 -> xs
|
||||||
|
| n == b -> (x - dx, a - 1) : (a + 1, x + dx) : xs
|
||||||
|
| otherwise -> (x - dx, x + dx) : xs
|
||||||
|
)
|
||||||
|
[]
|
||||||
|
input
|
||||||
|
|
||||||
|
q2 :: [Entity] -> [Entity] -> Int -> Int -> Int
|
||||||
|
q2 input@((x, y, succ -> e, a, b) : xs) all mn mx =
|
||||||
|
let (x', y') = dirs [(-1, -1), (1, -1), (1, 1), (-1, 1)]
|
||||||
|
in if (x', y') == (mn - 1, mn - 1) then q2 xs all mn mx else x' * mx + y'
|
||||||
|
where
|
||||||
|
dirs :: [(Int, Int)] -> (Int, Int)
|
||||||
|
dirs [] = (mn - 1, mn - 1)
|
||||||
|
dirs ((dx, dy) : ds) =
|
||||||
|
let b = border (dx, dy) (if dx * dy > 0 then [0 .. e - 1] else [e, e - 1 .. 1])
|
||||||
|
in if b == (mn - 1, mn - 1) then dirs ds else b
|
||||||
|
|
||||||
|
border :: (Int, Int) -> [Int] -> (Int, Int)
|
||||||
|
border _ [] = (mn - 1, mn - 1)
|
||||||
|
border (dx, dy) (b : bs) =
|
||||||
|
let (x', y') = (x + dx * b, y + dy * (e - b))
|
||||||
|
in if not (contains (x', y') all) && x' >= mn && x' <= mx && y' >= mn && y' <= mx
|
||||||
|
then (x', y')
|
||||||
|
else border (dx, dy) bs
|
||||||
|
|
||||||
|
contains :: (Int, Int) -> [Entity] -> Bool
|
||||||
|
contains (a', b') = any (\(x, y, r, a, b) -> r >= abs (b' - y) + abs (a' - x))
|
||||||
|
|
||||||
|
parse :: Text -> Entity
|
||||||
|
parse =
|
||||||
|
( \[ "Sensor",
|
||||||
|
"at",
|
||||||
|
"x",
|
||||||
|
T.signed T.decimal -> Right (x, ""),
|
||||||
|
"y",
|
||||||
|
T.signed T.decimal -> Right (y, ""),
|
||||||
|
"closest",
|
||||||
|
"beacon",
|
||||||
|
"is",
|
||||||
|
"at",
|
||||||
|
"x",
|
||||||
|
T.signed T.decimal -> Right (a, ""),
|
||||||
|
"y",
|
||||||
|
T.signed T.decimal -> Right (b, "")
|
||||||
|
] -> (x, y, abs (b - y) + abs (a - x), a, b)
|
||||||
|
)
|
||||||
|
. filter (not . T.null)
|
||||||
|
. T.split (`elem` (":=, " :: [Char]))
|
||||||
Reference in New Issue
Block a user