File tree Expand file tree Collapse file tree 3 files changed +51
-10
lines changed Expand file tree Collapse file tree 3 files changed +51
-10
lines changed Original file line number Diff line number Diff line change 1
- module Program = let [<EntryPoint>] main _ = 0
1
+ open Xunit
2
+ open FsUnit.Xunit
3
+
4
+ let blink stone =
5
+ if stone = 0 L then
6
+ [ 1 L ]
7
+ else
8
+ let s = stone.ToString()
9
+
10
+ if s.Length % 2 = 0 then
11
+ [ int64 s[.. ( s.Length / 2 - 1 )]; int64 s[ s.Length / 2 ..] ]
12
+ else
13
+ [ stone * 2024 L ]
14
+
15
+ let part1 ( stones : int64 seq ) =
16
+ ( List.ofSeq stones, [ 1 .. 25 ])
17
+ ||> List.fold ( fun stones _ -> stones |> List.collect blink)
18
+ |> List.length
19
+
20
+ let part2 ( stones : int64 seq ) =
21
+ let stones = stones |> Seq.map ( fun x -> ( x, 1 L)) |> Map.ofSeq
22
+
23
+ ( stones, [ 1 .. 75 ])
24
+ ||> List.fold ( fun stones _ ->
25
+ ( Map.empty, stones)
26
+ ||> Map.fold ( fun acc k v ->
27
+ ( acc, blink k)
28
+ ||> List.fold ( fun acc newK ->
29
+ let newV = v + ( acc |> Map.tryFind newK |> Option.defaultValue 0 L)
30
+ acc |> Map.add newK newV)))
31
+ |> Map.values
32
+ |> Seq.sum
33
+
34
+ let parse ( input : string ) = input.Split( " " ) |> Seq.map int64
35
+
36
+ module Example =
37
+ let input = " 125 17"
38
+
39
+ [<Fact>]
40
+ let testPart1 () =
41
+ parse input |> part1 |> should equal 55312
42
+
43
+ [<EntryPoint>]
44
+ let main _ =
45
+ let input = stdin.ReadToEnd() .TrimEnd()
46
+ let stones = parse input
47
+
48
+ stones |> part1 |> printfn " Part 1: %d "
49
+ stones |> part2 |> printfn " Part 2: %d "
50
+
51
+ 0
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 9
9
</PropertyGroup >
10
10
11
11
<ItemGroup >
12
- <Compile Include =" Tests.fs" />
13
12
<Compile Include =" Program.fs" />
14
13
</ItemGroup >
15
14
You can’t perform that action at this time.
0 commit comments