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.
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"
comments
Not 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.