Compare commits

...

10 Commits

Author SHA1 Message Date
Smaug123
c3611bffe3 Dead
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/all-checks-complete Pipeline was successful
2023-12-23 16:34:17 +00:00
Smaug123
a26a2a982b More 2023-12-23 16:31:04 +00:00
Smaug123
ad5dae8db7 Delete cata 2023-12-23 16:28:31 +00:00
Smaug123
1efec664ad Format 2023-12-23 16:27:28 +00:00
Smaug123
02a204ebb3 WIP 2023-12-23 16:07:15 +00:00
Smaug123
c84514aa38 No allocation please we're British 2023-12-23 15:42:20 +00:00
Smaug123
dbbb8a0326 Faster 2023-12-23 15:38:55 +00:00
Smaug123
9dd1148025 Rem todo 2023-12-23 15:28:20 +00:00
Smaug123
b0ebbe5842 Format 2023-12-23 15:24:43 +00:00
Smaug123
fd5b914237 Part 2 2023-12-23 15:24:37 +00:00
6 changed files with 217 additions and 69 deletions

View File

@@ -13,6 +13,7 @@
<Compile Include="Arithmetic.fs"/>
<Compile Include="Rational.fs"/>
<Compile Include="IntervalSet.fs" />
<Compile Include="List.fs" />
<Compile Include="Day1.fs"/>
<Compile Include="Day2.fs"/>
<Compile Include="Day3.fs"/>

View File

