this post was submitted on 02 Dec 2024
14 points (100.0% liked)

NotAwfulTech

385 readers
5 users here now

a community for posting cool tech news you don’t want to sneer at

non-awfulness of tech is not required or else we wouldn’t have any posts

founded 1 year ago
MODERATORS
 

copy pasting the rules from last year's thread:

Rules: no spoilers.

The other rules are made up aswe go along.

Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 4 points 2 weeks ago* (last edited 2 weeks ago)

Got stuck forever on 2-2 because of an edge case that only showed up in 7/1000 reports, ended up just brute forcing it, just ran the fitness function after removing one element at a time sequentially.

Then solved 3.x in like minutes because I could be worse at regex, posting code mostly because no-one else posted F# yet.

edited to fix spoiler header formatting

3-2 in F#

"./input.actual"
|> System.IO.File.ReadAllText
|> fun source -> 
    System.Text.RegularExpressions.Regex.Matches(source, @"don't\(\)|do\(\)|mul\((\d+),(\d+)\)")
    |> Seq.fold
        (fun (acc, enabled) m ->
            match m.Value with
            | "don't()" -> acc, false
            | "do()" -> acc, true
            | mul when enabled && mul.StartsWith("mul") ->
                let (x, y) = int m.Groups.[1].Value, int m.Groups.[2].Value
                acc + x * y, enabled
            | _ -> acc, enabled ) 
        (0, true)
    |> fst
|> printfn "The sum of all valid multiplications with respect to do() and don't() is %A"

commentsNot much to say, the regex grabs all relevant strings and the folding function propagates a flag that flips according to do/don't and an accumulator that is increased when a mul() is encountered and parsed.