-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.hs
197 lines (167 loc) · 5.71 KB
/
day24.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
{-# LANGUAGE Strict #-}
import Control.Arrow ((>>>))
import Data.Foldable (asum)
import Data.List (sort)
data Reg = X | Y | Z | W | C deriving (Eq, Show)
type Val = Either Reg Int
stoval :: String -> Val
stoval "x" = Left X
stoval "y" = Left Y
stoval "z" = Left Z
stoval "w" = Left W
stoval s = Right $ read s
storeg :: String -> Reg
storeg s = let Left r = stoval s in r
data Inst
= Inp Reg
| IAdd Reg Val
| IMul Reg Val
| IDiv Reg Val
| IMod Reg Val
| IEql Reg Val
deriving (Eq, Show)
parseInst :: String -> Inst
parseInst s
| inst == "inp" = Inp a
| inst == "add" = IAdd a (stoval $ args !! 1)
| inst == "mul" = IMul a (stoval $ args !! 1)
| inst == "div" = IDiv a (stoval $ args !! 1)
| inst == "mod" = IMod a (stoval $ args !! 1)
| inst == "eql" = IEql a (stoval $ args !! 1)
| otherwise = error "invalid instruction"
where
(inst : args) = words s
a = storeg $ head args
data Expr
= Var Int
| Const Int
| Init Reg
| Add Expr Expr
| Mul Expr Expr
| Div Expr Expr
| Mod Expr Expr
| Eql Expr Expr
| Neq Expr Expr
deriving (Eq, Show)
type Env = Reg -> Expr
defaultEnv :: Env
defaultEnv C = Const 0
defaultEnv r = Init r
initEnv :: Env
initEnv _ = Const 0
augment :: Reg -> Expr -> Env -> Env
augment r v env r'
| r == r' = v
| otherwise = env r'
rhs :: Val -> Env -> Expr
rhs (Left r) env = env r
rhs (Right v) _ = Const v
compute :: Env -> Inst -> Env
compute env (Inp r) =
let Const i = env C
in augment C (Const $ i + 1) . augment r (Var i) $ env
compute env (IAdd r v) = augment r (Add (env r) (rhs v env)) env
compute env (IMul r v) = augment r (Mul (env r) (rhs v env)) env
compute env (IDiv r v) = augment r (Div (env r) (rhs v env)) env
compute env (IMod r v) = augment r (Mod (env r) (rhs v env)) env
compute env (IEql r v) = augment r (Eql (env r) (rhs v env)) env
-- simplification patterns
type Pattern = Expr -> Expr
foldConst :: Pattern
foldConst (Add (Const a) (Const b)) = Const (a + b)
foldConst (Add (Const 0) r) = r
foldConst (Add l (Const 0)) = l
foldConst (Mul (Const a) (Const b)) = Const (a * b)
foldConst (Mul (Const 0) _) = Const 0
foldConst (Mul _ (Const 0)) = Const 0
foldConst (Mul (Const 1) r) = r
foldConst (Mul l (Const 1)) = l
foldConst (Div (Const a) (Const b)) = Const (a `div` b)
foldConst (Div (Const 0) _) = Const 0
foldConst (Div l (Const 1)) = l
foldConst (Mod (Const a) (Const b)) = Const (a `mod` b)
foldConst (Mod (Const 0) _) = Const 0
foldConst (Mod _ (Const 1)) = Const 0
foldConst (Eql (Const a) (Const b)) = Const (if a == b then 1 else 0)
foldConst (Eql (Var a) (Var b)) | a == b = Const 1
foldConst e = e
eqlSimp :: Pattern
eqlSimp (Eql (Eql l r) (Const 0)) = Neq l r
eqlSimp e = e
neqSimp :: Pattern
neqSimp (Neq (Add (Mod (Init Z) (Const 26)) (Const c)) (Var _)) | c >= 10 = Const 1
neqSimp e = e
-- pattern applicators
applyPattern :: Pattern -> Pattern
applyPattern p (Add l r) = p (Add (applyPattern p l) (applyPattern p r))
applyPattern p (Mul l r) = p (Mul (applyPattern p l) (applyPattern p r))
applyPattern p (Div l r) = p (Div (applyPattern p l) (applyPattern p r))
applyPattern p (Mod l r) = p (Mod (applyPattern p l) (applyPattern p r))
applyPattern p (Eql l r) = p (Eql (applyPattern p l) (applyPattern p r))
applyPattern p (Neq l r) = p (Neq (applyPattern p l) (applyPattern p r))
applyPattern p e = p e
applyPatterns :: [Pattern] -> Pattern
applyPatterns = foldr1 (.)
simpl :: Pattern
simpl = applyPatterns [foldConst, eqlSimp, neqSimp, foldConst]
splitSections :: [Inst] -> [[Inst]]
splitSections [] = [[]]
splitSections (Inp w : is) = let (g : gs) = splitSections is in [] : (Inp w : g) : gs
splitSections (i : is) = let (g : gs) = splitSections is in (i : g) : gs
extractNeq :: Expr -> Maybe Expr
extractNeq e@(Neq _ _) = Just e
extractNeq (Add l r) = asum [extractNeq l, extractNeq r]
extractNeq (Mul l r) = asum [extractNeq l, extractNeq r]
extractNeq (Div l r) = asum [extractNeq l, extractNeq r]
extractNeq (Mod l r) = asum [extractNeq l, extractNeq r]
extractNeq (Eql l r) = asum [extractNeq l, extractNeq r]
extractNeq _ = Nothing
-- produce list of constraints
stackMachine :: [Expr] -> [Expr]
stackMachine = go [] []
where
-- go stack res exprs
go :: [Expr] -> [Expr] -> [Expr] -> [Expr]
go [] res [] = res
go _ res [] = error "stack not empty"
go stk res (e : es) = case extractNeq e of
Just (Neq (Add _ (Const c)) d) ->
let (s : stk') = stk
in go stk' (Eql s (Add d (Const $ - c)) : res) es
Just (Neq _ d) ->
let (s : stk') = stk
in go stk' (Eql s (Add d (Const 0)) : res) es
Nothing ->
let (Add _ e') = e
in go (e' : stk) res es
_ -> error "invalid state"
-- given a list of constraints of the form: `d_i + c_i = d_j + c_j`,
-- compute the best number `d_0 d_1 ...`, decided by builder
build :: (Expr -> [(Int, Int)]) -> [Expr] -> Integer
build builder = read . concatMap (show . snd) . sort . concatMap builder
largeBuilder :: Expr -> [(Int, Int)]
largeBuilder (Eql (Add (Var i) (Const ci)) (Add (Var j) (Const cj))) =
[(i, 9 + min 0 (cj - ci)), (j, 9 + min 0 (ci - cj))]
largeBuilder _ = error "invalid constraint"
smallBuilder :: Expr -> [(Int, Int)]
smallBuilder (Eql (Add (Var i) (Const ci)) (Add (Var j) (Const cj))) =
[(i, 1 + max 0 (cj - ci)), (j, 1 + max 0 (ci - cj))]
smallBuilder _ = error "invalid constraint"
main =
interact $
lines
>>> map parseInst
>>> tail . splitSections
>>> zip [0 ..]
>>> map
( \(ix, is) ->
is >$> foldl (\e i -> simpl . compute e i) (augment C (Const ix) defaultEnv)
>>> ($ Z)
>>> simpl
)
>>> stackMachine
>>> (\cs -> build <$> [largeBuilder, smallBuilder] <*> [cs])
>>> map show
>>> unlines
x >$> f = f x
infixr 1 >$>