mirror of
https://github.com/Smaug123/AdventOfCode2022
synced 2025-10-05 17:48:40 +00:00
Initial commit, day 1
This commit is contained in:
12
.config/dotnet-tools.json
Normal file
12
.config/dotnet-tools.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"version": 1,
|
||||
"isRoot": true,
|
||||
"tools": {
|
||||
"fantomas": {
|
||||
"version": "5.1.0",
|
||||
"commands": [
|
||||
"fantomas"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
41
.editorconfig
Normal file
41
.editorconfig
Normal file
@@ -0,0 +1,41 @@
|
||||
root=true
|
||||
|
||||
[*]
|
||||
charset=utf-8
|
||||
end_of_line=crlf
|
||||
trim_trailing_whitespace=true
|
||||
insert_final_newline=true
|
||||
indent_style=space
|
||||
indent_size=4
|
||||
|
||||
# ReSharper properties
|
||||
resharper_xml_indent_size=2
|
||||
resharper_xml_max_line_length=100
|
||||
resharper_xml_tab_width=2
|
||||
|
||||
[*.{csproj,fsproj,sqlproj,targets,props,ts,tsx,css,json}]
|
||||
indent_style=space
|
||||
indent_size=2
|
||||
|
||||
[*.{fs,fsi}]
|
||||
fsharp_bar_before_discriminated_union_declaration=true
|
||||
fsharp_space_before_uppercase_invocation=true
|
||||
fsharp_space_before_class_constructor=true
|
||||
fsharp_space_before_member=true
|
||||
fsharp_space_before_colon=true
|
||||
fsharp_space_before_semicolon=true
|
||||
fsharp_multiline_block_brackets_on_same_column=true
|
||||
fsharp_newline_between_type_definition_and_members=true
|
||||
fsharp_align_function_signature_to_indentation=true
|
||||
fsharp_alternative_long_member_definitions=true
|
||||
fsharp_multi_line_lambda_closing_newline=true
|
||||
fsharp_experimental_keep_indent_in_branch=true
|
||||
fsharp_max_value_binding_width=80
|
||||
fsharp_max_record_width=0
|
||||
max_line_length=120
|
||||
end_of_line=lf
|
||||
|
||||
[*.{appxmanifest,build,dtd,nuspec,xaml,xamlx,xoml,xsd}]
|
||||
indent_style=space
|
||||
indent_size=2
|
||||
tab_width=2
|
49
.github/workflows/dotnet.yaml
vendored
Normal file
49
.github/workflows/dotnet.yaml
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
name: .NET
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
env:
|
||||
DOTNET_NOLOGO: true
|
||||
DOTNET_CLI_TELEMETRY_OPTOUT: true
|
||||
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v3
|
||||
with:
|
||||
dotnet-version: 6.0.x
|
||||
- name: Restore dependencies
|
||||
run: dotnet restore
|
||||
- name: Build
|
||||
run: dotnet build --no-restore
|
||||
- name: Test
|
||||
run: dotnet test --no-build --verbosity normal
|
||||
|
||||
check-format:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: Setup .NET SDK v6.0.x
|
||||
uses: actions/setup-dotnet@v3
|
||||
with:
|
||||
dotnet-version: 6.0.x
|
||||
- name: Prepare .NET tools
|
||||
run: dotnet tool restore
|
||||
- name: Run Fantomas
|
||||
run: ./hooks/pre-push
|
||||
|
||||
all-required-checks-complete:
|
||||
needs: [check-format, build]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: echo "All required checks complete."
|
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
bin/
|
||||
obj/
|
||||
/packages/
|
||||
riderModule.iml
|
||||
/_ReSharper.Caches/
|
||||
.idea/
|
||||
*.sln.DotSettings.user
|
29
AdventOfCode2022.Test/AdventOfCode2022.Test.fsproj
Normal file
29
AdventOfCode2022.Test/AdventOfCode2022.Test.fsproj
Normal file
@@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<GenerateProgramFile>false</GenerateProgramFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Assembly.fs" />
|
||||
<Compile Include="Day1.fs" />
|
||||
<EmbeddedResource Include="Inputs/Day1.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FsUnit" Version="5.1.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AdventOfCode2022\AdventOfCode2022.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
20
AdventOfCode2022.Test/Assembly.fs
Normal file
20
AdventOfCode2022.Test/Assembly.fs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace AdventOfCode2022.Test
|
||||
|
||||
open System.IO
|
||||
open System.Reflection
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Assembly =
|
||||
|
||||
type private Dummy =
|
||||
class
|
||||
end
|
||||
|
||||
let readResource (name : string) : string =
|
||||
let asm = Assembly.GetAssembly typeof<Dummy>
|
||||
|
||||
use stream =
|
||||
asm.GetManifestResourceStream (sprintf "AdventOfCode2022.Test.Inputs.%s" name)
|
||||
|
||||
use reader = new StreamReader (stream)
|
||||
reader.ReadToEnd ()
|
61
AdventOfCode2022.Test/Day1.fs
Normal file
61
AdventOfCode2022.Test/Day1.fs
Normal file
@@ -0,0 +1,61 @@
|
||||
namespace AdventOfCode2022.Test
|
||||
|
||||
open AdventOfCode2022
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
|
||||
[<TestFixture>]
|
||||
module TestDay1 =
|
||||
|
||||
[<Test>]
|
||||
let ``Part 1`` () =
|
||||
let input = Assembly.readResource "Day1.txt"
|
||||
input.Split '\n' |> Day1.part1 |> shouldEqual 66306
|
||||
|
||||
[<Test>]
|
||||
let ``Part 2`` () =
|
||||
let input = Assembly.readResource "Day1.txt"
|
||||
input.Split '\n' |> Day1.part2 |> shouldEqual 195292
|
||||
|
||||
[<Test>]
|
||||
let ``Part 1, given example`` () =
|
||||
"""1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
"""
|
||||
|> fun s -> s.Split '\n'
|
||||
|> Day1.part1
|
||||
|> shouldEqual 24000
|
||||
|
||||
|
||||
[<Test>]
|
||||
let ``Part 2, given example`` () =
|
||||
"""1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
"""
|
||||
|> fun s -> s.Split '\n'
|
||||
|> Day1.part2
|
||||
|> shouldEqual 45000
|
2244
AdventOfCode2022.Test/Inputs/Day1.txt
Normal file
2244
AdventOfCode2022.Test/Inputs/Day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
22
AdventOfCode2022.sln
Normal file
22
AdventOfCode2022.sln
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "AdventOfCode2022", "AdventOfCode2022\AdventOfCode2022.fsproj", "{6C4FDE18-389F-4AE6-AE9A-B4BD5BA19773}"
|
||||
EndProject
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "AdventOfCode2022.Test", "AdventOfCode2022.Test\AdventOfCode2022.Test.fsproj", "{F22BC060-E7D0-44EE-95F1-0CEFFD8D46E1}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{6C4FDE18-389F-4AE6-AE9A-B4BD5BA19773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6C4FDE18-389F-4AE6-AE9A-B4BD5BA19773}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6C4FDE18-389F-4AE6-AE9A-B4BD5BA19773}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6C4FDE18-389F-4AE6-AE9A-B4BD5BA19773}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F22BC060-E7D0-44EE-95F1-0CEFFD8D46E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F22BC060-E7D0-44EE-95F1-0CEFFD8D46E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F22BC060-E7D0-44EE-95F1-0CEFFD8D46E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F22BC060-E7D0-44EE-95F1-0CEFFD8D46E1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
12
AdventOfCode2022/AdventOfCode2022.fsproj
Normal file
12
AdventOfCode2022/AdventOfCode2022.fsproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Day1.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
36
AdventOfCode2022/Day1.fs
Normal file
36
AdventOfCode2022/Day1.fs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace AdventOfCode2022
|
||||
|
||||
open System
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day1 =
|
||||
|
||||
let counts (lines : string seq) : int list =
|
||||
((0, []), lines)
|
||||
||> Seq.fold (fun (acc, counts) line ->
|
||||
if String.IsNullOrWhiteSpace line then
|
||||
0, (acc :: counts)
|
||||
else
|
||||
match Int32.TryParse line with
|
||||
| false, _ -> failwithf "not an int: %s" line
|
||||
| true, v -> acc + v, counts
|
||||
)
|
||||
|> snd
|
||||
|
||||
/// Expects a trailing newline (as is present in the given input data).
|
||||
let part1 (lines : string seq) : int = lines |> counts |> List.max
|
||||
|
||||
/// I wanted to save cloning the entire seq, so here is some bonus efficiency.
|
||||
let maxThree (inputs : int list) : struct (int * int * int) =
|
||||
((struct (Int32.MinValue, Int32.MinValue, Int32.MinValue)), inputs)
|
||||
||> List.fold (fun (struct (max1, max2, max3) as maxes) input ->
|
||||
if input <= max3 then maxes
|
||||
elif input <= max2 then struct (max1, max2, input)
|
||||
elif input <= max1 then struct (max1, input, max2)
|
||||
else struct (input, max1, max2)
|
||||
)
|
||||
|
||||
let part2 (lines : string seq) : int =
|
||||
let struct (a, b, c) = lines |> counts |> maxThree
|
||||
|
||||
a + b + c
|
6
hooks/pre-push
Executable file
6
hooks/pre-push
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
if ! dotnet tool run fantomas --check -r . ; then
|
||||
echo "Formatting incomplete. Consider running 'dotnet tool run fantomas -r .'"
|
||||
exit 1
|
||||
fi
|
Reference in New Issue
Block a user