-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadv21.hs
142 lines (113 loc) · 3.99 KB
/
adv21.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env runhaskell
import Data.Bits
import Data.Char
import Data.List
import Data.Array
import Data.Maybe
import Data.Tuple
import Data.Foldable
import Debug.Trace
import Text.Printf
import Numeric
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Sequence as Seq
import qualified Data.ByteString.Lazy as BS
import System.Environment (getArgs)
-------------------------------------------------------------------
-------------------------------------------------------------------
-- main =
-- getArgs >>= getInput >>= return . solve2001_1 >>= print
-- where
-- getInput (l:_) = readFile l
-- getInput [] = getContents
-- :load adv21.hs
-- readFile "inputs/2101.in" >>= return . solve2101_1
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2020 DAY 3
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
-- 1071734 : (
solve2103_1 =
foldl1 (*) . map bin2dec . transpose
. map (map fst . sortOn snd . map (\x -> (head x, length x)) . group . sort)
. transpose . lines
-- 6124992
solve2103_2 input =
(bin2dec $ loopr 0 0 inlst) * (bin2dec $ loopr 1 0 inlst)
where
inlst = lines input
rankb lst =
if (nb '0' lst) > (nb '1' lst) then "10" else "01"
where nb x = length . filter (== x)
filtr num rnk lst =
filter ((== bit) . (!! num)) lst
where bit = (!! rnk) . rankb . map (!! num) $ lst
loopr rnk num lst
| length lst == 1 = head lst
| otherwise =
loopr rnk (num + 1) (filtr num rnk lst)
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2020 DAY 2
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
-- 1714950
solve2102_1 =
uncurry (*) . foldl nextpos (0, 0) . map words . lines
where
nextpos (hpos, dpth) (dir : nb : [])
| dir == "forward" = ((hpos + n), dpth)
| dir == "up" = (hpos, (dpth - n))
| dir == "down" = (hpos, (dpth + n))
| otherwise = undefined
where n = toInt nb
-- 1281977850
solve2102_2 =
(\(hpos, dpth, _) -> (hpos * dpth)) .
foldl nextpos (0, 0, 0) . map words . lines
where
nextpos (hpos, dpth, aim) (dir : nb : [])
| dir == "forward" = (hpos + n, dpth + aim * n, aim)
| dir == "up" = (hpos, dpth, aim - n)
| dir == "down" = (hpos, dpth, aim + n)
| otherwise = undefined
where n = toInt nb
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
--
-- 2020 DAY 1
--
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
-- 1676
solve2101_1 =
nbincx . map toInt . lines
where
nbincx xs = length . filter (uncurry (<)) $ zip xs (tail xs)
-- 1706
solve2101_2 =
nbinc . sumth . map toInt . lines
where
nbinc xs = length . filter (uncurry (<)) $ zip xs (tail xs)
sumth xs = zipWith3 (\x y z -> x + y + z) xs (drop 1 xs) (drop 2 xs)
-- -------------------------------------------------------------------
-- combinations m xs =
-- combsBySize xs !! m
-- where
-- combsBySize = foldr f ([[]] : repeat [])
-- f x next = zipWith (++) (map (map (x:)) ([]:next)) next
toInt :: String -> Int
toInt x = read x :: Int
-- https://stackoverflow.com/a/5921593
bin2dec = foldr (\c s -> s * 2 + c) 0 . reverse . map c2i
where c2i c = if c == '0' then 0 else 1
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------
-- -------------------------------------------------------------------