diff options
Diffstat (limited to 'aoc-2022-dotnet/Day25')
-rw-r--r-- | aoc-2022-dotnet/Day25/Day25.fsproj | 18 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day25/Program.fs | 39 |
2 files changed, 57 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Day25/Day25.fsproj b/aoc-2022-dotnet/Day25/Day25.fsproj new file mode 100644 index 0000000..e337a41 --- /dev/null +++ b/aoc-2022-dotnet/Day25/Day25.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/Day25/Program.fs b/aoc-2022-dotnet/Day25/Program.fs new file mode 100644 index 0000000..c4e1878 --- /dev/null +++ b/aoc-2022-dotnet/Day25/Program.fs @@ -0,0 +1,39 @@ +module Day25 + +open System +open System.IO + +module Number = + module private Digit = + let private values = + [ ('=', -2L) + ('-', -1L) + ('0', 0L) + ('1', 1L) + ('2', 2L) ] + + let snafuTuLong c = List.find (fst >> (=) c) values |> snd + let longToSnafu d = List.find (snd >> (=) d) values |> fst + + let snafuToLong = + let rec helper = + function + | [] -> 0L + | digit :: rest -> Digit.snafuTuLong digit + 5L * helper rest + + Seq.rev >> List.ofSeq >> helper + + let rec longToSnafu = + function + | 0L -> "" + | digit -> + let (div, rem) = Math.DivRem(digit + 2L, 5L) + sprintf "%s%c" (longToSnafu div) (Digit.longToSnafu <| rem - 2L) + +let solution = Seq.sumBy Number.snafuToLong >> Number.longToSnafu + +let test = File.ReadLines("test.txt") +assert (solution test = "2=-1=0") + +let input = File.ReadLines("input.txt") +printfn "%s" <| solution input |