From ee408551b5327b196cd94bb1b857bcdc9f8b10b1 Mon Sep 17 00:00:00 2001 From: Tomasz Chojnacki Date: Wed, 28 Dec 2022 22:13:42 +0100 Subject: Finish day 25 --- aoc-2022-dotnet/Day25/Day25.fsproj | 18 +++++++++++++++++ aoc-2022-dotnet/Day25/Program.fs | 39 +++++++++++++++++++++++++++++++++++++ aoc-2022-dotnet/README.md | 4 ++-- aoc-2022-dotnet/aoc-2022-dotnet.sln | 8 +++++++- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 aoc-2022-dotnet/Day25/Day25.fsproj create mode 100644 aoc-2022-dotnet/Day25/Program.fs (limited to 'aoc-2022-dotnet') 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 @@ + + + + Exe + net7.0 + + + + + Always + + + Always + + + + + 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 diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md index 37de967..97d275f 100644 --- a/aoc-2022-dotnet/README.md +++ b/aoc-2022-dotnet/README.md @@ -1,6 +1,6 @@ # Advent of Code 2022 in F# ![F#](https://img.shields.io/badge/F%23-grey?logo=.NET) -![Stars](https://img.shields.io/badge/🌟%20stars-40/50-orange) +![Stars](https://img.shields.io/badge/🌟%20stars-41/50-orange) | Day | Problem Name | Part 1 | Part 2 | Practiced Concepts | | :----: | ---------------------------------------------------------------- | :----: | :----: | ---------------------------------------------------- | @@ -28,4 +28,4 @@ | **22** | [Monkey Map](https://adventofcode.com/2022/day/22) | | | | | **23** | [Unstable Diffusion](https://adventofcode.com/2022/day/23) | | | | | **24** | [Blizzard Basin](https://adventofcode.com/2022/day/24) | | | | -| **25** | [Full of Hot Air](https://adventofcode.com/2022/day/25) | | | | +| **25** | [Full of Hot Air](https://adventofcode.com/2022/day/25) | :star: | | positional numeral systems, recursion | diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln index 0de2b21..9cfa122 100644 --- a/aoc-2022-dotnet/aoc-2022-dotnet.sln +++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln @@ -52,7 +52,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day18", "Day18\Day18.fsproj EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day20", "Day20\Day20.fsproj", "{B8263346-97C7-473B-B2EC-D7DC9D1DEF0F}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day21", "Day21\Day21.fsproj", "{7168DBF8-A03D-43A5-A429-FBAF87D55FF6}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day21", "Day21\Day21.fsproj", "{7168DBF8-A03D-43A5-A429-FBAF87D55FF6}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day25", "Day25\Day25.fsproj", "{3CBC73E3-821C-45FC-85F8-877C29810E67}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -148,6 +150,10 @@ Global {7168DBF8-A03D-43A5-A429-FBAF87D55FF6}.Debug|Any CPU.Build.0 = Debug|Any CPU {7168DBF8-A03D-43A5-A429-FBAF87D55FF6}.Release|Any CPU.ActiveCfg = Release|Any CPU {7168DBF8-A03D-43A5-A429-FBAF87D55FF6}.Release|Any CPU.Build.0 = Release|Any CPU + {3CBC73E3-821C-45FC-85F8-877C29810E67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3CBC73E3-821C-45FC-85F8-877C29810E67}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3CBC73E3-821C-45FC-85F8-877C29810E67}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3CBC73E3-821C-45FC-85F8-877C29810E67}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- cgit v1.2.3