Skip to content

Commit 185cec6

Browse files
committed
Day 6 Part 2
1 parent f34f151 commit 185cec6

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

Diff for: Day6.fs

+27-15
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,36 @@ module Day6 =
77

88
let decrease i = if i > 0 then i - 1 else 6
99

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
2013
else
21-
fish
14+
let newFishCount =
15+
fishAgesAndCounts
16+
|> Map.ofList
17+
|> Map.tryFind 0
18+
|> Option.defaultValue 0
2219

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
2532
|> System.IO.File.ReadAllText
2633
|> fun s -> s.Split(',')
2734
|> List.ofArray
2835
|> 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

Diff for: Program.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
open Adventofcode2021
22

3-
let r = Day6.day6 ()
3+
let r = Day6.day6Part2 ()
44
printfn "%A" r

0 commit comments

Comments
 (0)