Removed alternate solution for day 2; minor adjustments to day 5 alternate solution (Vector still slower than IntMap...)
This commit is contained in:
parent
ad9d3674f3
commit
98782c2506
13
2.hs
13
2.hs
|
@ -3,12 +3,6 @@
|
||||||
import Data.List (find)
|
import Data.List (find)
|
||||||
import Data.Maybe (catMaybes)
|
import Data.Maybe (catMaybes)
|
||||||
|
|
||||||
checksum :: [[Int]] -> Int
|
|
||||||
checksum = sum . map (\line -> maximum line - minimum line)
|
|
||||||
|
|
||||||
divsum :: [[Int]] -> Int
|
|
||||||
divsum = sum . map divline
|
|
||||||
|
|
||||||
divline :: [Int] -> Int
|
divline :: [Int] -> Int
|
||||||
divline ns =
|
divline ns =
|
||||||
uncurry div . head . catMaybes . map (\x -> Just (x,) <*> find (greaterAndDivisible x) ns) $ ns
|
uncurry div . head . catMaybes . map (\x -> Just (x,) <*> find (greaterAndDivisible x) ns) $ ns
|
||||||
|
@ -16,7 +10,6 @@ divline ns =
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "2.txt"
|
grid <- fmap (map (map read . words) . lines) $ readFile "2.txt"
|
||||||
let grid = map (map read . words) $ lines input :: [[Int]]
|
print $ sum . map (\line -> maximum line - minimum line) $ grid
|
||||||
print $ checksum grid
|
print $ sum . map divline $ grid
|
||||||
print $ divsum grid
|
|
28
2_alt.hs
28
2_alt.hs
|
@ -1,28 +0,0 @@
|
||||||
import Data.List
|
|
||||||
|
|
||||||
checksum :: [[Int]] -> Int
|
|
||||||
checksum = sum . map (\line -> maximum line - minimum line)
|
|
||||||
|
|
||||||
divsum :: [[Int]] -> Int
|
|
||||||
divsum = sum . map divline
|
|
||||||
|
|
||||||
divline :: [Int] -> Int
|
|
||||||
divline ns =
|
|
||||||
let (n, m) = catTupleMaybes $ map (\x -> (x, find (greaterAndDivisible x) ns)) ns
|
|
||||||
in n `div` m
|
|
||||||
where
|
|
||||||
greaterAndDivisible :: Int -> Int -> Bool
|
|
||||||
greaterAndDivisible n m = n > m && n `mod` m == 0
|
|
||||||
|
|
||||||
catTupleMaybes :: [(Int, Maybe Int)] -> (Int, Int)
|
|
||||||
catTupleMaybes [] = (0, 1)
|
|
||||||
catTupleMaybes ((t1, mt2):ts) = case mt2 of
|
|
||||||
Just t2 -> (t1, t2)
|
|
||||||
Nothing -> catTupleMaybes ts
|
|
||||||
|
|
||||||
main :: IO ()
|
|
||||||
main = do
|
|
||||||
input <- readFile "2.txt"
|
|
||||||
let grid = map (map read . words) $ lines input :: [[Int]]
|
|
||||||
print $ checksum grid
|
|
||||||
print $ divsum grid
|
|
12
5_alt.hs
12
5_alt.hs
|
@ -1,18 +1,13 @@
|
||||||
import Data.Vector.Unboxed (Vector, fromList, (!), (//))
|
import Data.Vector.Unboxed (Vector, fromList, (!), (//))
|
||||||
import qualified Data.Vector.Unboxed as V (length)
|
import qualified Data.Vector.Unboxed as V (length)
|
||||||
|
|
||||||
type Length = Int
|
type State = (Int, Int, Vector Int)
|
||||||
type Index = Int
|
|
||||||
type Steps = Int
|
|
||||||
type State = (Steps, Index, Vector Int)
|
|
||||||
type Update = Int -> Int
|
type Update = Int -> Int
|
||||||
|
|
||||||
next :: Update -> State -> State
|
next :: Update -> State -> State
|
||||||
next f (steps, i, jumps) =
|
next f (steps, i, jumps) =
|
||||||
let value = jumps ! i
|
let value = jumps ! i
|
||||||
nextI = i + value
|
in (steps + 1, i + value, jumps // [(i, f value)])
|
||||||
nextJumps = jumps // [(i, f value)]
|
|
||||||
in (steps + 1, nextI, nextJumps)
|
|
||||||
|
|
||||||
getExitSteps :: Update -> State -> Int
|
getExitSteps :: Update -> State -> Int
|
||||||
getExitSteps f (steps, i, jumps) =
|
getExitSteps f (steps, i, jumps) =
|
||||||
|
@ -20,7 +15,6 @@ getExitSteps f (steps, i, jumps) =
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "5.txt"
|
jumpsList <- fmap (fromList . map read . lines) $ readFile "5.txt"
|
||||||
let jumpsList = fromList $ map read $ lines input
|
|
||||||
print $ getExitSteps (+1) (0, 0, jumpsList)
|
print $ getExitSteps (+1) (0, 0, jumpsList)
|
||||||
print $ getExitSteps (\v -> if v >= 3 then v - 1 else v + 1) (0, 0, jumpsList)
|
print $ getExitSteps (\v -> if v >= 3 then v - 1 else v + 1) (0, 0, jumpsList)
|
Loading…
Reference in New Issue