2020-09 in haskell
This commit is contained in:
parent
6125f7357d
commit
d7756f3724
4 changed files with 1098 additions and 0 deletions
20
2020/09-Encoding_Error/example
Normal file
20
2020/09-Encoding_Error/example
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
35
|
||||||
|
20
|
||||||
|
15
|
||||||
|
25
|
||||||
|
47
|
||||||
|
40
|
||||||
|
62
|
||||||
|
55
|
||||||
|
65
|
||||||
|
95
|
||||||
|
102
|
||||||
|
117
|
||||||
|
150
|
||||||
|
182
|
||||||
|
127
|
||||||
|
219
|
||||||
|
299
|
||||||
|
277
|
||||||
|
309
|
||||||
|
576
|
31
2020/09-Encoding_Error/first.hs
Normal file
31
2020/09-Encoding_Error/first.hs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
module Main (main) where
|
||||||
|
import Control.Monad (when)
|
||||||
|
import Data.List (foldl')
|
||||||
|
import System.Exit (die)
|
||||||
|
|
||||||
|
exampleExpectedOutput = 127
|
||||||
|
|
||||||
|
parseInput :: String -> IO [Int]
|
||||||
|
parseInput filename = do
|
||||||
|
input <- readFile filename
|
||||||
|
return $ map read $ lines input
|
||||||
|
|
||||||
|
validate :: [Int] -> Int -> Bool
|
||||||
|
validate list n = processInput list
|
||||||
|
where
|
||||||
|
processInput :: [Int] -> Bool
|
||||||
|
processInput (_:[]) = False
|
||||||
|
processInput (x:xs) = (foldl' (\acc i -> acc || (x + i == n && x /= i)) False xs) || processInput xs
|
||||||
|
|
||||||
|
compute :: Int -> [Int] -> Int
|
||||||
|
compute len list
|
||||||
|
| validate (take len list) (list !! len) = compute len $ tail list
|
||||||
|
| otherwise = list !! len
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
example <- parseInput "example"
|
||||||
|
let exampleOutput = compute 5 example
|
||||||
|
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
|
||||||
|
input <- parseInput "input"
|
||||||
|
print $ compute 25 input
|
1000
2020/09-Encoding_Error/input
Normal file
1000
2020/09-Encoding_Error/input
Normal file
File diff suppressed because it is too large
Load diff
47
2020/09-Encoding_Error/second.hs
Normal file
47
2020/09-Encoding_Error/second.hs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
module Main (main) where
|
||||||
|
import Control.Monad (when)
|
||||||
|
import Data.List (foldl')
|
||||||
|
import Data.Maybe (fromJust, isNothing)
|
||||||
|
import System.Exit (die)
|
||||||
|
|
||||||
|
exampleExpectedOutput = 62
|
||||||
|
|
||||||
|
parseInput :: String -> IO [Int]
|
||||||
|
parseInput filename = do
|
||||||
|
input <- readFile filename
|
||||||
|
return $ map read $ lines input
|
||||||
|
|
||||||
|
validate :: [Int] -> Int -> Bool
|
||||||
|
validate list n = processInput list
|
||||||
|
where
|
||||||
|
processInput :: [Int] -> Bool
|
||||||
|
processInput (_:[]) = False
|
||||||
|
processInput (x:xs) = (foldl' (\acc i -> acc || (x + i == n && x /= i)) False xs) || processInput xs
|
||||||
|
|
||||||
|
isContiguousSum :: Int -> [Int] -> Int -> Maybe Int
|
||||||
|
isContiguousSum target list counter
|
||||||
|
| sum' == target = Just $ (minimum lst) + (maximum lst)
|
||||||
|
| sum' < target = isContiguousSum target list (counter + 1)
|
||||||
|
| otherwise = Nothing
|
||||||
|
where
|
||||||
|
lst = take counter list
|
||||||
|
sum' = sum lst
|
||||||
|
|
||||||
|
compute :: Int -> [Int] -> Int
|
||||||
|
compute len list
|
||||||
|
| validate (take len list) (list !! len) = compute len $ tail list
|
||||||
|
| otherwise = list !! len
|
||||||
|
|
||||||
|
compute' :: Int -> [Int] -> Int
|
||||||
|
compute' target list = case isContiguousSum target list 2 of
|
||||||
|
Nothing -> compute' target (tail list)
|
||||||
|
Just a -> a
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
example <- parseInput "example"
|
||||||
|
let exampleOutput = compute 5 example
|
||||||
|
exampleOutput' = compute' exampleOutput example
|
||||||
|
when (exampleOutput' /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput' ++ " instead of " ++ show exampleExpectedOutput)
|
||||||
|
input <- parseInput "input"
|
||||||
|
print $ compute' (compute 25 input) input
|
Loading…
Add table
Reference in a new issue