55 "fmt"
66 "io"
77 "log"
8- "math"
98 "os"
109 "sort"
1110 "strings"
@@ -20,13 +19,28 @@ func main() {
2019 panic (err )
2120 }
2221
23- monkeys , err := CreateMonkeys (io .NopCloser (bytes .NewBuffer (rawBody )))
22+ monkeys , _ , err := CreateMonkeys (io .NopCloser (bytes .NewBuffer (rawBody )))
2423 if err != nil {
2524 log .Fatal (err )
2625 }
2726
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+
2842 for i := 0 ; i < 20 ; i ++ {
29- PlayKeepAway (i + 1 , monkeys )
43+ PlayKeepAway (monkeys , relieve )
3044 }
3145
3246 fmt .Println ()
@@ -45,11 +59,43 @@ func main() {
4559 fmt .Printf ("Part 1: %s(%d) * %s(%d) = %d\n " , monkeys [0 ].Name , monkeys [0 ].Inspected (), monkeys [1 ].Name , monkeys [1 ].Inspected (), answer )
4660}
4761
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 ) {
4994 var monkeys []* Monkey
5095
5196 currentMonkey := & Monkey {}
5297 i := 0
98+ lcm := 1
5399
54100 err := iterate .Lines (input , func (instruction string ) iterate.Step {
55101 if instruction == "" {
@@ -65,15 +111,16 @@ func CreateMonkeys(input io.Reader) ([]*Monkey, error) {
65111
66112 currentMonkey .Name = strings .Trim (v [0 ], " " )
67113 case 1 :
68- currentMonkey .StartingItems = number .GetAllIntsFromString (v [1 ])
114+ currentMonkey .Items = number .GetAllIntsFromString (v [1 ])
69115 case 2 :
70116 currentMonkey .Operation = strings .Trim (v [1 ], " " )
71117 case 3 :
72- currentMonkey .Test = strings .Trim (v [1 ], " " )
118+ currentMonkey .Test = number .GetAllIntsFromString (instruction )[0 ]
119+ lcm *= currentMonkey .Test
73120 case 4 :
74- currentMonkey .TestTrue = strings . Trim ( v [ 1 ], " " )
121+ currentMonkey .TestTrue = number . GetAllIntsFromString ( instruction )[ 0 ]
75122 case 5 :
76- currentMonkey .TestFalse = strings . Trim ( v [ 1 ], " " )
123+ currentMonkey .TestFalse = number . GetAllIntsFromString ( instruction )[ 0 ]
77124 }
78125
79126 if i == 5 {
@@ -85,7 +132,7 @@ func CreateMonkeys(input io.Reader) ([]*Monkey, error) {
85132 return iterate .Continue
86133 })
87134
88- return monkeys , err
135+ return monkeys , lcm , err
89136}
90137
91138/**
@@ -101,39 +148,18 @@ Monkey 0:
101148 Current worry level is not divisible by 23.
102149 Item with worry level 620 is thrown to monkey 3.
103150**/
104- func PlayKeepAway (round int , monkeys []* Monkey ) {
105-
151+ func PlayKeepAway (monkeys []* Monkey , relieve func (w int ) int ) {
106152 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 )
108156
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 )
124159
125160 monkey .Inspect ()
126161 }
127162
128- monkey .StartingItems = [] int {}
163+ monkey .Items = nil
129164 }
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
139165}
0 commit comments