Skip to content

Commit e85dfdf

Browse files
committed
2022 day 11 part 1
1 parent 8488742 commit e85dfdf

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
part1()
6+
7+
//: [Next](@next)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 76, 88, 96, 97, 58, 61, 67
3+
Operation: new = old * 19
4+
Test: divisible by 3
5+
If true: throw to monkey 2
6+
If false: throw to monkey 3
7+
8+
Monkey 1:
9+
Starting items: 93, 71, 79, 83, 69, 70, 94, 98
10+
Operation: new = old + 8
11+
Test: divisible by 11
12+
If true: throw to monkey 5
13+
If false: throw to monkey 6
14+
15+
Monkey 2:
16+
Starting items: 50, 74, 67, 92, 61, 76
17+
Operation: new = old * 13
18+
Test: divisible by 19
19+
If true: throw to monkey 3
20+
If false: throw to monkey 1
21+
22+
Monkey 3:
23+
Starting items: 76, 92
24+
Operation: new = old + 6
25+
Test: divisible by 5
26+
If true: throw to monkey 1
27+
If false: throw to monkey 6
28+
29+
Monkey 4:
30+
Starting items: 74, 94, 55, 87, 62
31+
Operation: new = old + 5
32+
Test: divisible by 2
33+
If true: throw to monkey 2
34+
If false: throw to monkey 0
35+
36+
Monkey 5:
37+
Starting items: 59, 62, 53, 62
38+
Operation: new = old * old
39+
Test: divisible by 7
40+
If true: throw to monkey 4
41+
If false: throw to monkey 7
42+
43+
Monkey 6:
44+
Starting items: 62
45+
Operation: new = old + 2
46+
Test: divisible by 17
47+
If true: throw to monkey 5
48+
If false: throw to monkey 7
49+
50+
Monkey 7:
51+
Starting items: 85, 54, 53
52+
Operation: new = old + 3
53+
Test: divisible by 13
54+
If true: throw to monkey 4
55+
If false: throw to monkey 0
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import Foundation
2+
3+
enum Operation {
4+
case plus
5+
case multiply
6+
case square
7+
8+
func apply(worry: Int, constant: Int) -> Int {
9+
switch self {
10+
case .plus:
11+
return worry + constant
12+
case .multiply:
13+
return worry * constant
14+
case .square:
15+
return worry * worry
16+
}
17+
}
18+
}
19+
20+
struct Monkey {
21+
let id: Int
22+
var items: [Int]
23+
let op: Operation
24+
let constant: Int
25+
let divisible: Int
26+
let ifTrue: Int
27+
let ifFalse: Int
28+
var inspections = 0
29+
}
30+
31+
public func part1() -> Int {
32+
var monkeys = [Monkey]()
33+
var id = 0
34+
var items = [Int]()
35+
var op = Operation.plus
36+
var constant = 0
37+
var divisible = 0
38+
var ifTrue = 0
39+
var ifFalse = 0
40+
for line in stringsFromFile() {
41+
if line.hasPrefix("Monkey ") {
42+
id = Int(line.replacingOccurrences(of: ":", with: "").split(separator: " ").last!)!
43+
} else if line.hasPrefix(" Starting items:") {
44+
items = line.replacingOccurrences(of: ", ", with: " ").split(separator: " ").dropFirst(2).compactMap { Int($0)! }
45+
} else if line.hasPrefix(" Operation") {
46+
if line.contains("old * old") {
47+
op = .square
48+
constant = 1
49+
} else {
50+
constant = Int(line.split(separator: " ").last!)!
51+
op = line.contains("old + ") ? .plus : .multiply
52+
}
53+
} else if line.hasPrefix(" Test") {
54+
divisible = Int(line.split(separator: " ").last!)!
55+
} else if line.hasPrefix(" If true") {
56+
ifTrue = Int(line.split(separator: " ").last!)!
57+
} else if line.hasPrefix(" If false") {
58+
ifFalse = Int(line.split(separator: " ").last!)!
59+
monkeys.append(Monkey(id: id, items: items, op: op, constant: constant, divisible: divisible, ifTrue: ifTrue, ifFalse: ifFalse))
60+
}
61+
}
62+
63+
func inspect(monkey id: Int) {
64+
let items = monkeys[id].items
65+
monkeys[id].items = []
66+
for item in items {
67+
var worry = item
68+
worry = monkeys[id].op.apply(worry: worry, constant: monkeys[id].constant)
69+
worry /= 3
70+
if worry % monkeys[id].divisible == 0 {
71+
monkeys[monkeys[id].ifTrue].items.append(worry)
72+
} else {
73+
monkeys[monkeys[id].ifFalse].items.append(worry)
74+
}
75+
monkeys[id].inspections += 1
76+
}
77+
}
78+
79+
for _ in 0..<20 {
80+
for i in 0..<(monkeys.count) {
81+
inspect(monkey: i)
82+
}
83+
}
84+
85+
monkeys.sort { $0.inspections > $1.inspections }
86+
87+
return monkeys[0].inspections * monkeys[1].inspections
88+
}
89+
90+
public func part2() -> Int {
91+
0
92+
}

aoc22.playground/contents.xcplayground

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<page name='Day08'/>
1212
<page name='Day09'/>
1313
<page name='Day10'/>
14+
<page name='Day11'/>
1415
</pages>
1516
</playground>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy