-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15_part02.fs
49 lines (40 loc) · 1.26 KB
/
day15_part02.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
module day15_part02
open System.Text.RegularExpressions
open AdventOfCode_2016.Modules
type Disc = {
Number: int
Slots: int
Time: int
Position: int
}
let parseContent(lines: string array) =
lines
|> Array.map(fun disc ->
let regexpattern = @"(\d+).*?(\d+).*?=(\d+).*?(\d+)"
let m' = Regex.Match(disc, regexpattern)
{
Number = (int)(m'.Groups[1].Value);
Slots = (int)(m'.Groups[2].Value);
Time = (int)(m'.Groups[3].Value);
Position = (int)(m'.Groups[4].Value);
}
)
let slotPosition (disc: Disc) (second: int) =
let pos = (disc.Position + second + disc.Number) % disc.Slots
(pos, pos = 0)
let firstDrop (discs: Disc array) =
let mutable second = 0
let mutable passesAll = false
while not passesAll do
passesAll <-
discs
|> Array.mapi(fun i d -> slotPosition d (second))
|> Array.forall(fun (pos, p) -> p)
if not passesAll then
second <- second + 1
second
let execute =
let path = "day15/day15_input.txt"
let content = LocalHelper.GetLinesFromFile path
let discs = Array.append (parseContent content) [|{ Number = 7; Slots = 11; Time = 0; Position = 0 }|]
firstDrop discs