Skip to content

Commit 742a84c

Browse files
committed
Blergh
1 parent 0edfe4a commit 742a84c

File tree

5 files changed

+135
-117
lines changed

5 files changed

+135
-117
lines changed

day11/main.go

+63-37
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
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("\tMonkey 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\tMonkey 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\tItem 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
}

day11/main_test.go

+60-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"io"
5+
"math"
56
"os"
67
"reflect"
78
"testing"
@@ -10,35 +11,35 @@ import (
1011
var testMonkeys = []*Monkey{
1112
{
1213
Name: "Monkey 0",
13-
StartingItems: []int{79, 98},
14+
Items: []int{79, 98},
1415
Operation: "new = old * 19",
15-
Test: "divisible by 23",
16-
TestTrue: "throw to monkey 2",
17-
TestFalse: "throw to monkey 3",
16+
Test: 23,
17+
TestTrue: 2,
18+
TestFalse: 3,
1819
},
1920
{
2021
Name: "Monkey 1",
21-
StartingItems: []int{54, 65, 75, 74},
22+
Items: []int{54, 65, 75, 74},
2223
Operation: "new = old + 6",
23-
Test: "divisible by 19",
24-
TestTrue: "throw to monkey 2",
25-
TestFalse: "throw to monkey 0",
24+
Test: 19,
25+
TestTrue: 2,
26+
TestFalse: 0,
2627
},
2728
{
2829
Name: "Monkey 2",
29-
StartingItems: []int{79, 60, 97},
30+
Items: []int{79, 60, 97},
3031
Operation: "new = old * old",
31-
Test: "divisible by 13",
32-
TestTrue: "throw to monkey 1",
33-
TestFalse: "throw to monkey 3",
32+
Test: 12,
33+
TestTrue: 1,
34+
TestFalse: 3,
3435
},
3536
{
3637
Name: "Monkey 3",
37-
StartingItems: []int{74},
38+
Items: []int{74},
3839
Operation: "new = old + 3",
39-
Test: "divisible by 17",
40-
TestTrue: "throw to monkey 0",
41-
TestFalse: "throw to monkey 1",
40+
Test: 17,
41+
TestTrue: 0,
42+
TestFalse: 1,
4243
},
4344
}
4445

@@ -83,7 +84,7 @@ func TestCreateMonkeys(t *testing.T) {
8384
}
8485
for _, tt := range tests {
8586
t.Run(tt.name, func(t *testing.T) {
86-
got, err := CreateMonkeys(tt.args.input)
87+
got, _, err := CreateMonkeys(tt.args.input)
8788
if (err != nil) != tt.wantErr {
8889
t.Errorf("CreateMonkeys() error = %v, wantErr %v", err, tt.wantErr)
8990
return
@@ -116,7 +117,48 @@ func TestPlayKeepAway(t *testing.T) {
116117
}
117118
for _, tt := range tests {
118119
t.Run(tt.name, func(t *testing.T) {
119-
PlayKeepAway(1, tt.args.monkeys)
120+
PlayKeepAway(tt.args.monkeys, func(worryLvl int) int {
121+
return int(math.Floor(float64(worryLvl) / 3))
122+
})
123+
})
124+
}
125+
}
126+
127+
func Test_part1(t *testing.T) {
128+
type args struct {
129+
monkeys []*Monkey
130+
}
131+
tests := []struct {
132+
name string
133+
args args
134+
}{
135+
// TODO: Add test cases.
136+
}
137+
for _, tt := range tests {
138+
t.Run(tt.name, func(t *testing.T) {
139+
part1(tt.args.monkeys)
140+
})
141+
}
142+
}
143+
144+
func Test_part2(t *testing.T) {
145+
type args struct {
146+
monkeys []*Monkey
147+
}
148+
tests := []struct {
149+
name string
150+
args args
151+
}{
152+
{
153+
name: "example",
154+
args: args{
155+
monkeys: testMonkeys,
156+
},
157+
},
158+
}
159+
for _, tt := range tests {
160+
t.Run(tt.name, func(t *testing.T) {
161+
part2(tt.args.monkeys, 10)
120162
})
121163
}
122164
}

day11/monkey.go

+12-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"strings"
65

76
"github.com/dwethmar/adventofcode2022/pkg/number"
@@ -10,18 +9,17 @@ import (
109
type Monkey struct {
1110
// Name of the monkey
1211
Name string
13-
// StartingItems lists your worry level for each item the monkey is
12+
// Items lists your worry level for each item the monkey is
1413
// currently holding in the order they will be inspected.
15-
StartingItems []int
14+
Items []int
1615

1716
// Operation
18-
// example: new = old * 7
1917
Operation string
2018

2119
// test
22-
Test string
23-
TestTrue string
24-
TestFalse string
20+
Test int
21+
TestTrue int
22+
TestFalse int
2523

2624
// Inspected total number of times each monkey inspects items
2725
inspected int
@@ -30,8 +28,8 @@ type Monkey struct {
3028
// hows how your worry level changes as that monkey inspects an item.
3129
// (An operation like new = old * 5 means that your worry level after
3230
// the monkey inspected the item is five times whatever your worry level was before inspection.)
33-
func (m *Monkey) DoOperation(index int) (string, int) {
34-
item := m.StartingItems[index]
31+
func (m *Monkey) DoOperation(index int) int {
32+
item := m.Items[index]
3533

3634
p := strings.Split(m.Operation, " ")
3735

@@ -53,50 +51,26 @@ func (m *Monkey) DoOperation(index int) (string, int) {
5351
}
5452

5553
result := 0
56-
operatorStr := ""
57-
valueBStr := fmt.Sprint(valueB)
58-
if valueBStr == fmt.Sprint(item) {
59-
valueBStr = "itself"
60-
}
6154

6255
switch op {
6356
case "+":
6457
result = valueA + valueB
65-
operatorStr = "increases"
6658
case "*":
6759
result = valueA * valueB
68-
operatorStr = "is multiplied"
6960
}
7061

71-
return fmt.Sprintf("Worry level %s by %s to %d.", operatorStr, valueBStr, result), result
62+
return result
7263
}
7364

7465
// Test shows how the monkey uses your worry level to decide where
7566
// to throw an item next.
7667
// If true shows what happens with an item if the Test was true.
7768
// If false shows what happens with an item if the Test was false.
78-
func (m *Monkey) DoTest(worryLevel int) (string, int) {
79-
v := float64(number.GetAllIntsFromString(m.Test)[0])
80-
p := float64(worryLevel)
81-
82-
// check if p is divisible by v
83-
isDivisible := p/v == float64(int(p/v))
84-
85-
test := ""
86-
if isDivisible {
87-
test = m.TestTrue
88-
} else {
89-
test = m.TestFalse
69+
func (m *Monkey) DoTest(w int) int {
70+
if w%m.Test == 0 {
71+
return m.TestTrue
9072
}
91-
92-
successStr := ""
93-
if isDivisible {
94-
successStr = "is"
95-
} else {
96-
successStr = "is not"
97-
}
98-
99-
return fmt.Sprintf("Current worry level %s divisible by %d.", successStr, int(v)), number.GetAllIntsFromString(test)[0]
73+
return m.TestFalse
10074
}
10175

10276
func (m *Monkey) Inspect() {

go.mod

-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
module github.com/dwethmar/adventofcode2022
22

33
go 1.19
4-
5-
require github.com/stretchr/testify v1.8.1
6-
7-
require (
8-
github.com/davecgh/go-spew v1.1.1 // indirect
9-
github.com/pmezard/go-difflib v1.0.0 // indirect
10-
gopkg.in/yaml.v3 v3.0.1 // indirect
11-
)

0 commit comments

Comments
 (0)