aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-10 13:26:15 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-10 13:26:15 +0100
commit520a01ed88fafb165ecb3eaa9fd3e39db0431722 (patch)
treee4ec65b5b1dd5c51b5d22810b7c79ff2f5c6381c
parent3c444c7e3300ea7df5dcc8811e216ff552136fa9 (diff)
downloadgleam_aoc2020-520a01ed88fafb165ecb3eaa9fd3e39db0431722.tar.gz
gleam_aoc2020-520a01ed88fafb165ecb3eaa9fd3e39db0431722.zip
Finish day 10
-rw-r--r--README.md2
-rw-r--r--aoc-2022-dotnet/Common/Util.fs5
-rw-r--r--aoc-2022-dotnet/Day10/Day10.fsproj26
-rw-r--r--aoc-2022-dotnet/Day10/Program.fs63
-rw-r--r--aoc-2022-dotnet/README.md4
-rw-r--r--aoc-2022-dotnet/aoc-2022-dotnet.sln16
6 files changed, 108 insertions, 8 deletions
diff --git a/README.md b/README.md
index 3f98666..c875d98 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ Repository storing my solutions to Advent of Code. See other people's solutions
### [2022](aoc-2022-dotnet)
![.NET](https://img.shields.io/badge/.NET-grey?logo=.NET)
-![Stars](https://img.shields.io/badge/🌟%20stars-18/50-orange)
+![Stars](https://img.shields.io/badge/🌟%20stars-20/50-orange)
[awesome]: https://github.com/Bogdanp/awesome-advent-of-code
[aoc]: https://adventofcode.com
diff --git a/aoc-2022-dotnet/Common/Util.fs b/aoc-2022-dotnet/Common/Util.fs
index a9f3a1e..6393aad 100644
--- a/aoc-2022-dotnet/Common/Util.fs
+++ b/aoc-2022-dotnet/Common/Util.fs
@@ -25,6 +25,11 @@ module Util =
| [ x; y ] -> x, y
| _ -> failwith "Invalid string format!"
+ let matrixToString m =
+ m
+ |> Seq.map (Seq.map string >> String.concat "")
+ |> String.concat "\n"
+
let topN n xs =
let rec insertSorted x =
function
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
diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md
index a0ce07f..60906d3 100644
--- a/aoc-2022-dotnet/README.md
+++ b/aoc-2022-dotnet/README.md
@@ -1,6 +1,6 @@
# Advent of Code 2022 in .NET
![.NET](https://img.shields.io/badge/.NET-grey?logo=.NET)
-![Stars](https://img.shields.io/badge/🌟%20stars-18/50-orange)
+![Stars](https://img.shields.io/badge/🌟%20stars-20/50-orange)
## Progress
| Day | Part 1 | Part 2 |
@@ -14,7 +14,7 @@
| Day 7: No Space Left On Device | 🌟 | 🌟 |
| Day 8: Treetop Tree House | 🌟 | 🌟 |
| Day 9: Rope Bridge | 🌟 | 🌟 |
-| Day 10: ??? | | |
+| Day 10: Cathode-Ray Tube | 🌟 | 🌟 |
| Day 11: ??? | | |
| Day 12: ??? | | |
| Day 13: ??? | | |
diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln
index 3d324d0..b1299dc 100644
--- a/aoc-2022-dotnet/aoc-2022-dotnet.sln
+++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln
@@ -30,7 +30,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day07", "Day07\Day07.fsproj
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day08", "Day08\Day08.fsproj", "{F33E64B7-EC50-4017-B735-C8900684BF0D}"
EndProject
-Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day09", "Day09\Day09.fsproj", "{E0975DA5-E104-4969-A685-85D36F28A321}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day09", "Day09\Day09.fsproj", "{E0975DA5-E104-4969-A685-85D36F28A321}"
+EndProject
+Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day10", "Day10\Day10.fsproj", "{FD0F0852-30E0-485E-9423-B25CB6C4C035}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -38,6 +40,10 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {548B09AD-6595-45A3-A19E-147DC41615C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {548B09AD-6595-45A3-A19E-147DC41615C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {548B09AD-6595-45A3-A19E-147DC41615C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {548B09AD-6595-45A3-A19E-147DC41615C4}.Release|Any CPU.Build.0 = Release|Any CPU
{3148A67C-BDDB-4660-83E6-C2C6D6016A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3148A67C-BDDB-4660-83E6-C2C6D6016A47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3148A67C-BDDB-4660-83E6-C2C6D6016A47}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -74,14 +80,14 @@ Global
{F33E64B7-EC50-4017-B735-C8900684BF0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F33E64B7-EC50-4017-B735-C8900684BF0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F33E64B7-EC50-4017-B735-C8900684BF0D}.Release|Any CPU.Build.0 = Release|Any CPU
- {548B09AD-6595-45A3-A19E-147DC41615C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {548B09AD-6595-45A3-A19E-147DC41615C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {548B09AD-6595-45A3-A19E-147DC41615C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {548B09AD-6595-45A3-A19E-147DC41615C4}.Release|Any CPU.Build.0 = Release|Any CPU
{E0975DA5-E104-4969-A685-85D36F28A321}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0975DA5-E104-4969-A685-85D36F28A321}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0975DA5-E104-4969-A685-85D36F28A321}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0975DA5-E104-4969-A685-85D36F28A321}.Release|Any CPU.Build.0 = Release|Any CPU
+ {FD0F0852-30E0-485E-9423-B25CB6C4C035}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {FD0F0852-30E0-485E-9423-B25CB6C4C035}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {FD0F0852-30E0-485E-9423-B25CB6C4C035}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {FD0F0852-30E0-485E-9423-B25CB6C4C035}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE