2020-10 in haskell
This commit is contained in:
parent
6bb7ccb654
commit
f5eb3e1a98
6 changed files with 227 additions and 0 deletions
36
2020/10-Adapter_Array/#second.hs#
Normal file
36
2020/10-Adapter_Array/#second.hs#
Normal file
|
@ -0,0 +1,36 @@
|
|||
module Main (main) where
|
||||
import Control.Monad (when)
|
||||
import Data.List (group, sort)
|
||||
import System.Exit (die)
|
||||
|
||||
exampleExpectedOutput = 8
|
||||
exampleExpectedOutput2 = 19208
|
||||
|
||||
parseInput :: String -> IO [Int]
|
||||
parseInput filename = do
|
||||
input <- readFile filename
|
||||
return $ map read $ lines input
|
||||
|
||||
compute :: [Int] -> Int
|
||||
compute jolts = product $ map (([1, 1, 2, 4, 7, 13, 24] !!) . length) $ filter ((==) 1 . head) $ group $ zipWith (-) s (0 : s) --snd . head $ foldl compute' [(0, 1)] $ sort jolts
|
||||
where
|
||||
s = sort jolts
|
||||
--compute' (prev, n) x =
|
||||
--compute' (x:xs) = (length $ takeWhile (\i -> i <= x + 3) xs) * compute' xs
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
example <- parseInput "example"
|
||||
let s = sort example
|
||||
print s
|
||||
print $ zipWith (-) s (0 : s)
|
||||
print $ group $ zipWith (-) s (0 : s)
|
||||
print $ filter ((==) 1 . head) $ group $ zipWith (-) s (0 : s)
|
||||
print $ map length $ filter ((==) 1 . head) $ group $ zipWith (-) s (0 : s)
|
||||
let exampleOutput = compute example
|
||||
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
|
||||
example2 <- parseInput "example2"
|
||||
let exampleOutput2 = compute example2
|
||||
when (exampleOutput2 /= exampleExpectedOutput2) (die $ "example2 failed: got " ++ show exampleOutput2 ++ " instead of " ++ show exampleExpectedOutput2)
|
||||
input <- parseInput "input"
|
||||
print $ compute input
|
11
2020/10-Adapter_Array/example
Normal file
11
2020/10-Adapter_Array/example
Normal file
|
@ -0,0 +1,11 @@
|
|||
16
|
||||
10
|
||||
15
|
||||
5
|
||||
1
|
||||
11
|
||||
7
|
||||
19
|
||||
6
|
||||
12
|
||||
4
|
31
2020/10-Adapter_Array/example2
Normal file
31
2020/10-Adapter_Array/example2
Normal file
|
@ -0,0 +1,31 @@
|
|||
28
|
||||
33
|
||||
18
|
||||
42
|
||||
31
|
||||
14
|
||||
46
|
||||
20
|
||||
48
|
||||
47
|
||||
24
|
||||
23
|
||||
49
|
||||
45
|
||||
19
|
||||
38
|
||||
39
|
||||
11
|
||||
1
|
||||
32
|
||||
25
|
||||
35
|
||||
8
|
||||
17
|
||||
7
|
||||
9
|
||||
4
|
||||
2
|
||||
34
|
||||
10
|
||||
3
|
31
2020/10-Adapter_Array/first.hs
Normal file
31
2020/10-Adapter_Array/first.hs
Normal file
|
@ -0,0 +1,31 @@
|
|||
module Main (main) where
|
||||
import Control.Monad (when)
|
||||
import Data.List (foldl', sort)
|
||||
import System.Exit (die)
|
||||
|
||||
exampleExpectedOutput = 35
|
||||
exampleExpectedOutput2 = 220
|
||||
|
||||
parseInput :: String -> IO [Int]
|
||||
parseInput filename = do
|
||||
input <- readFile filename
|
||||
return $ map read $ lines input
|
||||
|
||||
compute :: [Int] -> Int
|
||||
compute jolts = result $ foldl' compute' (0, 0, 0) $ sort jolts
|
||||
where
|
||||
compute' (prev, ones, threes) jolt
|
||||
| jolt - prev == 1 = (jolt, ones + 1, threes)
|
||||
| jolt - prev == 3 = (jolt, ones, threes + 1)
|
||||
result (_, ones, threes) = ones * (threes + 1)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
example <- parseInput "example"
|
||||
let exampleOutput = compute example
|
||||
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
|
||||
example2 <- parseInput "example2"
|
||||
let exampleOutput2 = compute example2
|
||||
when (exampleOutput2 /= exampleExpectedOutput2) (die $ "example2 failed: got " ++ show exampleOutput2 ++ " instead of " ++ show exampleExpectedOutput2)
|
||||
input <- parseInput "input"
|
||||
print $ compute input
|
89
2020/10-Adapter_Array/input
Normal file
89
2020/10-Adapter_Array/input
Normal file
|
@ -0,0 +1,89 @@
|
|||
67
|
||||
118
|
||||
90
|
||||
41
|
||||
105
|
||||
24
|
||||
137
|
||||
129
|
||||
124
|
||||
15
|
||||
59
|
||||
91
|
||||
94
|
||||
60
|
||||
108
|
||||
63
|
||||
112
|
||||
48
|
||||
62
|
||||
125
|
||||
68
|
||||
126
|
||||
131
|
||||
4
|
||||
1
|
||||
44
|
||||
77
|
||||
115
|
||||
75
|
||||
89
|
||||
7
|
||||
3
|
||||
82
|
||||
28
|
||||
97
|
||||
130
|
||||
104
|
||||
54
|
||||
40
|
||||
80
|
||||
76
|
||||
19
|
||||
136
|
||||
31
|
||||
98
|
||||
110
|
||||
133
|
||||
84
|
||||
2
|
||||
51
|
||||
18
|
||||
70
|
||||
12
|
||||
120
|
||||
47
|
||||
66
|
||||
27
|
||||
39
|
||||
109
|
||||
61
|
||||
34
|
||||
121
|
||||
38
|
||||
96
|
||||
30
|
||||
83
|
||||
69
|
||||
13
|
||||
81
|
||||
37
|
||||
119
|
||||
55
|
||||
20
|
||||
87
|
||||
95
|
||||
29
|
||||
88
|
||||
111
|
||||
45
|
||||
46
|
||||
14
|
||||
11
|
||||
8
|
||||
74
|
||||
101
|
||||
73
|
||||
56
|
||||
132
|
||||
23
|
29
2020/10-Adapter_Array/second.hs
Normal file
29
2020/10-Adapter_Array/second.hs
Normal file
|
@ -0,0 +1,29 @@
|
|||
module Main (main) where
|
||||
import Control.Monad (when)
|
||||
import Data.List (group, sort)
|
||||
import System.Exit (die)
|
||||
|
||||
exampleExpectedOutput = 8
|
||||
exampleExpectedOutput2 = 19208
|
||||
|
||||
parseInput :: String -> IO [Int]
|
||||
parseInput filename = do
|
||||
input <- readFile filename
|
||||
return $ map read $ lines input
|
||||
|
||||
compute :: [Int] -> Int
|
||||
compute jolts = product $ map (([0, 1, 2, 4, 7] !!) . length) $ filter ((==) 1 . head) $ group $ zipWith (-) s (0 : s)
|
||||
where
|
||||
s = sort jolts
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
example <- parseInput "example"
|
||||
let s = sort example
|
||||
let exampleOutput = compute example
|
||||
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
|
||||
example2 <- parseInput "example2"
|
||||
let exampleOutput2 = compute example2
|
||||
when (exampleOutput2 /= exampleExpectedOutput2) (die $ "example2 failed: got " ++ show exampleOutput2 ++ " instead of " ++ show exampleExpectedOutput2)
|
||||
input <- parseInput "input"
|
||||
print $ compute input
|
Loading…
Add table
Reference in a new issue