-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.fs
64 lines (56 loc) · 1.84 KB
/
14.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
open System
open System.IO
open System.Text.RegularExpressions
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let DEBUG = false
let dbg v =
if DEBUG then
printfn "%A" v
v
let lines =
File.ReadAllLines(@"input")
|> Array.map (fun x ->
x
|> Regex("-?\\d+").Matches
|> Array.ofSeq
|> Array.map (fun x -> x |> string |> int)
|> (fun x -> (x[0], x[1], x[2], x[3])))
let ans1 =
((0, 0, 0, 0),
lines
|> Array.map (fun (px, py, vx, vy) ->
match (((px + vx * 100) % 101) + 101) % 101, (((py + vy * 100) % 103) + 103) % 103 with
| (x, y) ->
((x < 50 && y < 51) |> Convert.ToInt32,
(x > 50 && y < 51) |> Convert.ToInt32,
(x < 50 && y > 51) |> Convert.ToInt32,
(x > 50 && y > 51) |> Convert.ToInt32)))
||> Array.fold (fun (a1, a2, a3, a4) (x1, x2, x3, x4) -> (a1 + x1, a2 + x2, a3 + x3, a4 + x4))
|> fun (q1, q2, q3, q4) -> q1 * q2 * q3 * q4
let ans2 =
1
|> Array.unfold (fun k ->
lines
|> Array.map (fun (px, py, vx, vy) ->
((((px + vx * k) % 101) + 101) % 101, (((py + vy * k) % 103) + 103) % 103), 1)
|> Map
|> fun m ->
[| 0..103 |]
|> Array.map (fun i ->
[| 0..101 |]
|> Array.map (fun j ->
match m.ContainsKey(j, i) with
| true -> '*'
| false -> ' ')
|> String)
|> String.concat "\n"
|> fun x -> printfn "\n\n\n\n\n\n\n\n%s" x
|> fun _ ->
match Console.ReadLine() with
| "y" -> None
| "b" -> Some(k - 1, k - 1)
| _ -> Some(k + 1, k + 1))
|> Array.last
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds