-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.fsx
77 lines (58 loc) · 1.82 KB
/
day06.fsx
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
open System
open System.Text.RegularExpressions
// --- Part A ---
let numberExpression = Regex "([0-9]+)"
let parseNumbersFromLine input =
numberExpression.Matches input
|> Seq.map (fun i -> i.Value |> int64)
let example = [
"Time: 7 15 30"
"Distance: 9 40 200"
]
let parseTrials (input: string list) =
Seq.zip
(parseNumbersFromLine input[0])
(parseNumbersFromLine input[1])
parseTrials example
// determines if the combination of time and
let travel (holdTime: int64) (time: int64) (distance: int64) =
(time - holdTime) * holdTime
0L = travel 0L 7L 9L
6L = travel 1L 7L 9L
10L = travel 2L 7L 9L
12L = travel 3L 7L 9L
let minMaxTrials ((time, distance): int64 * int64) =
let findTravel = Seq.find (fun h -> (travel h time distance) > distance)
// find the minimum to succeed
let minHold =
seq { 1L..time }
|> findTravel
// find the maximum to succeed
let maxHold =
seq { time..(-1L)..1L }
|> findTravel
(minHold, maxHold)
(4L, 11L) = minMaxTrials (15L, 40L)
(11L, 19L) = minMaxTrials (30L, 200L)
let partA (input: string list) =
parseTrials input
|> Seq.map minMaxTrials
|> Seq.map (fun (minHold, maxHold) -> (maxHold - minHold) + 1L)
|> Seq.reduce (*)
partA example
let lines = (IO.File.ReadAllLines "day06inp.txt") |> List.ofSeq
partA lines
// --- Part B ---
let parseSpacedNumberFromLine (input: string) =
numberExpression.Matches input
|> Seq.map (fun m -> m.Value)
|> String.concat ""
|> int64
123456L = parseSpacedNumberFromLine "12 34 56"
let partB (input: string list) =
let time = parseSpacedNumberFromLine input[0]
let distance = parseSpacedNumberFromLine input[1]
let (minHold, maxHold) = minMaxTrials (time, distance)
(maxHold - minHold) + 1L
partB example
partB lines