-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16_part01.fs
72 lines (62 loc) · 1.97 KB
/
day16_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module day16_part01
open AdventOfCode_2017.Modules
type MoveKind =
| Spin of steps: int
| Exchange of placeA: int * placeB: int
| Partner of placeA: string * placeB: string
let parseContent(input: string) =
let instructions = input.Split(",")
instructions
|> Array.map(fun ins ->
match ins[0] with
| 's' ->
Spin((int)(ins.Substring(1)))
| 'x' ->
let a = (int)(ins.Substring(1).Split("/")[0])
let b = (int)(ins.Substring(1).Split("/")[1])
Exchange(a, b)
| 'p' ->
let a = ins.Substring(1).Split("/")[0]
let b = ins.Substring(1).Split("/")[1]
Partner(a, b)
| _ -> failwith "error"
)
let performMove(op: MoveKind) (state: string array) =
match op with
| Spin(steps) ->
let initpart = Array.sub state (state.Length - steps) steps
let endpart = Array.sub state 0 (state.Length - steps)
Array.concat [|initpart; endpart|]
| Exchange(placeA, placeB) ->
state
|> Array.mapi(fun i v ->
if i = placeA then
state[placeB]
elif i = placeB then
state[placeA]
else
v
)
| Partner(placeA, placeB) ->
state
|> Array.map(fun v ->
if v = placeA then
placeB
elif v = placeB then
placeA
else
v
)
let rec performDance(dance: MoveKind list) (state: string array) =
match dance with
| [] -> String.concat "" state
| move :: remaining ->
let newstate = performMove move state
performDance remaining newstate
let execute() =
let path = "day16/day16_input.txt"
let content = LocalHelper.GetContentFromFile path
let dance = parseContent content |> List.ofArray
let order = "abcdefghijklmnop"
let initial = order.ToCharArray() |> Array.map string
performDance dance initial