mirror of
https://github.com/Smaug123/AdventOfCode2022
synced 2025-10-06 18:18:40 +00:00
Day 4 (#5)
This commit is contained in:
@@ -10,9 +10,11 @@
|
||||
<Compile Include="Day1.fs" />
|
||||
<Compile Include="Day2.fs" />
|
||||
<Compile Include="Day3.fs" />
|
||||
<Compile Include="Day4.fs" />
|
||||
<EmbeddedResource Include="Inputs/Day1.txt" />
|
||||
<EmbeddedResource Include="Inputs/Day2.txt" />
|
||||
<EmbeddedResource Include="Inputs\Day3.txt" />
|
||||
<EmbeddedResource Include="Inputs\Day4.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
52
AdventOfCode2022.Test/Day4.fs
Normal file
52
AdventOfCode2022.Test/Day4.fs
Normal file
@@ -0,0 +1,52 @@
|
||||
namespace AdventOfCode2022.Test
|
||||
|
||||
open System
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open AdventOfCode2022
|
||||
|
||||
[<TestFixture>]
|
||||
module TestDay4 =
|
||||
|
||||
[<Test>]
|
||||
let ``Part 1, given`` () =
|
||||
"""2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8"""
|
||||
|> fun s -> s.Split System.Environment.NewLine
|
||||
|> Day4.part1
|
||||
|> shouldEqual 2
|
||||
|
||||
[<Test>]
|
||||
let ``Part 1`` () =
|
||||
let input = Assembly.readResource "Day4.txt"
|
||||
|
||||
input.Split '\n'
|
||||
|> Seq.filter (not << String.IsNullOrWhiteSpace)
|
||||
|> Day4.part1
|
||||
|> shouldEqual 433
|
||||
|
||||
|
||||
[<Test>]
|
||||
let ``Part 2, given`` () =
|
||||
"""2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8"""
|
||||
|> fun s -> s.Split System.Environment.NewLine
|
||||
|> Day4.part2
|
||||
|> shouldEqual 4
|
||||
|
||||
[<Test>]
|
||||
let ``Part 2`` () =
|
||||
let input = Assembly.readResource "Day4.txt"
|
||||
|
||||
input.Split '\n'
|
||||
|> Seq.filter (not << String.IsNullOrWhiteSpace)
|
||||
|> Day4.part2
|
||||
|> shouldEqual 852
|
1000
AdventOfCode2022.Test/Inputs/Day4.txt
Normal file
1000
AdventOfCode2022.Test/Inputs/Day4.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,7 @@
|
||||
<Compile Include="Day1.fs" />
|
||||
<Compile Include="Day2.fs" />
|
||||
<Compile Include="Day3.fs" />
|
||||
<Compile Include="Day4.fs" />
|
||||
<Compile Include="Day3Efficient.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
|
36
AdventOfCode2022/Day4.fs
Normal file
36
AdventOfCode2022/Day4.fs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace AdventOfCode2022
|
||||
|
||||
open System
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day4 =
|
||||
|
||||
/// This only checks if the first arg fully contains the second arg.
|
||||
let inline fullyContains (a, b) (c, d) : bool = a <= c && b >= d
|
||||
|
||||
let inline overlaps (a, b) (c, d) : bool = b >= c && a <= d
|
||||
|
||||
let parse (s : ReadOnlySpan<char>) : (int * int) * (int * int) =
|
||||
let commaIndex = s.IndexOf ','
|
||||
let firstElf = s.Slice (0, commaIndex)
|
||||
let secondElf = s.Slice (commaIndex + 1)
|
||||
let firstDashIndex = firstElf.IndexOf '-'
|
||||
let firstElf1 = firstElf.Slice (0, firstDashIndex)
|
||||
let firstElf2 = firstElf.Slice (firstDashIndex + 1)
|
||||
let secondDashIndex = secondElf.IndexOf '-'
|
||||
let secondElf1 = secondElf.Slice (0, secondDashIndex)
|
||||
let secondElf2 = secondElf.Slice (secondDashIndex + 1)
|
||||
(Int32.Parse firstElf1, Int32.Parse firstElf2), (Int32.Parse secondElf1, Int32.Parse secondElf2)
|
||||
|
||||
let part1 (lines : string seq) : int =
|
||||
lines
|
||||
|> Seq.map (fun s -> parse (s.AsSpan ()))
|
||||
|> Seq.filter (fun (firstElf, secondElf) -> fullyContains firstElf secondElf || fullyContains secondElf firstElf
|
||||
)
|
||||
|> Seq.length
|
||||
|
||||
let part2 (lines : string seq) : int =
|
||||
lines
|
||||
|> Seq.map (fun s -> parse (s.AsSpan ()))
|
||||
|> Seq.filter (fun (firstElf, secondElf) -> overlaps firstElf secondElf)
|
||||
|> Seq.length
|
Reference in New Issue
Block a user