5
5
"fmt"
6
6
"io"
7
7
"log"
8
- "math"
9
8
"os"
10
9
"sort"
11
10
"strings"
@@ -20,13 +19,28 @@ func main() {
20
19
panic (err )
21
20
}
22
21
23
- monkeys , err := CreateMonkeys (io .NopCloser (bytes .NewBuffer (rawBody )))
22
+ monkeys , _ , err := CreateMonkeys (io .NopCloser (bytes .NewBuffer (rawBody )))
24
23
if err != nil {
25
24
log .Fatal (err )
26
25
}
27
26
27
+ part1 (monkeys )
28
+
29
+ monkeys , lcm , err := CreateMonkeys (io .NopCloser (bytes .NewBuffer (rawBody )))
30
+ if err != nil {
31
+ log .Fatal (err )
32
+ }
33
+
34
+ part2 (monkeys , lcm )
35
+ }
36
+
37
+ func part1 (monkeys []* Monkey ) {
38
+ relieve := func (w int ) int {
39
+ return w / 3
40
+ }
41
+
28
42
for i := 0 ; i < 20 ; i ++ {
29
- PlayKeepAway (i + 1 , monkeys )
43
+ PlayKeepAway (monkeys , relieve )
30
44
}
31
45
32
46
fmt .Println ()
@@ -45,11 +59,43 @@ func main() {
45
59
fmt .Printf ("Part 1: %s(%d) * %s(%d) = %d\n " , monkeys [0 ].Name , monkeys [0 ].Inspected (), monkeys [1 ].Name , monkeys [1 ].Inspected (), answer )
46
60
}
47
61
48
- func CreateMonkeys (input io.Reader ) ([]* Monkey , error ) {
62
+ func part2 (monkeys []* Monkey , lcm int ) {
63
+ relieve := func (worryLvl int ) int {
64
+ return worryLvl % lcm
65
+ }
66
+
67
+ for i := 0 ; i < 10000 ; i ++ {
68
+ PlayKeepAway (monkeys , relieve )
69
+
70
+ if i == 0 {
71
+ fmt .Printf ("After %d rounds:\n " , i )
72
+ for _ , monkey := range monkeys {
73
+ fmt .Printf ("%s inspected items %d times.\n " , monkey .Name , monkey .Inspected ())
74
+ }
75
+ fmt .Println ()
76
+ }
77
+ }
78
+
79
+ for _ , monkey := range monkeys {
80
+ fmt .Printf ("%s inspected items %d times.\n " , monkey .Name , monkey .Inspected ())
81
+ }
82
+
83
+ // sort monkeys based on Inspected()
84
+ sort .Slice (monkeys , func (i , j int ) bool {
85
+ return monkeys [i ].Inspected () > monkeys [j ].Inspected ()
86
+ })
87
+
88
+ answer := monkeys [0 ].Inspected () * monkeys [1 ].Inspected ()
89
+
90
+ fmt .Printf ("Part 2: %s(%d) * %s(%d) = %d\n " , monkeys [0 ].Name , monkeys [0 ].Inspected (), monkeys [1 ].Name , monkeys [1 ].Inspected (), answer )
91
+ }
92
+
93
+ func CreateMonkeys (input io.Reader ) ([]* Monkey , int , error ) {
49
94
var monkeys []* Monkey
50
95
51
96
currentMonkey := & Monkey {}
52
97
i := 0
98
+ lcm := 1
53
99
54
100
err := iterate .Lines (input , func (instruction string ) iterate.Step {
55
101
if instruction == "" {
@@ -65,15 +111,16 @@ func CreateMonkeys(input io.Reader) ([]*Monkey, error) {
65
111
66
112
currentMonkey .Name = strings .Trim (v [0 ], " " )
67
113
case 1 :
68
- currentMonkey .StartingItems = number .GetAllIntsFromString (v [1 ])
114
+ currentMonkey .Items = number .GetAllIntsFromString (v [1 ])
69
115
case 2 :
70
116
currentMonkey .Operation = strings .Trim (v [1 ], " " )
71
117
case 3 :
72
- currentMonkey .Test = strings .Trim (v [1 ], " " )
118
+ currentMonkey .Test = number .GetAllIntsFromString (instruction )[0 ]
119
+ lcm *= currentMonkey .Test
73
120
case 4 :
74
- currentMonkey .TestTrue = strings . Trim ( v [ 1 ], " " )
121
+ currentMonkey .TestTrue = number . GetAllIntsFromString ( instruction )[ 0 ]
75
122
case 5 :
76
- currentMonkey .TestFalse = strings . Trim ( v [ 1 ], " " )
123
+ currentMonkey .TestFalse = number . GetAllIntsFromString ( instruction )[ 0 ]
77
124
}
78
125
79
126
if i == 5 {
@@ -85,7 +132,7 @@ func CreateMonkeys(input io.Reader) ([]*Monkey, error) {
85
132
return iterate .Continue
86
133
})
87
134
88
- return monkeys , err
135
+ return monkeys , lcm , err
89
136
}
90
137
91
138
/**
@@ -101,39 +148,18 @@ Monkey 0:
101
148
Current worry level is not divisible by 23.
102
149
Item with worry level 620 is thrown to monkey 3.
103
150
**/
104
- func PlayKeepAway (round int , monkeys []* Monkey ) {
105
-
151
+ func PlayKeepAway (monkeys []* Monkey , relieve func (w int ) int ) {
106
152
for _ , monkey := range monkeys {
107
- fmt .Print (monkey .Name , ":\n " )
153
+ for i , _ := range monkey .Items {
154
+ w := relieve (monkey .DoOperation (i ))
155
+ t := monkey .DoTest (w )
108
156
109
- for i , item := range monkey .StartingItems {
110
- fmt .Printf ("\t Monkey inspects an item with a worry level of %d.\n " , item )
111
-
112
- d , newWorryLevel := monkey .DoOperation (i )
113
- fmt .Printf ("\t \t %s\n " , d )
114
-
115
- newWorryLevel = int (math .Floor (float64 (newWorryLevel ) / 3 ))
116
- fmt .Printf ("\t \t Monkey gets bored with item. Worry level is divided by 3 to %d.\n " , newWorryLevel )
117
-
118
- t , throwToMonkeyIndex := monkey .DoTest (newWorryLevel )
119
- fmt .Printf ("\t \t %s\n " , t )
120
-
121
- fmt .Printf ("\t \t Item with worry level %d is thrown to monkey %d.\n " , newWorryLevel , throwToMonkeyIndex )
122
- receivingMonkey := monkeys [throwToMonkeyIndex ]
123
- receivingMonkey .StartingItems = append (receivingMonkey .StartingItems , newWorryLevel )
157
+ receivingMonkey := monkeys [t ]
158
+ receivingMonkey .Items = append (receivingMonkey .Items , w )
124
159
125
160
monkey .Inspect ()
126
161
}
127
162
128
- monkey .StartingItems = [] int {}
163
+ monkey .Items = nil
129
164
}
130
-
131
- fmt .Println ("" )
132
-
133
- fmt .Printf ("After round %d, the monkeys are holding items with these worry levels:\n " , round )
134
- for _ , monkey := range monkeys {
135
- fmt .Printf ("%s: %d\n " , monkey .Name , monkey .StartingItems )
136
- }
137
-
138
- return
139
165
}
0 commit comments