Day 7: Bridge Repair

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

You are viewing a single thread.
View all comments
1 point

I’m way behind, but I’m trying to learn F#.

I’m using the library Combinatorics in dotnet, which I’ve used in the past, generate in this case every duplicating possibility of the operations. I the only optimization that I did was to use a function to concatenate numbers without converting to strings, but that didn’t actually help much.

I have parser helpers that use ReadOnlySpans over strings to prevent unnecessary allocations. However, here I’m adding to a C# mutable list and then converting to an FSharp (linked) list, which this language is more familiar with. Not optimal, but runtime was pretty good.

I’m not terribly good with F#, but I think I did ok for this challenge.

F#

// in another file:
let concatenateLong (a:Int64) (b:Int64) : Int64 =
    let rec countDigits (n:int64) =
        if n = 0 then 0
        else 1 + countDigits (n / (int64 10))   

    let bDigits = if b = 0 then 1 else countDigits b
    let multiplier = pown 10 bDigits |> int64
    a * multiplier + b

// challenge file
type Operation = {Total:Int64; Inputs:Int64 list }

let parse (s:ReadOnlySpan<char>) : Operation =
    let sep = s.IndexOf(':')
    let total = Int64.Parse(s.Slice(0, sep))
    let inputs = System.Collections.Generic.List<Int64>()
    let right:ReadOnlySpan<char> = s.Slice(sep + 1).Trim()

   // because the Split function on a span returns a SpanSplitEnumerator, which is a ref-struct and can only live on the stack, 
   // I can't use the F# list syntax here
    for range in right.Split(" ") do
        inputs.Add(Int64.Parse(sliceRange right range))
        
    {Total = total; Inputs = List.ofSeq(inputs) }

let part1Ops = [(+); (*)]

let execute ops input =
    input
    |> PSeq.choose (fun op ->
        let total = op.Total
        let inputs = op.Inputs
        let variations = Variations(ops, inputs.Length - 1, GenerateOption.WithRepetition)
        variations
        |> Seq.tryFind (fun v ->
            let calcTotal = (inputs[0], inputs[1..], List.ofSeq(v)) |||> List.fold2 (fun acc n f -> f acc n) 
            calcTotal = total
            )
        |> Option.map(fun _ -> total)
        )
    |> PSeq.fold (fun acc n -> acc + n) 0L

let part1 input =
    (read input parse)
    |> execute part1Ops

let part2Ops = [(+); (*); concatenateLong]

let part2 input = (read input parse) |> execute part2Ops

The Gen0 garbage collection looks absurd, but Gen0 is generally considered “free”.

Method Mean Error StdDev Gen0 Gen1 Allocated
Part1 19.20 ms 0.372 ms 0.545 ms 17843.7500 156.2500 106.55 MB
Part2 17.94 ms 0.355 ms 0.878 ms 17843.7500 156.2500 106.55 MB

V2 - concatenate numbers did little for the runtime, but did help with Gen1 garbage, but not the overall allocation.

Method Mean Error StdDev Gen0 Gen1 Allocated
Part1 17.34 ms 0.342 ms 0.336 ms 17843.7500 125.0000 106.55 MB
Part2 17.24 ms 0.323 ms 0.270 ms 17843.7500 93.7500 106.55 MB
permalink
report
reply

Advent Of Code

!advent_of_code@programming.dev

Create post

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

  • Follow the programming.dev instance rules
  • Keep all content related to advent of code in some way
  • If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2024 Day 10])
  • When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

Community stats

  • 476

    Monthly active users

  • 110

    Posts

  • 1.1K

    Comments