diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-10 13:26:15 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-10 13:26:15 +0100 |
commit | 520a01ed88fafb165ecb3eaa9fd3e39db0431722 (patch) | |
tree | e4ec65b5b1dd5c51b5d22810b7c79ff2f5c6381c /aoc-2022-dotnet/Day10 | |
parent | 3c444c7e3300ea7df5dcc8811e216ff552136fa9 (diff) | |
download | gleam_aoc2020-520a01ed88fafb165ecb3eaa9fd3e39db0431722.tar.gz gleam_aoc2020-520a01ed88fafb165ecb3eaa9fd3e39db0431722.zip |
Finish day 10
Diffstat (limited to 'aoc-2022-dotnet/Day10')
-rw-r--r-- | aoc-2022-dotnet/Day10/Day10.fsproj | 26 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day10/Program.fs | 63 |
2 files changed, 89 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Day10/Day10.fsproj b/aoc-2022-dotnet/Day10/Day10.fsproj new file mode 100644 index 0000000..358ef88 --- /dev/null +++ b/aoc-2022-dotnet/Day10/Day10.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="FParsec" Version="1.1.1" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\Common\Common.fsproj" /> + </ItemGroup> + +</Project> diff --git a/aoc-2022-dotnet/Day10/Program.fs b/aoc-2022-dotnet/Day10/Program.fs new file mode 100644 index 0000000..35f602f --- /dev/null +++ b/aoc-2022-dotnet/Day10/Program.fs @@ -0,0 +1,63 @@ +module Day10 + +open System.IO +open FParsec +open Common + +let initialCpuState = Map.empty |> Map.add 1 1 +let screenWidth = 40 +let screenHeight = 6 + +let testScreen = + "██░░██░░██░░██░░██░░██░░██░░██░░██░░██░░\n\ + ███░░░███░░░███░░░███░░░███░░░███░░░███░\n\ + ████░░░░████░░░░████░░░░████░░░░████░░░░\n\ + █████░░░░░█████░░░░░█████░░░░░█████░░░░░\n\ + ██████░░░░░░██████░░░░░░██████░░░░░░████\n\ + ███████░░░░░░░███████░░░░░░░███████░░░░░" + +type Instr = + | NOOP + | ADDX of int + + static member parse str = + let pnoop = pstring "noop" >>% NOOP + let paddx = pstring "addx " >>. pint32 |>> ADDX + let pinstr = pnoop <|> paddx + Util.parse pinstr str + +let solution actOnCpuStates = + Seq.map Instr.parse + >> Seq.fold + (fun states instr -> + let (cycle, X) = Map.maxKeyValue states + + states + |> Map.add (cycle + 1) X + |> match instr with + | NOOP -> id + | ADDX v -> Map.add (cycle + 2) (X + v)) + initialCpuState + >> actOnCpuStates + +let signalStrengthSum = + Map.filter (fun cycle _ -> cycle % 40 = 20) + >> Seq.sumBy (fun kv -> kv.Key * kv.Value) + +let drawScreen = + Map.map (fun cycle X -> + match abs ((cycle - 1) % 40 - X) <= 1 with + | true -> '█' + | false -> '░') + >> Map.values + >> Seq.truncate (screenWidth * screenHeight) + >> Seq.chunkBySize screenWidth + >> Util.matrixToString + +let test = File.ReadLines("test.txt") +assert (solution signalStrengthSum test = 13140) +assert (solution drawScreen test = testScreen) + +let input = File.ReadLines("input.txt") +printfn "%d" <| solution signalStrengthSum input +printfn "%s" <| solution drawScreen input |