Skip to content

Commit

Permalink
Day 7 Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kodfodrasz committed Dec 29, 2024
1 parent c6601fa commit 220274d
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Kodfodrasz.AoC.Year2024.Tests/Day7Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module Kodfodrasz.AoC.Year2024.Tests.Day7Tests

open Xunit
open Swensen.Unquote.Assertions

open Kodfodrasz.AoC
open Kodfodrasz.AoC.Year2024
open Kodfodrasz.AoC.Year2024.Day7


let exampleInput = """
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
"""

[<Fact>]
let ``Parsing example input`` () =
let expected = [
{
TestValue = 190L
Terms = [10L; 19L]
}
{
TestValue = 3267L
Terms = [81L; 40L; 27L]
}
{
TestValue = 83L
Terms = [17L; 5L]
}
{
TestValue = 156L
Terms = [15L; 6L]
}
{
TestValue = 7290L
Terms = [6L; 8L; 6L; 15L]
}
{
TestValue = 161011L
Terms = [16L; 10L; 13L]
}
{
TestValue = 192L
Terms = [17L; 8L; 14L]
}
{
TestValue = 21037L
Terms = [9L; 7L; 18L; 13L]
}
{
TestValue = 292L
Terms = [11L; 6L; 16L; 20L]
}
]

test
<@ let actual = parseInput exampleInput
actual = Ok expected @>

[<Fact>]
let ``Answer 1 helper function: check`` () =
let input = parseInput exampleInput |> Result.get

test <@ true = Day7.check input[0] @>
test <@ true = Day7.check input[1] @>
test <@ false = Day7.check input[3] @>
test <@ false = Day7.check input[5] @>
test <@ true = Day7.check input[8] @>


[<Fact>]
let ``Answer 1 for example input`` () =
let input = parseInput exampleInput

test
<@ let actual = Result.bind answer1 input
let expected: Result<_, string> = Ok 3749L
actual = expected @>

[<Fact(Skip="TODO")>]
let ``Answer 2 for example input`` () =
let input = parseInput exampleInput

test
<@ let actual = Result.bind answer2 input
let expected: Result<_, string> = Ok 31
actual = expected @>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Compile Include="Day3Tests.fs" />
<Compile Include="Day4Tests.fs" />
<Compile Include="Day5Tests.fs" />
<Compile Include="Day7Tests.fs" />
<Compile Include="Day11Tests.fs" />
<Compile Include="Day13Tests.fs" />
<Compile Include="Day20Tests.fs" />
Expand Down
75 changes: 75 additions & 0 deletions Kodfodrasz.AoC.Year2024/Day7.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module Kodfodrasz.AoC.Year2024.Day7

open System
open System.Text.RegularExpressions
open Kodfodrasz.AoC

type CalibrationEquation = {
TestValue : int64
Terms: int64 list
}

type parsedInput = CalibrationEquation list
let parseInput (input: string): Result<parsedInput,string> =
let matchesEquatons= Regex.Matches(
input,
@"^\s*(?<testvalue>\d+):(?:\s+(?<n>\d+))+\s*$",
RegexOptions.Multiline)

let matchedEquations =
let parseRule (m:Match) =
let tv = m.Groups["testvalue"].Value |> int64
let terms =
m.Groups["n"].Captures
|> Seq.map (fun (c:Capture) -> c.Value |> int64)
|> Seq.toList

{
TestValue = tv
Terms = terms
}

matchesEquatons
|> Seq.map parseRule
|> Seq.toList

Ok matchedEquations

let check (eqn : CalibrationEquation) =
let rec check goal acc terms =
match terms with
| [] -> goal = acc
| head :: tail ->
if acc > goal then false
else
if (check goal (acc + head) tail) then
true
else
check goal (acc * head) tail
match eqn.Terms with
| [] -> false
| h :: t -> check eqn.TestValue h t

let answer1 (data : parsedInput) =
data
|> List.toArray
|> Array.Parallel.filter check
|> Array.sumBy(fun eqn -> eqn.TestValue)
|> Ok

let answer2 (data : parsedInput) =
failwith "TODO"

type Solver() =
inherit SolverBase("Bridge Repair")
with
override this.Solve input =
input
|>
this.DoSolve
(parseInput)
[
answer1;
answer2;
]

1 change: 1 addition & 0 deletions Kodfodrasz.AoC.Year2024/Kodfodrasz.AoC.Year2024.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Compile Include="Day3.fs" />
<Compile Include="Day4.fs" />
<Compile Include="Day5.fs" />
<Compile Include="Day7.fs" />
<Compile Include="Day11.fs" />
<Compile Include="Day13.fs" />
<Compile Include="Day20.fs" />
Expand Down

0 comments on commit 220274d

Please sign in to comment.