Initial commit of day 1

This commit is contained in:
Smaug123
2021-12-06 19:37:33 +00:00
commit 7d24b7d8d0
15 changed files with 2248 additions and 0 deletions

12
.config/dotnet-tools.json Normal file
View File

@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"fantomas-tool": {
"version": "4.5.10",
"commands": [
"fantomas"
]
}
}
}

14
.editorconfig Normal file
View File

@@ -0,0 +1,14 @@
root = true
[*.{fs,fsi,fsx}]
fsharp_space_before_uppercase_invocation=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_keep_indent_in_branch=true
fsharp_align_function_signature_to_indentation=true
fsharp_alternative_long_member_definitions=true
fsharp_disable_elmish_syntax=true
fsharp_multi_line_lambda_closing_newline=true

27
.github/workflows/dotnet-core.yaml vendored Normal file
View File

@@ -0,0 +1,27 @@
name: .NET Core
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.100
- name: Install dependencies
run: dotnet restore AdventOfCode2021.sln
- name: Build
run: dotnet build AdventOfCode2021.sln --configuration Release --no-restore
- name: Test
run: dotnet test AdventOfCode2021.sln --no-restore --verbosity normal
- name: Run Fantomas
run: dotnet tool run fantomas --check -r .

8
.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea/
.ionide/
*.sln.DotSettings.user

18
AdventOfCode2021.fsproj Normal file
View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Utils.fs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Inputs/Day1.txt" />
<Compile Include="Day1.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
</Project>

22
AdventOfCode2021.sln Normal file
View File

@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "AdventOfCode2021", "AdventOfCode2021.fsproj", "{47D24174-5D7A-44F5-8215-F2D2B00BA452}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test", "Test\Test.fsproj", "{C3ED559C-4D93-41F9-91C2-2B95F7EA3A83}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{47D24174-5D7A-44F5-8215-F2D2B00BA452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47D24174-5D7A-44F5-8215-F2D2B00BA452}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47D24174-5D7A-44F5-8215-F2D2B00BA452}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47D24174-5D7A-44F5-8215-F2D2B00BA452}.Release|Any CPU.Build.0 = Release|Any CPU
{C3ED559C-4D93-41F9-91C2-2B95F7EA3A83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3ED559C-4D93-41F9-91C2-2B95F7EA3A83}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3ED559C-4D93-41F9-91C2-2B95F7EA3A83}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3ED559C-4D93-41F9-91C2-2B95F7EA3A83}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

20
Day1.fs Normal file
View File

@@ -0,0 +1,20 @@
namespace AdventOfCode
open AdventOfCode.Internals
[<RequireQualifiedAccess>]
module Day1 =
let part1 () =
Utils.readResource "Day1.txt"
|> Seq.map int
|> Seq.pairwise
|> Seq.filter (fun (a, b) -> a < b)
|> Seq.length
let part2 () =
Utils.readResource "Day1.txt"
|> Seq.map int
|> Seq.windowed 4
|> Seq.filter (fun arr -> arr.[0] < arr.[3])
|> Seq.length

2000
Inputs/day1.txt Normal file

File diff suppressed because it is too large Load Diff

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Patrick Stevens
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH TH SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.E

12
Program.fs Normal file
View File

@@ -0,0 +1,12 @@
namespace AdventOfCode
module Program =
[<EntryPoint>]
let main (argv : string []) : int =
match argv.[0] with
| "1" ->
printfn $"{Day1.part1 ()}"
printfn $"{Day1.part2 ()}"
| s ->
failwithf $"Unexpected argument: %s{s}"
0

16
README.md Normal file
View File

@@ -0,0 +1,16 @@
# Advent of Code 2021, in F#
I'm not promising to actually do this; my [main attempt](https://github.com/Smaug123/advent-of-code-2021) is in Rust.
# Development tips
There are pull request checks on this repo, enforcing [Fantomas](https://github.com/fsprojects/fantomas/)-compliant formatting.
After checking out the repo, you may wish to add a pre-push hook to ensure locally that formatting is complete, rather than having to wait for the CI checks to tell you that you haven't formatted your code.
Consider performing the following command to set this up in the repo:
```bash
git config core.hooksPath hooks/
```
Before your first push (but only once), you will need to install the [.NET local tools](https://docs.microsoft.com/en-us/dotnet/core/tools/local-tools-how-to-use) which form part of the pre-push hook:
```bash
dotnet tool restore
```

25
Test/Test.fsproj Normal file
View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="TestDay1.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FsUnit" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdventOfCode2021.fsproj" />
</ItemGroup>
</Project>

17
Test/TestDay1.fs Normal file
View File

@@ -0,0 +1,17 @@
namespace AdventOfCode2021.Test
open AdventOfCode
open Xunit
open FsUnitTyped
module TestDay1 =
[<Fact>]
let ``Part 1`` () =
Day1.part1 ()
|> shouldEqual 1766
[<Fact>]
let ``Part 2`` () =
Day1.part2 ()
|> shouldEqual 1797

19
Utils.fs Normal file
View File

@@ -0,0 +1,19 @@
namespace AdventOfCode.Internals
open System.IO
open System.Reflection
[<RequireQualifiedAccess>]
module Utils =
type private Dummy = class end
let readResource' (name : string) : string array =
let asm = Assembly.GetAssembly typeof<Dummy>
use stream = asm.GetManifestResourceStream (sprintf "AdventOfCode2021.Inputs.%s" name)
let s =
use reader = new StreamReader(stream)
reader.ReadToEnd()
s.Split('\r', '\n')
let inline readResource (name : string) : string list =
readResource' name |> List.ofArray

17
hooks/pre-push Normal file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/python3
import subprocess
def check_fantomas():
result = subprocess.run(["dotnet", "tool", "run", "fantomas", "--check", "-r", "."])
if result.returncode != 0:
print(result.stdout)
raise Exception(f"Formatting incomplete (return code: {result.returncode}). Consider running `dotnet tool run fantomas -r .`")
def main():
check_fantomas()
if __name__ == "__main__":
main()