@@ -55,7 +55,8 @@ module Day19 =
| '<' -> true
| c -> failwith $"Bad comparison: %c{c}"
let operand = Int32.Parse (s.Slice (2, colon - 2), NumberStyles.None)
let operand =
Int32.Parse (s.Slice (2, colon - 2), NumberStyles.None, NumberFormatInfo.InvariantInfo)
{
Component = comp
@@ -89,7 +90,7 @@ module Day19 =
None
| _ -> failwith "bad component"
let rec compute (components : Dictionary<string, Rule ResizeArray * Dest>) x m a s (reg : string) =
let rec computePart1 (components : Dictionary<string, Rule ResizeArray * Dest>) x m a s (reg : string) =
match components.TryGetValue reg with
| false, _ -> failwith $"no rule matched: %s{reg}"
| true, (rules, dest) ->
@@ -106,7 +107,7 @@ module Day19 =
| None -> i <- i + 1
match result.Value with
| Register reg -> compute components x m a s reg
| Register reg -> computePart1 components x m a s reg
| Accept -> true
| Reject -> false
@@ -139,7 +140,8 @@ module Day19 =
let mutable s = 0
for comp in StringSplitEnumerator.make' ',' (row.Slice (1, row.Length - 2)) do
let number = Int32.Parse (comp.Slice 2, NumberStyles.None)
let number =
Int32.Parse (comp.Slice 2, NumberStyles.None, NumberFormatInfo.InvariantInfo)
match comp.[0] with
| 'x' -> x <- number
@@ -148,7 +150,7 @@ module Day19 =
| 's' -> s <- number
| c -> failwith $"Bad char: %c{c}"
if compute workflows x m a s "in" then
if computePart1 workflows x m a s "in" then
answer <- answer + x + m + a + s
answer
@@ -178,34 +180,134 @@ module Day19 =
| _, AcceptanceCriterion.True -> AcceptanceCriterion.True
| _, _ -> AcceptanceCriterion.Or (a, b)
let rec cata<'ret>
(atOr : 'ret -> 'ret -> 'ret)
(atAnd : 'ret -> 'ret -> 'ret)
(atFalse : unit -> 'ret)
(atTrue : unit -> 'ret)
(atBase : Component -> int -> int -> 'ret)
(ac : AcceptanceCriterion)
: 'ret
=
/// "low > high" means "empty interval"
[<Struct>]
type Interval =
{
Low : int
High : int
}
static member Empty =
{
Low = 1
High = 0
}
[<RequireQualifiedAccess>]
module Interval =
let size (i : Interval) =
if i.Low > i.High then 0 else i.High - i.Low + 1
let intersect (i1 : Interval) (i2 : Interval) =
if i1.Low > i1.High || i2.Low > i2.High then
i1
else if i1.High < i2.Low then
Interval.Empty
elif i2.High < i1.Low then
Interval.Empty
else
{
Low = max i1.Low i2.Low
High = min i1.High i2.High
}
type Conjunction =
{
X : Interval
M : Interval
A : Interval
S : Interval
}
static member All =
{
X =
{
Low = 1
High = 4000
}
M =
{
Low = 1
High = 4000
}
A =
{
Low = 1
High = 4000
}
S =
{
Low = 1
High = 4000
}
}
[<RequireQualifiedAccess>]
module Conjunction =
let conjAnd (c1 : Conjunction) (c2 : Conjunction) =
{
X = Interval.intersect c1.X c2.X
M = Interval.intersect c1.M c2.M
A = Interval.intersect c1.A c2.A
S = Interval.intersect c1.S c2.S
}
/// We rely on the intervals being disjoint by construction.
let size (x : Conjunction) : uint64 =
uint64 (Interval.size x.X)
* uint64 (Interval.size x.M)
* uint64 (Interval.size x.A)
* uint64 (Interval.size x.S)
type UnionOfConjunctions = Conjunction list
let rec toUnion (ac : AcceptanceCriterion) : UnionOfConjunctions =
match ac with
| AcceptanceCriterion.False -> atFalse ()
| AcceptanceCriterion.True -> atTrue ()
| AcceptanceCriterion.Base (comp, low, high) -> atBase comp low high
| AcceptanceCriterion.And (a1, a2) ->
atAnd (cata atOr atAnd atFalse atTrue atBase a1) (cata atOr atAnd atFalse atTrue atBase a2)
| AcceptanceCriterion.Or (a1, a2) ->
atOr (cata atOr atAnd atFalse atTrue atBase a1) (cata atOr atAnd atFalse atTrue atBase a2)
type UnionOfConjunctions = (Component * int * int) list list
let toUnion (ac : AcceptanceCriterion) : UnionOfConjunctions =
cata
List.append
(fun l1 l2 -> [ List.concat (l1 @ l2) ])
(fun () -> failwith "no falses")
(fun () -> failwith "no trues")
(fun comp low high -> [ [ comp, low, high ] ])
ac
| Base (comp, low, high) ->
match comp with
| Component.A ->
{ Conjunction.All with
A =
{
Low = low
High = high
}
}
| Component.M ->
{ Conjunction.All with
M =
{
Low = low
High = high
}
}
| Component.S ->
{ Conjunction.All with
S =
{
Low = low
High = high
}
}
| Component.X ->
{ Conjunction.All with
X =
{
Low = low
High = high
}
}
| _ -> failwith "bad"
|> List.singleton
| And (a, b) ->
[
Conjunction.conjAnd (List.exactlyOne (toUnion a)) (List.exactlyOne (toUnion b))
]
| Or (a, b) -> toUnion a @ toUnion b
| True -> failwith "already stripped out"
| False -> failwith "already stripped out"
let rec acceptance
(store : Dictionary<string, AcceptanceCriterion>)
@@ -219,46 +321,37 @@ module Day19 =
let rules, final = workflows.[key]
let seed =
let mutable result =
match final with
| Register s -> acceptance store workflows s
| Accept -> AcceptanceCriterion.True
| Reject -> AcceptanceCriterion.False
let result =
(seed, Seq.rev rules)
||> Seq.fold (fun crit rule ->
for i = rules.Count - 1 downto 0 do
let rule = rules.[i]
let cond =
if rule.IsLess then
AcceptanceCriterion.Base (rule.Component, 1, rule.Operand - 1)
else
AcceptanceCriterion.Base (rule.Component, rule.Operand + 1, 4000)
let negCond =
if rule.IsLess then
AcceptanceCriterion.Base (rule.Component, rule.Operand, 4000)
else
AcceptanceCriterion.Base (rule.Component, 1, rule.Operand)
result <-
match rule.Target with
| Register target ->
let cond =
if rule.IsLess then
AcceptanceCriterion.Base (rule.Component, 1, rule.Operand - 1)
else
AcceptanceCriterion.Base (rule.Component, rule.Operand + 1, 4000)
acOr (acAnd cond (acceptance store workflows target)) crit
| Accept ->
let cond =
if rule.IsLess then
AcceptanceCriterion.Base (rule.Component, 1, rule.Operand - 1)
else
AcceptanceCriterion.Base (rule.Component, rule.Operand + 1, 4000)
acOr cond crit
| Reject ->
let negCond =
if not rule.IsLess then
AcceptanceCriterion.Base (rule.Component, 1, rule.Operand - 1)
else
AcceptanceCriterion.Base (rule.Component, rule.Operand + 1, 4000)
acAnd negCond crit
)
| Register target -> acOr (acAnd cond (acceptance store workflows target)) (acAnd negCond result)
| Accept -> acOr cond (acAnd negCond result)
| Reject -> acAnd negCond result
store.[key] <- result
result
let part2 (s : string) =
let part2 (s : string) : uint64 =
use mutable rows = StringSplitEnumerator.make '\n' s
let workflows = readWorkflows &rows
@@ -267,8 +360,4 @@ module Day19 =
let a = acceptance acceptanceRanges workflows "in"
let union = toUnion a
printfn "%+A" a
printfn "%+A" union
//uint64 (IntervalSet.count resolved.X) * uint64 (IntervalSet.count resolved.M) * uint64 (IntervalSet.count resolved.A) * uint64 (IntervalSet.count resolved.S)
0uL
union |> List.sumBy Conjunction.size

View File

@@ -0,0 +1,38 @@
namespace AdventOfCode2023
open System
[<RequireQualifiedAccess>]
module List =
let rec nTuples (n : int) (xs : 'a list) : 'a list list =
#if DEBUG
if n < 0 then
raise (ArgumentException "n cannot be negative")
#endif
match n, xs with
| 0, _ -> [ [] ]
| _, [] -> []
| _, x :: xs ->
let withX = nTuples (n - 1) xs |> List.map (fun xs -> x :: xs)
let withoutX = nTuples n xs
withX @ withoutX
let inline inclusionExclusion (sizeOf : 'a -> 'ret) (sizeOfTuple : 'a list -> 'ret) (xs : 'a list) : 'ret =
let mutable result = List.sumBy sizeOf xs
let mutable i = 2
while i <= xs.Length do
let nTuples = nTuples i xs
let sum = List.sumBy sizeOfTuple nTuples
if sum = LanguagePrimitives.GenericZero then
i <- Int32.MaxValue
else
if i % 2 = 0 then
result <- result - sum
else
result <- result + sum
i <- i + 1
result

View File

@@ -13,6 +13,7 @@
<ItemGroup>
<Compile Include="Util.fs"/>
<Compile Include="TestIntervalSet.fs" />
<Compile Include="TestList.fs" />
<Compile Include="TestDay1.fs"/>
<Compile Include="TestDay2.fs"/>
<Compile Include="TestDay3.fs"/>

View File

@@ -7,7 +7,6 @@ open System.IO
[<TestFixture>]
module TestDay19 =
[<Test>]
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day19.txt"
@@ -43,4 +42,4 @@ module TestDay19 =
Assert.Inconclusive ()
failwith "unreachable"
Day19.part2 s |> shouldEqual 8314uL
Day19.part2 s |> shouldEqual 127675188176682uL

View File

@@ -0,0 +1,20 @@
namespace Test
open NUnit.Framework
open FsUnitTyped
open FsCheck
open AdventOfCode2023
[<TestFixture>]
module TestList =
[<Test>]
let ``n-tuples have the right length`` () =
let property (n : int) (xs : char list) =
let n = min (abs n) 6
let xs = xs |> List.take (min 10 xs.Length)
let tuples = List.nTuples n xs
tuples |> List.forall (fun i -> i.Length = n)
property 1 [ 'v' ] |> shouldEqual true
Check.QuickThrowOnFailure property