-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.elm
188 lines (143 loc) · 4.66 KB
/
day09.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
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
module Main exposing (..)
import Array
import Regex
import Html exposing (program, text, textarea, div, button, pre)
import View exposing (renderProblem)
import Inputs exposing (problemInputs)
main : Program Never Model Msg
main =
program { init = init, view = view, update = update, subscriptions = subscriptions }
subscriptions : Model -> Sub msg
subscriptions model =
Sub.none
type Msg
= Solve
| Solve2
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
Solve ->
let
solution =
solverPart1 model
in
( { model | solutionPart1 = solution }, Cmd.none )
Solve2 ->
let
solution =
solverPart2 model
in
( { model | solutionPart2 = solution }, Cmd.none )
type alias Model =
{ problemDay : Int
, input : String
, solutionPart1 : String
, solutionPart2 : String
}
problemDay : Int
problemDay =
9
init : ( Model, Cmd msg )
init =
( { problemDay = problemDay
, input = Array.get (problemDay - 1) problemInputs |> Maybe.withDefault ""
, solutionPart1 = ""
, solutionPart2 = ""
}
, Cmd.none
)
type alias CompressSequence =
{ length : Int
, times : Int
}
matchToSequence : Regex.Match -> CompressSequence
matchToSequence regexMatch =
case regexMatch.submatches of
length :: times :: [] ->
{ length = length |> Maybe.withDefault "" |> String.toInt |> Result.withDefault 0
, times = times |> Maybe.withDefault "" |> String.toInt |> Result.withDefault 0
}
_ ->
Debug.crash "Invalid sequence"
uncompressSequence : CompressSequence -> String -> String
uncompressSequence sequence string =
let
subString =
string
|> String.slice 0 sequence.length
in
String.repeat sequence.times subString
uncompress : Bool -> String -> String
uncompress version2 string =
let
matches =
Regex.find (Regex.AtMost 1) (Regex.regex "\\((\\d+)x(\\d+)\\)") string
in
case matches of
[] ->
string
match :: [] ->
let
sequence =
matchToSequence match
preSequence =
String.slice 0 match.index string
compressed =
String.dropLeft (match.index + (String.length match.match)) string
afterSequence =
String.dropLeft (match.index + (String.length match.match) + sequence.length) string
uncompressed =
uncompressSequence sequence compressed
in
if version2 then
(uncompress version2 (preSequence ++ uncompressed ++ afterSequence))
else
preSequence ++ uncompressed ++ (uncompress version2 afterSequence)
_ ->
Debug.crash "Invalid sequence"
uncompress2 : Int -> String -> Int
uncompress2 length string =
let
matches =
Regex.find (Regex.AtMost 1) (Regex.regex "\\((\\d+)x(\\d+)\\)") string
in
case matches of
[] ->
String.length string
match :: [] ->
let
sequence =
matchToSequence match
preSequence =
String.slice 0 match.index string
matchEnd =
(match.index + (String.length match.match))
compressed =
String.slice matchEnd (matchEnd + sequence.length) string
afterSequence =
String.dropLeft (matchEnd + sequence.length) string
in
length
+ (String.length preSequence)
+ (sequence.times * uncompress2 0 compressed)
+ (uncompress2 0 afterSequence)
_ ->
Debug.crash "Invalid sequence"
solverPart1 : Model -> String
solverPart1 model =
model.input
|> Regex.replace (Regex.All) (Regex.regex "\\s") (\_ -> "")
|> uncompress False
|> String.length
|> toString
solverPart2 : Model -> String
solverPart2 model =
model.input
|> Regex.replace (Regex.All) (Regex.regex "\\s") (\_ -> "")
|> uncompress2 0
|> toString
view : Model -> Html.Html Msg
view model =
div []
[ renderProblem model ( Solve, Solve2 )
]