Skip to content

Commit a8e4906

Browse files
committed
2022 day 11
1 parent 71c175f commit a8e4906

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

2022/go/day11/day11.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"sort"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func main() {
12+
fmt.Printf("part1 : %d\n", monkeyBusiness(20))
13+
fmt.Printf("part2 : %d\n", monkeyBusiness(10000))
14+
}
15+
16+
func monkeyBusiness(rounds int) int {
17+
input, _ := os.ReadFile("../../input/day11.txt")
18+
monkeys := parseMonkeys(string(input))
19+
20+
for i := 0; i < rounds; i++ {
21+
doRound(monkeys, rounds <= 20)
22+
}
23+
24+
inspects := []int{}
25+
for _, m := range monkeys {
26+
inspects = append(inspects, m.inspectCount)
27+
}
28+
sort.Sort(sort.Reverse(sort.IntSlice(inspects)))
29+
return inspects[0] * inspects[1]
30+
}
31+
32+
func doRound(monkeys []*monkey, manageWorry bool) {
33+
commonMultiplier := 1
34+
for _, m := range monkeys {
35+
commonMultiplier *= m.divisible
36+
}
37+
38+
for _, m := range monkeys {
39+
for _, i := range m.items {
40+
m.inspectCount++
41+
newLevel := op(i, m.oper)
42+
if manageWorry {
43+
newLevel = newLevel / 3
44+
}
45+
newLevel = newLevel % commonMultiplier
46+
47+
if newLevel%m.divisible == 0 {
48+
monkeys[m.trueTo].items = append(monkeys[m.trueTo].items, newLevel)
49+
} else {
50+
monkeys[m.falseTo].items = append(monkeys[m.falseTo].items, newLevel)
51+
}
52+
}
53+
m.items = []int{}
54+
}
55+
}
56+
57+
func op(val int, op operation) int {
58+
var v2 int
59+
if op.val == "old" {
60+
v2 = val
61+
} else {
62+
fmt.Sscanf(op.val, "%d", &v2)
63+
}
64+
65+
switch op.op {
66+
case '*':
67+
return val * v2
68+
case '+':
69+
return val + v2
70+
}
71+
panic("error")
72+
}
73+
74+
func parseMonkeys(input string) (monkeys []*monkey) {
75+
monkey := strings.Split(input, "\n\n")
76+
for _, m := range monkey {
77+
monkeys = append(monkeys, parseMonkey(m))
78+
}
79+
return monkeys
80+
}
81+
82+
func parseMonkey(input string) *monkey {
83+
var m monkey
84+
lines := strings.Split(input, "\n")
85+
m.items = parseItems(strings.Split(lines[1], ":")[1])
86+
fmt.Sscanf(lines[2], " Operation: new = old %c %s", &m.oper.op, &m.oper.val)
87+
fmt.Sscanf(lines[3], " Test: divisible by %d", &m.divisible)
88+
fmt.Sscanf(lines[4], " If true: throw to monkey %d", &m.trueTo)
89+
fmt.Sscanf(lines[5], " If false: throw to monkey %d", &m.falseTo)
90+
return &m
91+
}
92+
93+
func parseItems(input string) (items []int) {
94+
parts := strings.Split(input, ", ")
95+
for _, p := range parts {
96+
i, _ := strconv.Atoi(strings.TrimSpace(p))
97+
items = append(items, i)
98+
}
99+
return items
100+
}
101+
102+
type operation struct {
103+
op byte
104+
val string
105+
}
106+
107+
type monkey struct {
108+
items []int
109+
oper operation
110+
divisible int
111+
trueTo, falseTo int
112+
inspectCount int
113+
}

