Day 7
This commit is contained in:
parent
5ad43785a1
commit
171d11f532
|
@ -0,0 +1,21 @@
|
||||||
|
module Day7 where
|
||||||
|
import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents)
|
||||||
|
import Data.Text (splitOn, unpack, pack)
|
||||||
|
import Prelude hiding (splitAt)
|
||||||
|
|
||||||
|
splitAt :: String -> String -> [String]
|
||||||
|
splitAt delimiter str = map unpack $ splitOn (pack delimiter) $ pack str
|
||||||
|
|
||||||
|
fuel :: Int -> Int -> Int
|
||||||
|
fuel to from = abs $ from - to
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
input <- openFile "inputs/7.txt" ReadMode
|
||||||
|
contents <- hGetContents input
|
||||||
|
|
||||||
|
let crabby = map read $ splitAt "," contents :: [Int]
|
||||||
|
|
||||||
|
print $ minimum [ sum $ map (fuel i) crabby | i <- [minimum crabby .. maximum crabby] ]
|
||||||
|
|
||||||
|
hClose input
|
|
@ -0,0 +1,21 @@
|
||||||
|
module Day7_1 where
|
||||||
|
import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents)
|
||||||
|
import Data.Text (splitOn, unpack, pack)
|
||||||
|
import Prelude hiding (splitAt)
|
||||||
|
|
||||||
|
splitAt :: String -> String -> [String]
|
||||||
|
splitAt delimiter str = map unpack $ splitOn (pack delimiter) $ pack str
|
||||||
|
|
||||||
|
fuel :: Int -> Int -> Int
|
||||||
|
fuel to from = sum [0..abs $ from - to]
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
input <- openFile "inputs/7.txt" ReadMode
|
||||||
|
contents <- hGetContents input
|
||||||
|
|
||||||
|
let crabby = map read $ splitAt "," contents :: [Int]
|
||||||
|
|
||||||
|
print $ minimum [ sum $ map (fuel i) crabby | i <- [minimum crabby .. maximum crabby] ]
|
||||||
|
|
||||||
|
hClose input
|
Loading…
Reference in New Issue