-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.fs
54 lines (46 loc) · 1.61 KB
/
11.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
open System
open System.IO
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let DEBUG = false
let dbg v =
if DEBUG then
printfn "%A" v
v
let lines = File.ReadAllText(@"input").Trim().Split(" ") |> List.ofSeq
let ans1 =
(lines |> List.countBy id, [ 1..25 ])
||> List.fold (fun l _ ->
(l, [])
||> List.foldBack (fun (r, c) nl ->
match (r, r.Length) with
| ("0", 1) -> ("1", c) :: nl
| (r, n) when n % 2 = 0 ->
(r.Substring(0, n / 2), c)
:: (match r.Substring(n / 2).TrimStart('0') with
| "" -> ("0", c)
| x -> (x, c))
:: nl
| _ -> (string ((int64 r) * 2024L), c) :: nl)
|> List.groupBy fst
|> List.map (fun (k, v) -> (k, v |> List.sumBy snd)))
|> List.sumBy snd
let ans2 =
(lines |> List.countBy id |> List.map (fun (k, v) -> (k, v |> int64)), [ 1..75 ])
||> List.fold (fun l _ ->
(l, [])
||> List.foldBack (fun (r, c) nl ->
match (r, r.Length) with
| ("0", 1) -> ("1", c) :: nl
| (r, n) when n % 2 = 0 ->
(r.Substring(0, n / 2), c)
:: (match r.Substring(n / 2).TrimStart('0') with
| "" -> ("0", c)
| x -> (x, c))
:: nl
| _ -> (string ((int64 r) * 2024L), c) :: nl)
|> List.groupBy fst
|> List.map (fun (k, v) -> (k, v |> List.sumBy snd)))
|> List.sumBy snd
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds