aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-03 12:12:27 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-03 12:12:27 +0100
commita01f87dd23443566a0aab586363052eaeff9ffd1 (patch)
tree000bac963905239219850861fd87c2e14b4b2321 /aoc-2022-dotnet
parentdf1680c97bf2dbfc844ad0cd903665a3f4d8de9f (diff)
downloadgleam_aoc2020-a01f87dd23443566a0aab586363052eaeff9ffd1.tar.gz
gleam_aoc2020-a01f87dd23443566a0aab586363052eaeff9ffd1.zip
Finish day 3
Diffstat (limited to 'aoc-2022-dotnet')
-rw-r--r--aoc-2022-dotnet/Day03/Day03.fsproj18
-rw-r--r--aoc-2022-dotnet/Day03/Program.fs41
-rw-r--r--aoc-2022-dotnet/README.md56
-rw-r--r--aoc-2022-dotnet/aoc-2022-dotnet.sln6
4 files changed, 93 insertions, 28 deletions
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 @@
+ο»Ώ<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>
+
+</Project>
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