diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-11 15:14:53 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-11 15:14:53 +0100 |
commit | eb7cdaab9b9c13b3594e0abdbbff6f72f8cb4807 (patch) | |
tree | a03f4459c88aa028237a31d6ed159d236ab2d4f3 /aoc-2022-dotnet | |
parent | 520a01ed88fafb165ecb3eaa9fd3e39db0431722 (diff) | |
download | gleam_aoc2020-eb7cdaab9b9c13b3594e0abdbbff6f72f8cb4807.tar.gz gleam_aoc2020-eb7cdaab9b9c13b3594e0abdbbff6f72f8cb4807.zip |
Finish day 11
Diffstat (limited to 'aoc-2022-dotnet')
-rw-r--r-- | aoc-2022-dotnet/Common/Util.fs | 2 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day11/Day11.fsproj | 26 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day11/Program.fs | 135 | ||||
-rw-r--r-- | aoc-2022-dotnet/README.md | 4 | ||||
-rw-r--r-- | aoc-2022-dotnet/aoc-2022-dotnet.sln | 8 |
5 files changed, 172 insertions, 3 deletions
diff --git a/aoc-2022-dotnet/Common/Util.fs b/aoc-2022-dotnet/Common/Util.fs index 6393aad..34bf8ff 100644 --- a/aoc-2022-dotnet/Common/Util.fs +++ b/aoc-2022-dotnet/Common/Util.fs @@ -30,6 +30,8 @@ module Util = |> Seq.map (Seq.map string >> String.concat "") |> String.concat "\n" + let composition n f = List.replicate n f |> List.reduce (>>) + let topN n xs = let rec insertSorted x = function diff --git a/aoc-2022-dotnet/Day11/Day11.fsproj b/aoc-2022-dotnet/Day11/Day11.fsproj new file mode 100644 index 0000000..358ef88 --- /dev/null +++ b/aoc-2022-dotnet/Day11/Day11.fsproj @@ -0,0 +1,26 @@ +ο»Ώ<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net7.0</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <Content Include="test.txt"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> + <Content Include="input.txt"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> + <Compile Include="Program.fs" /> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="FParsec" Version="1.1.1" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\Common\Common.fsproj" /> + </ItemGroup> + +</Project> diff --git a/aoc-2022-dotnet/Day11/Program.fs b/aoc-2022-dotnet/Day11/Program.fs new file mode 100644 index 0000000..640a731 --- /dev/null +++ b/aoc-2022-dotnet/Day11/Program.fs @@ -0,0 +1,135 @@ +ο»Ώmodule Day11 + +open System.IO +open FParsec +open Common + +type Item = int64 +type MonkeyId = int32 +type Throw = { To: MonkeyId; Item: Item } + +type Monkey = + { Id: MonkeyId + Items: Item list + Operation: Item -> Item + Condition: Item + IfTrue: MonkeyId + IfFalse: MonkeyId + Inspections: int64 } + + static member condition m = m.Condition + static member inspections m = m.Inspections + + member m.throwItem i = + { Item = i + To = + match i % m.Condition with + | 0L -> m.IfTrue + | _ -> m.IfFalse } + + static member receiveThrows ts m = + { m with + Items = + m.Items + @ List.choose (fun ({ To = ti; Item = item }) -> if ti = m.Id then Some(item) else None) ts } + + static member throwAllAway worryReducer m = + let throws = List.map (m.Operation >> worryReducer >> m.throwItem) m.Items + + { m with + Items = [] + Inspections = m.Inspections + int64 (List.length throws) }, + throws + + static member parseList input = + let pid = + pstring "Monkey " >>. pint32 + .>> pchar ':' + .>> newline + + let pitems = + pstring " Starting items: " + >>. sepBy1 pint64 (pstring ", ") + .>> newline + + let padd = attempt (pstring "+ " >>. pint64 |>> (+)) + let pmul = attempt (pstring "* " >>. pint64 |>> (*)) + let psqr = attempt (pstring "* old" >>% fun n -> n * n) + + let poperation = + pstring " Operation: new = old " + >>. choice [ padd; pmul; psqr ] + .>> newline + + let pcondition = + pstring " Test: divisible by " >>. pint64 + .>> newline + + let piftrue = + pstring " If true: throw to monkey " >>. pint32 + .>> newline + + let piffalse = + pstring " If false: throw to monkey " + >>. pint32 + .>> newline + + let ptest = tuple3 pcondition piftrue piffalse + + let pmonkey = + tuple4 pid pitems poperation ptest + |>> fun (id, items, operation, (condition, iftrue, iffalse)) -> + { Id = id + Items = items + Operation = operation + Condition = condition + IfTrue = iftrue + IfFalse = iffalse + Inspections = 0 } + + let pmonkeys = sepBy1 pmonkey newline + Util.parse pmonkeys input + +let monkeyBusiness = + List.map Monkey.inspections + >> Util.topN 2 + >> List.reduce (*) + +let solution (worryManager, n) input = + let monkeys = Monkey.parseList input + let reduceWorry = worryManager monkeys + + let processRound monkeys = + let rec helper ms i = + let (current', throws) = List.item i ms |> Monkey.throwAllAway reduceWorry + + let ms' = + ms + |> List.updateAt i current' + |> List.map (Monkey.receiveThrows throws) + + match List.tryItem (i + 1) ms' with + | Some (_) -> helper ms' (i + 1) + | None -> ms' + + helper monkeys 0 + + monkeys + |> Util.composition n processRound + |> monkeyBusiness + +let part1 = (fun _ n -> n / 3L), 20 + +let part2 = + (fun ms n -> + let lcm = ms |> List.map Monkey.condition |> List.reduce (*) + n % lcm), + 10_000 + +let test = File.ReadAllText("test.txt") +assert (solution part1 test = 10605) +assert (solution part2 test = 2713310158L) + +let input = File.ReadAllText("input.txt") +printfn "%d" <| solution part1 input +printfn "%d" <| solution part2 input diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md index 60906d3..94f057c 100644 --- a/aoc-2022-dotnet/README.md +++ b/aoc-2022-dotnet/README.md @@ -1,6 +1,6 @@ # Advent of Code 2022 in .NET  - + ## Progress | Day | Part 1 | Part 2 | @@ -15,7 +15,7 @@ | Day 8: Treetop Tree House | π | π | | Day 9: Rope Bridge | π | π | | Day 10: Cathode-Ray Tube | π | π | -| Day 11: ??? | | | +| Day 11: Monkey in the Middle | π | π | | Day 12: ??? | | | | Day 13: ??? | | | | Day 14: ??? | | | diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln index b1299dc..6881d92 100644 --- a/aoc-2022-dotnet/aoc-2022-dotnet.sln +++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln @@ -32,7 +32,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day08", "Day08\Day08.fsproj EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day09", "Day09\Day09.fsproj", "{E0975DA5-E104-4969-A685-85D36F28A321}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day10", "Day10\Day10.fsproj", "{FD0F0852-30E0-485E-9423-B25CB6C4C035}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day10", "Day10\Day10.fsproj", "{FD0F0852-30E0-485E-9423-B25CB6C4C035}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day11", "Day11\Day11.fsproj", "{79DDA3B8-91C1-4F6D-A26C-3FA35609126E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -88,6 +90,10 @@ Global {FD0F0852-30E0-485E-9423-B25CB6C4C035}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD0F0852-30E0-485E-9423-B25CB6C4C035}.Release|Any CPU.ActiveCfg = Release|Any CPU {FD0F0852-30E0-485E-9423-B25CB6C4C035}.Release|Any CPU.Build.0 = Release|Any CPU + {79DDA3B8-91C1-4F6D-A26C-3FA35609126E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79DDA3B8-91C1-4F6D-A26C-3FA35609126E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79DDA3B8-91C1-4F6D-A26C-3FA35609126E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79DDA3B8-91C1-4F6D-A26C-3FA35609126E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE |