-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.fs
103 lines (95 loc) · 3.44 KB
/
07.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
open System
open System.IO
open System.Numerics
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.Split ": ")
|> Array.map (fun x ->
(x[0] |> BigInteger.Parse, x[1].Split " " |> Array.map BigInteger.Parse |> Array.rev |> List.ofSeq))
|> Array.mapi (fun i (a, b) -> (i, a, b, BigInteger(1)))
let ans1 =
lines
|> List.ofSeq
|> Array.unfold (fun l ->
match l.Length with
| 0 -> None
| _ ->
match l[0] with
| (i, t, n, c) ->
match t with
| t when t <= 0 -> Some(-1, l |> List.tail)
| _ ->
match n.Length with
| 1 ->
match n[0] with
| x when t = x -> Some(i, l |> List.tail)
| _ -> Some(-1, l |> List.tail)
| _ ->
match n[0] with
| x when t % x = 0 ->
Some(
-1,
(i, t / x, n |> List.tail, c)
:: (i, t - x, (n |> List.tail), c)
:: (l |> List.tail)
)
| x -> Some(-1, (i, t - x, n |> List.tail, c) :: (l |> List.tail)))
|> Array.filter ((<>) -1)
|> Array.countBy id
|> Array.sumBy (fun (i, c) ->
match lines[i] with
| (a, b, c, d) -> b)
let ans2 =
lines
|> List.ofSeq
|> Array.unfold (fun l ->
match l.Length with
| 0 -> None
| _ ->
match l[0] with
| (i, t, n, c) ->
match t with
| t when t <= 0 -> Some(-1, l |> List.tail)
| _ ->
match n.Length with
| 1 ->
match t with
| x when x = n[0] * c -> Some(i, l |> List.tail)
| _ -> Some(-1, l |> List.tail)
| _ ->
match n[0] with
| x when t % x = 0 ->
Some(
-1,
(i, t / x, n |> List.tail, c)
:: (i, t - x * c, (n |> List.tail), c)
:: (i,
t - x * c,
(n |> List.tail),
c * BigInteger.Pow(10, n[0] |> string |> String.length))
:: (l |> List.tail)
)
| x ->
Some(
-1,
(i, t - x * c, n |> List.tail, c)
:: (i,
t - x * c,
(n |> List.tail),
c * BigInteger.Pow(10, n[0] |> string |> String.length))
:: (l |> List.tail)
))
|> Array.filter ((<>) -1)
|> Array.countBy id
|> Array.sumBy (fun (i, c) ->
match lines[i] with
| (a, b, c, d) -> b)
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds