2 Commits

Author SHA1 Message Date
Smaug123
3da77f08c3 Merge branch 'main' into day-18
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
2023-12-23 21:35:26 +00:00
Smaug123
fd90f94653 Part 1
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
2023-12-23 21:28:39 +00:00
15 changed files with 195 additions and 11 deletions

View File

@@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"fantomas": {
"version": "7.0.3",
"version": "6.2.3",
"commands": [
"fantomas"
]

View File

@@ -2,6 +2,7 @@ root=true
[*]
charset=utf-8
end_of_line=crlf
trim_trailing_whitespace=true
insert_final_newline=true
indent_style=space

1
.envrc
View File

@@ -1 +0,0 @@
use flake

1
.gitignore vendored
View File

@@ -8,7 +8,6 @@ _ReSharper.Caches/
.DS_Store
result
.profile*
.direnv/
inputs/
AdventOfCode2023.FSharp/Test/TestResults/

View File

@@ -31,6 +31,7 @@
<Compile Include="Day14.fs" />
<Compile Include="Day15.fs" />
<Compile Include="Day16.fs" />
<Compile Include="Day18.fs" />
<Compile Include="Day19.fs" />
</ItemGroup>

View File

@@ -0,0 +1,103 @@
namespace AdventOfCode2023
#if DEBUG
#else
#nowarn "9"
#endif
open System
open System.Collections.Generic
open System.Globalization
[<RequireQualifiedAccess>]
module Day18 =
let part1 (s : string) =
// Interleaved row and col
let mutable weHave = ResizeArray<int> ()
weHave.Add 0
weHave.Add 0
let mutable minCol = 0
let mutable maxCol = 0
let mutable minRow = 0
let mutable maxRow = 0
let mutable col = 0
let mutable row = 0
let mutable edgeCount = 0
use rows = StringSplitEnumerator.make '\n' s
for currRow in rows do
use mutable entries = StringSplitEnumerator.make' ' ' currRow
entries.MoveNext () |> ignore
let dir = Direction.ofChar entries.Current.[0]
entries.MoveNext () |> ignore
let distance =
Int32.Parse (entries.Current, NumberStyles.None, CultureInfo.InvariantCulture)
edgeCount <- edgeCount + distance
match dir with
| Direction.Down ->
for _ = 1 to distance do
row <- row + 1
weHave.Add row
weHave.Add col
maxRow <- max row maxRow
| Direction.Up ->
for _ = 1 to distance do
row <- row - 1
weHave.Add row
weHave.Add col
minRow <- min row minRow
| Direction.Left ->
for _ = 1 to distance do
col <- col - 1
weHave.Add row
weHave.Add col
minCol <- min col minCol
| Direction.Right ->
for _ = 1 to distance do
col <- col + 1
weHave.Add row
weHave.Add col
maxCol <- max col maxCol
| _ -> failwith "bad dir"
let buffer = Array.zeroCreate ((maxRow - minRow + 3) * (maxCol - minCol + 3))
#if DEBUG
let system : Arr2D<byte> =
{
Elements = buffer
Width = maxCol - minCol + 3
}
#else
use ptr = fixed buffer
let system : Arr2D<byte> =
{
Elements = ptr
Length = buffer.Length
Width = maxCol - minCol + 3
}
#endif
let mutable pointIndex = 0
while pointIndex < weHave.Count do
let row = weHave.[pointIndex]
let col = weHave.[pointIndex + 1]
Arr2D.set system (col - minCol + 1) (row - minRow + 1) 1uy
pointIndex <- pointIndex + 2
Arr2D.floodFill (ResizeArray ()) system 0uy 2uy 0 0
Arr2D.count system 0uy + edgeCount
let part2 (s : string) = -1

View File

@@ -316,6 +316,22 @@ module Program =
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
Console.WriteLine "=====Day 18====="
do
let input = Path.Combine (dir.FullName, "day18.txt") |> File.ReadAllText
sw.Restart ()
let part1 = Day18.part1 input
sw.Stop ()
Console.WriteLine (part1.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
sw.Restart ()
let part2 = Day18.part2 input
sw.Stop ()
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
Console.WriteLine "=====Day 19====="
do

View File

@@ -30,6 +30,7 @@
<Compile Include="TestDay14.fs" />
<Compile Include="TestDay15.fs" />
<Compile Include="TestDay16.fs" />
<Compile Include="TestDay18.fs" />
<Compile Include="TestDay19.fs" />
<EmbeddedResource Include="samples\day1.txt"/>
<EmbeddedResource Include="samples\day1part1.txt"/>
@@ -50,6 +51,7 @@
<EmbeddedResource Include="samples\day14.txt" />
<EmbeddedResource Include="samples\day15.txt" />
<EmbeddedResource Include="samples\day16.txt" />
<EmbeddedResource Include="samples\day18.txt" />
<EmbeddedResource Include="samples\day19.txt" />
</ItemGroup>

View File

@@ -0,0 +1,44 @@
namespace AdventOfCode2023.Test
open AdventOfCode2023
open NUnit.Framework
open FsUnitTyped
open System.IO
[<TestFixture>]
module TestDay18 =
[<Test>]
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day18.txt"
[<Test>]
let part1Sample () = sample |> Day18.part1 |> shouldEqual 62
[<Test>]
let part2Sample () = sample |> Day18.part2 |> shouldEqual 0
[<Test>]
let part1Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day18.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day18.part1 s |> shouldEqual 106459
[<Test>]
let part2Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day18.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day18.part2 s |> shouldEqual 0

View File

@@ -3,7 +3,9 @@ namespace AdventOfCode2023.Test
open System.IO
open System.Reflection
type Dummy = class end
type Dummy =
class
end
[<RequireQualifiedAccess>]
module Assembly =

View File

@@ -0,0 +1,14 @@
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)

12
flake.lock generated
View File

@@ -5,11 +5,11 @@
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"lastModified": 1694529238,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github"
},
"original": {
@@ -20,11 +20,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1757068644,
"narHash": "sha256-NOrUtIhTkIIumj1E/Rsv1J37Yi3xGStISEo8tZm3KW4=",
"lastModified": 1701253981,
"narHash": "sha256-ztaDIyZ7HrTAfEEUt9AtTDNoCYxUdSd6NrRHaYOIxtk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8eb28adfa3dc4de28e792e3bf49fcf9007ca8ac9",
"rev": "e92039b55bcd58469325ded85d4f58dd5a4eaf58",
"type": "github"
},
"original": {

View File

@@ -14,12 +14,15 @@
system: let
pkgs = nixpkgs.legacyPackages.${system};
in
# Conditionally include Swift for Darwin systems
# Conditionally include Swift and Apple SDK for Darwin systems
let
darwinDeps =
if system == "x86_64-darwin" || system == "aarch64-darwin"
then [
pkgs.swift
pkgs.darwin.apple_sdk.frameworks.Foundation
pkgs.darwin.apple_sdk.frameworks.CryptoKit
pkgs.darwin.apple_sdk.frameworks.GSS
]
else [];
in {