File tree 2 files changed +28
-16
lines changed
2 files changed +28
-16
lines changed Original file line number Diff line number Diff line change @@ -7,24 +7,36 @@ module Day6 =
7
7
8
8
let decrease i = if i > 0 then i - 1 else 6
9
9
10
- let rec simulate days fish =
11
- if days > 0 then
12
- let newFish =
13
- fish
14
- |> List.sumBy ( fun i -> if i = 0 then 1 else 0 )
15
- |> fun length -> List.replicate length 8
16
-
17
- let fish ' = fish |> List.map decrease
18
- let fish '' = List.append fish' newFish
19
- simulate ( days - 1 ) fish''
10
+ let rec simulate days ( fishAgesAndCounts : ( int * int64 ) list ) =
11
+ if days = 0 then
12
+ fishAgesAndCounts |> List.sumBy snd
20
13
else
21
- fish
14
+ let newFishCount =
15
+ fishAgesAndCounts
16
+ |> Map.ofList
17
+ |> Map.tryFind 0
18
+ |> Option.defaultValue 0
22
19
23
- let day6 () =
24
- InputFile
20
+ let fishAgesAndCounts ' =
21
+ fishAgesAndCounts
22
+ |> List.map ( fun ( a , c ) -> ( decrease a, c))
23
+ |> List.groupBy fst
24
+ |> List.map ( fun ( age , ageCountList ) -> ( age, ageCountList |> List.sumBy snd))
25
+ |> Map.ofList
26
+ |> Map.add 8 newFishCount
27
+
28
+ simulate ( days - 1 ) ( fishAgesAndCounts' |> Map.toList)
29
+
30
+ let initialFish path =
31
+ path
25
32
|> System.IO.File.ReadAllText
26
33
|> fun s -> s.Split( ',' )
27
34
|> List.ofArray
28
35
|> List.map int
29
- |> simulate 80
30
- |> List.length
36
+ |> List.countBy id
37
+ |> List.map ( fun ( a , c ) -> ( a, int64 c))
38
+
39
+ let day6 () = InputFile |> initialFish |> simulate 80
40
+
41
+ let day6Part2 () =
42
+ InputFile |> initialFish |> simulate 256
Original file line number Diff line number Diff line change 1
1
open Adventofcode2021
2
2
3
- let r = Day6.day6 ()
3
+ let r = Day6.day6Part2 ()
4
4
printfn " %A " r
You can’t perform that action at this time.
0 commit comments