2022/input/day11.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 84, 72, 58, 51
3+
Operation: new = old * 3
4+
Test: divisible by 13
5+
If true: throw to monkey 1
6+
If false: throw to monkey 7
7+
8+
Monkey 1:
9+
Starting items: 88, 58, 58
10+
Operation: new = old + 8
11+
Test: divisible by 2
12+
If true: throw to monkey 7
13+
If false: throw to monkey 5
14+
15+
Monkey 2:
16+
Starting items: 93, 82, 71, 77, 83, 53, 71, 89
17+
Operation: new = old * old
18+
Test: divisible by 7
19+
If true: throw to monkey 3
20+
If false: throw to monkey 4
21+
22+
Monkey 3:
23+
Starting items: 81, 68, 65, 81, 73, 77, 96
24+
Operation: new = old + 2
25+
Test: divisible by 17
26+
If true: throw to monkey 4
27+
If false: throw to monkey 6
28+
29+
Monkey 4:
30+
Starting items: 75, 80, 50, 73, 88
31+
Operation: new = old + 3
32+
Test: divisible by 5
33+
If true: throw to monkey 6
34+
If false: throw to monkey 0
35+
36+
Monkey 5:
37+
Starting items: 59, 72, 99, 87, 91, 81
38+
Operation: new = old * 17
39+
Test: divisible by 11
40+
If true: throw to monkey 2
41+
If false: throw to monkey 3
42+
43+
Monkey 6:
44+
Starting items: 86, 69
45+
Operation: new = old + 6
46+
Test: divisible by 3
47+
If true: throw to monkey 1
48+
If false: throw to monkey 0
49+
50+
Monkey 7:
51+
Starting items: 91
52+
Operation: new = old + 1
53+
Test: divisible by 19
54+
If true: throw to monkey 2
55+
If false: throw to monkey 5

2022/java/Day11.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.nio.file.Files;
2+
import java.nio.file.Path;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
class Day11 {
9+
10+
public static void main(String ... args) throws Exception {
11+
System.out.printf("part1: %d\n", monkeyBusiness(20, true));
12+
System.out.printf("part2: %d\n", monkeyBusiness(10_000, false));
13+
}
14+
15+
static long monkeyBusiness(int rounds, boolean manageLevel) throws Exception {
16+
var monkeys = readMonkeys(Files.readString(Path.of("../input/day11.txt")));
17+
var commonMultiplier = monkeys.stream().mapToInt(m -> m.divisible).reduce(1, (a,b) -> a*b);
18+
long[] inspects = new long[monkeys.size()];
19+
20+
for (int j = 0; j < rounds; j++) {
21+
for (int mi = 0; mi < monkeys.size(); mi++) {
22+
var m = monkeys.get(mi);
23+
for (var i : m.items) {
24+
inspects[mi]++;
25+
var level = m.worryLevel(i) / (manageLevel ? 3 : 1) % commonMultiplier;
26+
var target = level % m.divisible == 0 ? m.trueTo : m.falseTo;
27+
monkeys.get(target).items.add(level);
28+
}
29+
m.items.clear();
30+
}
31+
}
32+
return Arrays.stream(inspects).mapToObj(i -> (Long)i).sorted((a,b) -> b.compareTo(a))
33+
.limit(2).reduce(1l, (a,b) -> a*b);
34+
}
35+
36+
static List<Monkey> readMonkeys(String input) {
37+
return Arrays.stream(input.split("\n\n"))
38+
.map(Monkey::parse).collect(Collectors.toCollection(() -> new ArrayList<>()));
39+
}
40+
41+
record Monkey (List<Long> items, String[] oper, int divisible, int trueTo, int falseTo) {
42+
static Monkey parse(String input) {
43+
var lines = input.split("\n");
44+
var items = Arrays.stream(lines[1].split(":")[1].split(","))
45+
.map(i -> Long.parseLong(i.trim())).collect(Collectors.toCollection(ArrayList::new));
46+
var operation = lines[2].split("= old ")[1].split(" ");
47+
var divisible = Integer.parseInt(lines[3].split("by ")[1]);
48+
var trueTo = Integer.parseInt(lines[4].split("monkey ")[1]);
49+
var falseTo = Integer.parseInt(lines[5].split("monkey ")[1]);
50+
return new Monkey(items, operation, divisible, trueTo, falseTo);
51+
}
52+
53+
long worryLevel(long level) {
54+
var val = oper[1].equals("old") ? level : Integer.parseInt(oper[1]);
55+
return switch (oper[0]) {
56+
case "*" -> level * val;
57+
case "+" -> level + val;
58+
default -> throw new IllegalArgumentException();
59+
};
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)