|
| 1 | +let maxWorry = 2L * 3L * 5L * 7L * 11L * 13L * 17L * 19L * 23L // hardcoded by max prime in inputs |
| 2 | +let lines = System.IO.File.ReadAllLines("day11.input") |
| 3 | +let splittedLines = lines |> Array.chunkBySize 7 |
| 4 | + |
| 5 | +open System.Text.RegularExpressions |
| 6 | + |
| 7 | +let (|Match|_|) (pat: string) (inp: string) = |
| 8 | + let m = Regex.Match(inp, pat) in |
| 9 | + |
| 10 | + if m.Success then |
| 11 | + Some(List.tail [ for g in m.Groups -> g.Value ]) |
| 12 | + else |
| 13 | + None |
| 14 | + |
| 15 | +type Monkey = |
| 16 | + { Items: int64 list |
| 17 | + Operation: int64 -> int64 |
| 18 | + Test: int64 -> int // itemWorry -> monkeyNumber |
| 19 | + InspectionCount: int } |
| 20 | + |
| 21 | +let readMonkey relief (arr: string[]) = |
| 22 | + let items = |
| 23 | + match arr[1] with |
| 24 | + | Match ".*: (.*)" [ s ] -> s.Split ", " |> Seq.map int64 |> Seq.toList |
| 25 | + |
| 26 | + let operation = |
| 27 | + match arr[2] with |
| 28 | + | Match ".* old \* old" [] -> fun x -> x * x |
| 29 | + | Match ".* old \* ([0-9]*)" [ s ] -> let y = int64 s in fun x -> x * y |
| 30 | + | Match ".* old \+ ([0-9]*)" [ s ] -> let y = int64 s in fun x -> x + y |
| 31 | + |> fun op -> |
| 32 | + if relief then |
| 33 | + fun x -> op x / 3L |
| 34 | + else |
| 35 | + fun x -> op x % maxWorry |
| 36 | + |
| 37 | + let test = |
| 38 | + let condition = |
| 39 | + match arr[3] with |
| 40 | + | Match ".* divisible by ([0-9]*)" [ s ] -> let d = int64 s in fun x -> x % d = 0L |
| 41 | + |
| 42 | + let trueMonkey = |
| 43 | + match arr[4] with |
| 44 | + | Match ".* If true: throw to monkey ([0-9]*)" [ s ] -> int s |
| 45 | + |
| 46 | + let falseMonkey = |
| 47 | + match arr[5] with |
| 48 | + | Match ".* If false: throw to monkey ([0-9]*)" [ s ] -> int s |
| 49 | + |
| 50 | + fun x -> if condition x then trueMonkey else falseMonkey |
| 51 | + |
| 52 | + { Items = items |
| 53 | + Operation = operation |
| 54 | + Test = test |
| 55 | + InspectionCount = 0 } |
| 56 | + |
| 57 | +let monkeyBusiness relief rounds = |
| 58 | + let monkeys = |
| 59 | + splittedLines |> Array.map (readMonkey relief) |> Seq.indexed |> Map.ofSeq |
| 60 | + |
| 61 | + let doRound monkeys = |
| 62 | + (monkeys, Map.keys monkeys |> Seq.sort) |
| 63 | + ||> Seq.fold (fun s i -> |
| 64 | + let monkey = Map.find i s |
| 65 | + |
| 66 | + let itemChanges = |
| 67 | + monkey.Items |
| 68 | + |> List.map (fun w -> |
| 69 | + let newWorry = monkey.Operation w |
| 70 | + let newMonkeyNumber = monkey.Test newWorry |
| 71 | + newMonkeyNumber, newWorry) |
| 72 | + |
| 73 | + (s, itemChanges) |
| 74 | + ||> Seq.fold (fun s (m, w) -> s |> Map.add m { s[m] with Items = s[m].Items @ [ w ] }) |
| 75 | + |> Map.add |
| 76 | + i |
| 77 | + { monkey with |
| 78 | + InspectionCount = monkey.InspectionCount + List.length monkey.Items |
| 79 | + Items = [] }) |
| 80 | + |
| 81 | + let allRounds = (monkeys, [ 1..rounds ]) ||> List.scan (fun s _ -> doRound s) |
| 82 | + |
| 83 | + let inspections = |
| 84 | + allRounds |> Seq.last |> Map.map (fun _ x -> x.InspectionCount) |> Map.toSeq |
| 85 | + |
| 86 | + inspections |
| 87 | + |> Seq.map snd |
| 88 | + |> Seq.sortDescending |
| 89 | + |> Seq.take 2 |
| 90 | + |> Seq.toArray |
| 91 | + |> fun xs -> int64 xs[0] * int64 xs[1] |
| 92 | + |
| 93 | +let part1 = monkeyBusiness true 20 |
| 94 | + |
| 95 | +printfn $"PART1: {part1}" |
| 96 | + |
| 97 | +let part2 = monkeyBusiness false 10000 |
| 98 | + |
| 99 | +printfn $"PART2: {part2}" |
0 commit comments