diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-28 22:13:42 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-28 22:13:42 +0100 |
commit | ee408551b5327b196cd94bb1b857bcdc9f8b10b1 (patch) | |
tree | 9d0509f88f09f16aeddfa2cc833c1e72ee19479b /aoc-2022-dotnet/Day25 | |
parent | ac81191905118139c5461ee552c1ee06e4f8544b (diff) | |
download | gleam_aoc2020-ee408551b5327b196cd94bb1b857bcdc9f8b10b1.tar.gz gleam_aoc2020-ee408551b5327b196cd94bb1b857bcdc9f8b10b1.zip |
Finish day 25
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 |