-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11_part01.fs
50 lines (43 loc) · 1.37 KB
/
day11_part01.fs
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
module day11_part01
open AdventOfCode_2017.Modules
type direction = {
X: int;
Y: int;
Z: int;
}
let parseContent (content: string) =
let parts =
content.Split(",")
|> Array.countBy(fun e -> e)
parts
let getCoordinate (mov: string) : direction =
match mov with
| "nw" -> { X = -1; Y = 1; Z = 0 }
| "n" -> { X = 0; Y = 1; Z = -1 }
| "ne" -> { X = 1; Y = 0; Z = -1 }
| "sw" -> { X = -1; Y = 0; Z = 1 }
| "s" -> { X = 0; Y = -1; Z = 1 }
| "se" -> { X = 1; Y = -1; Z = 0 }
| _ -> failwith "error"
let calculatePosition (instructions: (string * int) array) =
let position =
instructions
|> Array.map(fun (k, v) ->
let coord = getCoordinate k
{ X = coord.X * v; Y = coord.Y * v; Z = coord.Z * v }
)
|> Array.reduce(fun acc elem -> {
X = acc.X + elem.X;
Y = acc.Y + elem.Y;
Z = acc.Z + elem.Z
})
position
let cubeDistance coord1 coord2 =
max (abs (coord1.X - coord2.X)) (max (abs (coord1.Y - coord2.Y)) (abs (coord1.Z - coord2.Z)))
let execute() =
let path = "day11/day11_input.txt"
//let path = "day11/test_input_01.txt"
let content = LocalHelper.GetContentFromFile path
let parts = parseContent content
let position = calculatePosition parts
cubeDistance { X = 0; Y = 0; Z = 0 } position