-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day06.fs
35 lines (29 loc) · 1.04 KB
/
Day06.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
module Day06
open System
open System.Text.RegularExpressions
let records unkern (lines: string list) =
let [times; dists] = lines|> List.map (fun ln ->
Regex.Split(ln, @"\s+")[1..]
|> List.ofArray
|> fun parts -> if unkern then [String.concat "" parts] else parts
|> List.map Int64.Parse)
List.zip times dists
let shortestPress time dist =
let rec search low guess high =
match guess * (time - guess) > dist with
| true when guess - low = 1L -> guess
| true -> search low ((guess + low) / 2L) guess
| false -> search guess ((guess + high + 1L) / 2L) high
let guess = dist |> float |> sqrt |> Convert.ToInt64
search 1 guess guess
let variations (time, dist) = 1L + (time - 2L * shortestPress time dist)
let part1 (getLines: string -> string list) =
getLines "input"
|> records false
|> List.map variations
|> List.reduce (*)
let part2 (getLines: string -> string list) =
getLines "input"
|> records true
|> List.map variations
|> List.reduce (*)