From a01f87dd23443566a0aab586363052eaeff9ffd1 Mon Sep 17 00:00:00 2001 From: Tomasz Chojnacki Date: Sat, 3 Dec 2022 12:12:27 +0100 Subject: Finish day 3 --- aoc-2022-dotnet/Day03/Day03.fsproj | 18 ++++++++++++ aoc-2022-dotnet/Day03/Program.fs | 41 +++++++++++++++++++++++++++ aoc-2022-dotnet/README.md | 56 ++++++++++++++++++------------------- aoc-2022-dotnet/aoc-2022-dotnet.sln | 6 ++++ 4 files changed, 93 insertions(+), 28 deletions(-) create mode 100644 aoc-2022-dotnet/Day03/Day03.fsproj create mode 100644 aoc-2022-dotnet/Day03/Program.fs (limited to 'aoc-2022-dotnet') diff --git a/aoc-2022-dotnet/Day03/Day03.fsproj b/aoc-2022-dotnet/Day03/Day03.fsproj new file mode 100644 index 0000000..e337a41 --- /dev/null +++ b/aoc-2022-dotnet/Day03/Day03.fsproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + + + + + Always + + + Always + + + + + diff --git a/aoc-2022-dotnet/Day03/Program.fs b/aoc-2022-dotnet/Day03/Program.fs new file mode 100644 index 0000000..1bf2235 --- /dev/null +++ b/aoc-2022-dotnet/Day03/Program.fs @@ -0,0 +1,41 @@ +open System.IO + +let priority item = + if 'a' <= item && item <= 'z' then + int item - int 'a' + 1 + elif 'A' <= item && item <= 'Z' then + int item - int 'A' + 27 + else + failwithf "Invalid item: %c" item + +let cutInHalf xs = + let half = Seq.length xs / 2 + [ Seq.take half xs; Seq.skip half xs ] + +let solution1 input = + input + |> Seq.sumBy ( + cutInHalf + >> Seq.map Set + >> Set.intersectMany + >> Seq.exactlyOne + >> priority + ) + +let solution2 input = + input + |> Seq.chunkBySize 3 + |> Seq.sumBy ( + Seq.map Set + >> Set.intersectMany + >> Seq.exactlyOne + >> priority + ) + +let test = File.ReadLines "test.txt" +assert (solution1 test = 157) +assert (solution2 test = 70) + +let input = File.ReadAllLines "input.txt" +printfn "%d" <| solution1 input +printfn "%d" <| solution2 input diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md index 7a9a03c..922498a 100644 --- a/aoc-2022-dotnet/README.md +++ b/aoc-2022-dotnet/README.md @@ -1,32 +1,32 @@ # Advent of Code 2022 in .NET ![.NET](https://img.shields.io/badge/.NET-grey?logo=.NET) -![Stars](https://img.shields.io/badge/🌟%20stars-4/50-orange) +![Stars](https://img.shields.io/badge/🌟%20stars-6/50-orange) ## Progress -| Day | Part 1 | Part 2 | -| -------------------------- | :----: | :----: | -| Day 1: Calorie Counting | 🌟 | 🌟 | -| Day 2: Rock Paper Scissors | 🌟 | 🌟 | -| Day 3: ??? | | | -| Day 4: ??? | | | -| Day 5: ??? | | | -| Day 6: ??? | | | -| Day 7: ??? | | | -| Day 8: ??? | | | -| Day 9: ??? | | | -| Day 10: ??? | | | -| Day 11: ??? | | | -| Day 12: ??? | | | -| Day 13: ??? | | | -| Day 14: ??? | | | -| Day 15: ??? | | | -| Day 16: ??? | | | -| Day 17: ??? | | | -| Day 18: ??? | | | -| Day 19: ??? | | | -| Day 20: ??? | | | -| Day 21: ??? | | | -| Day 22: ??? | | | -| Day 23: ??? | | | -| Day 24: ??? | | | -| Day 25: ??? | | | +| Day | Part 1 | Part 2 | +| ------------------------------ | :----: | :----: | +| Day 1: Calorie Counting | 🌟 | 🌟 | +| Day 2: Rock Paper Scissors | 🌟 | 🌟 | +| Day 3: Rucksack Reorganization | 🌟 | 🌟 | +| Day 4: ??? | | | +| Day 5: ??? | | | +| Day 6: ??? | | | +| Day 7: ??? | | | +| Day 8: ??? | | | +| Day 9: ??? | | | +| Day 10: ??? | | | +| Day 11: ??? | | | +| Day 12: ??? | | | +| Day 13: ??? | | | +| Day 14: ??? | | | +| Day 15: ??? | | | +| Day 16: ??? | | | +| Day 17: ??? | | | +| Day 18: ??? | | | +| Day 19: ??? | | | +| Day 20: ??? | | | +| Day 21: ??? | | | +| Day 22: ??? | | | +| Day 23: ??? | | | +| Day 24: ??? | | | +| Day 25: ??? | | | diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln index 22b3799..44b5d4e 100644 --- a/aoc-2022-dotnet/aoc-2022-dotnet.sln +++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day02", "Day02\Day02.fsproj", "{A5148468-D518-4678-B32C-D3C59B6290AB}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day03", "Day03\Day03.fsproj", "{EB78C4D1-7BDD-42AF-820B-32F4102190B1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,6 +30,10 @@ Global {A5148468-D518-4678-B32C-D3C59B6290AB}.Debug|Any CPU.Build.0 = Debug|Any CPU {A5148468-D518-4678-B32C-D3C59B6290AB}.Release|Any CPU.ActiveCfg = Release|Any CPU {A5148468-D518-4678-B32C-D3C59B6290AB}.Release|Any CPU.Build.0 = Release|Any CPU + {EB78C4D1-7BDD-42AF-820B-32F4102190B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EB78C4D1-7BDD-42AF-820B-32F4102190B1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EB78C4D1-7BDD-42AF-820B-32F4102190B1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EB78C4D1-7BDD-42AF-820B-32F4102190B1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- cgit v1.2.3