-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.elm
228 lines (180 loc) · 5.66 KB
/
day01.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
module Main exposing (..)
import Debug
import Regex
import Set
import Array
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 }
type alias Model =
{ problemDay : Int
, input : String
, solutionPart1 : String
, solutionPart2 : String
, direction : ( Int, Int )
, position : ( Int, Int )
}
type Msg
= Solve
| Solve2
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
Solve ->
let
solution =
solverPart1 model
|> toString
in
( { model | solutionPart1 = solution }, Cmd.none )
Solve2 ->
let
solution =
solverPart2 model
|> toString
in
( { model | solutionPart2 = solution }, Cmd.none )
subscriptions : Model -> Sub msg
subscriptions model =
Sub.none
init : ( Model, Cmd msg )
init =
( { problemDay = 1
, input = Array.get 0 problemInputs |> Maybe.withDefault ""
, solutionPart1 = ""
, solutionPart2 = ""
, direction = ( 0, 1 )
, position = ( 0, 0 )
}
, Cmd.none
)
turn : String -> Model -> Model
turn direction model =
let
( dirX, dirY ) =
model.direction
in
case direction of
"R" ->
{ model | direction = ( dirY, -dirX ) }
"L" ->
{ model | direction = ( -dirY, dirX ) }
_ ->
Debug.crash "Unknown direction"
advance : Int -> Model -> Model
advance distance model =
let
( dirX, dirY ) =
model.direction
( posX, posY ) =
model.position
in
{ model
| position =
( (distance * dirX) + posX
, (distance * dirY) + posY
)
}
blockDistance : ( Int, Int ) -> Int
blockDistance ( x, y ) =
x + y
getMovements : Model -> List ( String, Int )
getMovements model =
model.input
|> String.split ","
|> List.map String.trim
|> List.map (\x -> (Regex.find (Regex.AtMost 2) (Regex.regex "([RL])(\\d+)") x))
|> List.map
(\x ->
case List.head x of
Just d ->
case d.submatches of
[ dir, dist ] ->
( dir |> Maybe.withDefault "R"
, dist
|> Maybe.withDefault "0"
|> String.toInt
|> Result.withDefault 0
)
_ ->
Debug.crash "Input is not correct"
Nothing ->
Debug.crash "Input is not correct"
)
solverPart1 : Model -> Int
solverPart1 model =
(getMovements model)
|> List.foldl
(\( direction, distance ) m ->
m |> turn direction |> advance distance
)
model
|> .position
|> blockDistance
solverPart2 : Model -> Int
solverPart2 model =
let
moves =
getMovements model
in
findDuplicatePosition_ model moves (Set.fromList [])
|> blockDistance
intermediatePositions : ( Int, Int ) -> ( Int, Int ) -> List ( Int, Int )
intermediatePositions ( x1, y1 ) ( x2, y2 ) =
if x1 == x2 then
-- TODO less copy pasta
if y1 <= y2 then
List.map ((\x y -> ( x, y )) x1) (List.range y1 y2)
else
List.map ((\x y -> ( x, y )) x1) (List.range y2 y1)
|> List.reverse
else if x1 <= x2 then
List.map ((\y x -> ( x, y )) y1) (List.range x1 x2)
else
List.map ((\y x -> ( x, y )) y1) (List.range x2 x1)
|> List.reverse
type AlreadyVisited
= NotVisited (Set.Set ( Int, Int ))
| Visited ( Int, Int )
checkVisited : List ( Int, Int ) -> Set.Set ( Int, Int ) -> AlreadyVisited
checkVisited newPositions positions =
case newPositions of
[] ->
NotVisited positions
h :: t ->
case (Set.member h positions) of
True ->
Visited h
False ->
checkVisited t (Set.insert h positions)
findDuplicatePosition_ : Model -> List ( String, Int ) -> Set.Set ( Int, Int ) -> ( Int, Int )
findDuplicatePosition_ model movements positions =
case movements of
[] ->
Debug.crash "No solution, no duplicate movements"
h :: t ->
let
( direction, distance ) =
h
updatedModel =
(model |> turn direction |> advance distance)
intermediates =
(intermediatePositions model.position updatedModel.position) |> List.reverse |> List.tail
in
case intermediates of
Nothing ->
Debug.crash "No intermediate positions!!!"
Just i ->
case checkVisited (List.reverse i) positions of
Visited a ->
a
NotVisited p ->
findDuplicatePosition_ updatedModel t p
view : Model -> Html.Html Msg
view model =
div []
[ renderProblem model ( Solve, Solve2 )
]