-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.elm
153 lines (126 loc) · 3.79 KB
/
Utils.elm
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
module Utils exposing (..)
import Maybe exposing (withDefault)
import List exposing (concatMap, range, length, head, isEmpty, concat, filter, map, map2, member)
import List.Extra exposing (andThen, splitAt, permutations, subsequences)
import Types exposing (..)
valid : Op -> Int -> Int -> Bool
valid op x y =
case (op, x, y) of
(Add, x, y) -> x <= y
(Sub, x, y) -> x > y
(Mul, x, y) -> not (x == 1) && not (y == 1) && x <= y
(Div, x, y) -> not (y == 1) && rem x y == 0
valid_ : Op -> (Int, Int) -> Bool
valid_ op xy =
case xy of
(x, y) -> valid op x y
apply : Op -> Int -> Int -> Int
apply op x y =
case (op, x, y) of
(Add, x, y) -> x + y
(Sub, x, y) -> x - y
(Mul, x, y) -> x * y
(Div, x, y) -> x//y
apply_ : Op -> (Int, Int) -> Int
apply_ op xy =
case xy of
(x, y) -> apply op x y
values : Expr -> List Int
values expr =
case expr of
Val n -> [n]
App _ l r -> values l ++ values r
eval : Expr -> List Int
eval expr =
case expr of
Val n -> filter (\n -> n > 0) [n]
App o l r ->
let
lr_ = map2 (,) (eval l) (eval r)
lr = filter (uncurry (valid o)) lr_
in
map (uncurry (apply o)) lr
subsets : List a -> List (List a)
subsets xs =
case xs of
[] -> [[]]
xs -> concat <| map permutations (subsequences xs)
solution : Expr -> List Int -> Int -> Bool
solution e ns n = member (values e) (subsets ns) && eval e == [n]
splitN : List Int -> List (List Int, List Int)
splitN ns =
case ns of
[] -> []
[x] -> []
[x, y] -> [([x], [y])]
xs ->
let
cs = range 1 (length ns - 1)
fs = map (\n -> splitAt n) cs
in
map (\f -> f xs) fs
nesplitN : List Int -> List (List Int, List Int)
nesplitN ns = filter ne (splitN ns)
split : List a -> List (List a, List a)
split xs =
case xs of
[] -> [([], [])]
(x :: xs) -> ([], x :: xs) :: map (\(l,r) -> (x :: l, r)) (split xs)
nesplit : List a -> List (List a, List a)
nesplit xs = filter ne (split xs)
ne : (List a, List b) -> Bool
ne (xs, ys) = not (isEmpty xs || isEmpty ys)
exprs : List Int -> List Expr
exprs xs =
case xs of
[] -> []
[n] -> [Val n]
ns ->
nesplitN ns |> andThen (\(l,r) -> exprs l
|> andThen (\l -> exprs r
|> andThen (\r -> combine l r)))
combine : Expr -> Expr -> List Expr
combine l r = map (app l r) ops
results : List Int -> List Res
results ns =
case ns of
[] -> []
--[n] -> filter (\(e,n) -> n > 0) [(Val n, n)]
[n] -> [(Val n, n)] |> andThen (\(e,n) -> if n > 0 then [(e,n)] else [])
ns ->
nesplitN ns
|> andThen (\(ls,rs) -> results ls
|> andThen (\lx -> results rs
|> andThen (\rx -> combineR lx rx)))
combineR : Res -> Res -> List Res
combineR ls rs =
case (ls, rs) of
((l,x),(r,y)) -> map (\(o,x,y) -> (App o l r, apply o x y)) <| filter (\(o,x,y) -> valid o x y) <| map (\o -> (o,x,y)) ops
app : Expr -> Expr -> Op -> Expr
app l r o = App o l r
ops : List Op
ops = [Add, Sub, Mul, Div]
--solutions : List Int -> Int -> List Expr
--solutions ns n =
--filter (\e -> eval e == [n]) (concatMap exprs (subsets ns))
solutionsR : List Int -> Int -> List Expr
solutionsR ns n =
subsets ns
|> andThen (\nss -> results nss
|> andThen (\(e,m) -> if m == n then [e] else []))
inputs = [1, 3, 7, 10, 25, 50]
targetValue = 765
--find = solutions inputs
findR = solutionsR inputs
input_ = [1, 50, 25, 10]
target_ = 765
print : Expr -> String
print expr =
case expr of
Val n -> toString n
App o l r ->
case o of
Add -> "(" ++ print l ++ " + " ++ print r ++ ")"
Sub -> "(" ++ print l ++ " - " ++ print r ++ ")"
Mul -> "(" ++ print l ++ " * " ++ print r ++ ")"
Div -> "(" ++ print l ++ " / " ++ print r ++ ")"