-
Notifications
You must be signed in to change notification settings - Fork 0
/
3a.hs
72 lines (54 loc) · 1.46 KB
/
3a.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{-
Advent of Code
Mark Hegreberg
3a: gather diagnostic data about fuel consumption from binary data
-}
sampleData = [00100,
11110,
10110,
10111,
10101,
01111,
00111,
11100,
10000,
11001,
00010,
01010]
:: [Int]
main = do
contents <- readFile "./3.input"
let input = lines contents
input' = map (read::String->Int) input
print $ solution 12 input'
solution width x =
let gam = gamma width x
eps = epsilon gam
in toDec gam * toDec eps
sumPlace :: (Foldable t, Integral a) => a -> t a -> a
sumPlace place = foldl
(\acc x -> acc +
(x `mod` (place*10))
`div` place) 0
--sumPlaces :: (Foldable t, Integral a) => t a -> [a]
sumPlaces width x =
map ((`sumPlace` x) . (10^)) (reverse [0..(width-1)])
--gamma :: (Integral a, Foldable t, Num b) => a -> t a -> [b]
gamma width x =
let size = length x
in map (gammaNormalize
. subtract (size `div` 2))
(sumPlaces width x)
gammaNormalize :: (Ord a, Num a, Num p) => a -> p
gammaNormalize x
| x > 0 = 1
| otherwise = 0
epsilon :: (Num a, Eq a) => [a] -> [a]
epsilon [] = []
epsilon a
| x == 1 = 0:epsilon (tail a)
| x == 0 = 1:epsilon (tail a)
where
x = head a
toDec :: [Integer] -> Integer
toDec input = foldr (\x y ->x + 2*y) 0 $ reverse input