Skip to content

Commit c8e108a

Browse files
committed
day 11
1 parent 69614e3 commit c8e108a

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

day11.fsx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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}"

day11.input

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 74, 64, 74, 63, 53
3+
Operation: new = old * 7
4+
Test: divisible by 5
5+
If true: throw to monkey 1
6+
If false: throw to monkey 6
7+
8+
Monkey 1:
9+
Starting items: 69, 99, 95, 62
10+
Operation: new = old * old
11+
Test: divisible by 17
12+
If true: throw to monkey 2
13+
If false: throw to monkey 5
14+
15+
Monkey 2:
16+
Starting items: 59, 81
17+
Operation: new = old + 8
18+
Test: divisible by 7
19+
If true: throw to monkey 4
20+
If false: throw to monkey 3
21+
22+
Monkey 3:
23+
Starting items: 50, 67, 63, 57, 63, 83, 97
24+
Operation: new = old + 4
25+
Test: divisible by 13
26+
If true: throw to monkey 0
27+
If false: throw to monkey 7
28+
29+
Monkey 4:
30+
Starting items: 61, 94, 85, 52, 81, 90, 94, 70
31+
Operation: new = old + 3
32+
Test: divisible by 19
33+
If true: throw to monkey 7
34+
If false: throw to monkey 3
35+
36+
Monkey 5:
37+
Starting items: 69
38+
Operation: new = old + 5
39+
Test: divisible by 3
40+
If true: throw to monkey 4
41+
If false: throw to monkey 2
42+
43+
Monkey 6:
44+
Starting items: 54, 55, 58
45+
Operation: new = old + 7
46+
Test: divisible by 11
47+
If true: throw to monkey 1
48+
If false: throw to monkey 5
49+
50+
Monkey 7:
51+
Starting items: 79, 51, 83, 88, 93, 76
52+
Operation: new = old * 3
53+
Test: divisible by 2
54+
If true: throw to monkey 0
55+
If false: throw to monkey 6

day11.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Monkey 0:
2+
Starting items: 79, 98
3+
Operation: new = old * 19
4+
Test: divisible by 23
5+
If true: throw to monkey 2
6+
If false: throw to monkey 3
7+
8+
Monkey 1:
9+
Starting items: 54, 65, 75, 74
10+
Operation: new = old + 6
11+
Test: divisible by 19
12+
If true: throw to monkey 2
13+
If false: throw to monkey 0
14+
15+
Monkey 2:
16+
Starting items: 79, 60, 97
17+
Operation: new = old * old
18+
Test: divisible by 13
19+
If true: throw to monkey 1
20+
If false: throw to monkey 3
21+
22+
Monkey 3:
23+
Starting items: 74
24+
Operation: new = old + 3
25+
Test: divisible by 17
26+
If true: throw to monkey 0
27+
If false: throw to monkey 1

0 commit comments

Comments
 (0)