diff options
Diffstat (limited to 'aoc-2022-dotnet/Day20')
-rw-r--r-- | aoc-2022-dotnet/Day20/Day20.fsproj | 26 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day20/Program.fs | 47 |
2 files changed, 73 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Day20/Day20.fsproj b/aoc-2022-dotnet/Day20/Day20.fsproj new file mode 100644 index 0000000..795d59c --- /dev/null +++ b/aoc-2022-dotnet/Day20/Day20.fsproj @@ -0,0 +1,26 @@ +<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> + + <ItemGroup> + <PackageReference Include="FSharpPlus" Version="1.3.2" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\Common\Common.fsproj" /> + </ItemGroup> + +</Project> diff --git a/aoc-2022-dotnet/Day20/Program.fs b/aoc-2022-dotnet/Day20/Program.fs new file mode 100644 index 0000000..9453a8d --- /dev/null +++ b/aoc-2022-dotnet/Day20/Program.fs @@ -0,0 +1,47 @@ +module Day20 + +open System.IO +open FSharpPlus.Math.Generic +open Common + +let mix listWithIds = + let cycle = List.length listWithIds - 1 + + let move xs idToMove = + let oldIndex = List.findIndex (fst >> (=) idToMove) xs + let element = xs[oldIndex] + let newIndex = int <| remE (int64 oldIndex + snd element) cycle + + xs + |> List.removeAt oldIndex + |> List.insertAt newIndex element + + seq { 0..cycle } |> Seq.fold move listWithIds + +let nthAfterZero n xs = + xs + |> List.item (remE (List.findIndex (snd >> (=) 0L) xs + n) (List.length xs)) + |> snd + +let groveCoords multiplier rounds input = + let mixed = + input + |> Seq.map (int64 >> (*) multiplier) + |> Seq.indexed + |> List.ofSeq + |> Util.composition rounds mix + + nthAfterZero 1000 mixed + + nthAfterZero 2000 mixed + + nthAfterZero 3000 mixed + +let solution1 = groveCoords 1 1 +let solution2 = groveCoords 811589153 10 + +let test = File.ReadLines("test.txt") +assert (solution1 test = 3) +assert (solution2 test = 1623178306) + +let input = File.ReadLines("input.txt") +printfn "%d" <| solution1 input +printfn "%d" <| solution2 input |