-
Notifications
You must be signed in to change notification settings - Fork 4
/
Euler14.elm
104 lines (77 loc) · 2.07 KB
/
Euler14.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
module Euler14 exposing (..)
import State exposing (State, andThen)
import Dict exposing (Dict)
import Html exposing (text)
import List.Extra as List
step n =
if n % 2 == 0 then
n // 2
else
3 * n + 1
increment n =
step n
|> generate
|> State.map (\rest -> n :: rest)
generate n =
generateIfMissing increment n
generates =
State.traverse generate
generateIfMissing : (comparable -> State (Dict comparable value) value) -> comparable -> State (Dict comparable value) value
generateIfMissing generator key =
let
modifyWith : (a -> s -> s) -> a -> State s a
modifyWith f value =
State.modify (f value)
|> State.map (\_ -> value)
updateIfNeeded cache =
case Dict.get key cache of
Just v ->
State.state v
Nothing ->
let
result =
generator key
in
generator key
|> andThen (modifyWith (Dict.insert key))
in
State.get
|> andThen updateIfNeeded
upper =
500000
folder upper key value accum =
if key <= upper then
let
size =
List.length value
in
case accum of
Nothing ->
Just ( key, size )
Just ( keyAccum, valueAccum ) ->
if size > valueAccum then
Just ( key, size )
else
accum
else
accum
largest upper =
generates [1..upper]
|> State.finalState (Dict.fromList [ ( 1, [ 1 ] ) ])
|> Dict.foldr (folder upper) Nothing
main =
largest upper
|> toString
|> text
alternative a b =
case a of
Just v ->
Just v
Nothing ->
b
predicate key value accum =
let
size =
List.length value
in
alternative (Just size) (Maybe.map (max size) accum)