Skip to content

Commit 0edfe4a

Browse files
committed
day 11 part 1
1 parent 08154bf commit 0edfe4a

File tree

12 files changed

+583
-17
lines changed

12 files changed

+583
-17
lines changed

day09/main_test.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@ D 1
1515
L 5
1616
R 2`
1717

18-
const testInput2 = `U 1
19-
U 1
20-
U 1
21-
U 1`
22-
23-
const testInput3 = `U 4`
24-
25-
const testInput4 = `U 1
26-
R 1
27-
U 1
28-
R 1
29-
U 1
30-
R 1
31-
`
32-
3318
func Test_main(t *testing.T) {
3419
tests := []struct {
3520
name string
@@ -60,7 +45,7 @@ func TestMoveRope(t *testing.T) {
6045
args: args{
6146
input: strings.NewReader(testInput),
6247
debug: true,
63-
tailSize: 9,
48+
tailSize: 1,
6449
},
6550
wantUniquePositionsVisitedByTail: 13,
6651
wantErr: false,

day10/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ReadAndParse(in io.Reader) (signalStrengths int, err error) {
7878
// every 20th, 60th, 100th, 140th, 180th, and 220th
7979
if cycle == 20 || cycle == 60 || cycle == 100 || cycle == 140 || cycle == 180 || cycle == 220 {
8080
signalStrengths += cycle * x
81-
fmt.Printf(" [signalStrengths: %d * %d = %d]", cycle, x, cycle*x)
81+
fmt.Printf(" [signalStrengths: %d * %d = %d sum: %d]", cycle, x, cycle*x, signalStrengths)
8282
}
8383

8484
/// Sprite position is x

day11.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#! /bin/bash
2+
3+
CHALLENGE=day11
4+
5+
go build -o $(pwd)/bin/$CHALLENGE ./$CHALLENGE/*.go
6+
7+
echo "Tests 1"
8+
cat $CHALLENGE/input_test_01.txt | bin/$CHALLENGE
9+
10+
echo "Solution"
11+
cat $CHALLENGE/input.txt | bin/$CHALLENGE

day11/input.txt

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 57, 58
3+
Operation: new = old * 19
4+
Test: divisible by 7
5+
If true: throw to monkey 2
6+
If false: throw to monkey 3
7+
8+
Monkey 1:
9+
Starting items: 66, 52, 59, 79, 94, 73
10+
Operation: new = old + 1
11+
Test: divisible by 19
12+
If true: throw to monkey 4
13+
If false: throw to monkey 6
14+
15+
Monkey 2:
16+
Starting items: 80
17+
Operation: new = old + 6
18+
Test: divisible by 5
19+
If true: throw to monkey 7
20+
If false: throw to monkey 5
21+
22+
Monkey 3:
23+
Starting items: 82, 81, 68, 66, 71, 83, 75, 97
24+
Operation: new = old + 5
25+
Test: divisible by 11
26+
If true: throw to monkey 5
27+
If false: throw to monkey 2
28+
29+
Monkey 4:
30+
Starting items: 55, 52, 67, 70, 69, 94, 90
31+
Operation: new = old * old
32+
Test: divisible by 17
33+
If true: throw to monkey 0
34+
If false: throw to monkey 3
35+
36+
Monkey 5:
37+
Starting items: 69, 85, 89, 91
38+
Operation: new = old + 7
39+
Test: divisible by 13
40+
If true: throw to monkey 1
41+
If false: throw to monkey 7
42+
43+
Monkey 6:
44+
Starting items: 75, 53, 73, 52, 75
45+
Operation: new = old * 7
46+
Test: divisible by 2
47+
If true: throw to monkey 0
48+
If false: throw to monkey 4
49+
50+
Monkey 7:
51+
Starting items: 94, 60, 79
52+
Operation: new = old + 2
53+
Test: divisible by 3
54+
If true: throw to monkey 1
55+
If false: throw to monkey 6

day11/input_test_01.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Monkey 0:
2+
Starting items: 79, 98
3+
Operation: new = old * 19
4+
Test: divisible by 23
5+
If true: throw to monkey 2
6+
If false: throw to monkey 3
7+
8+
Monkey 1:
9+
Starting items: 54, 65, 75, 74
10+
Operation: new = old + 6
11+
Test: divisible by 19
12+
If true: throw to monkey 2
13+
If false: throw to monkey 0
14+
15+
Monkey 2:
16+
Starting items: 79, 60, 97
17+
Operation: new = old * old
18+
Test: divisible by 13
19+
If true: throw to monkey 1
20+
If false: throw to monkey 3
21+
22+
Monkey 3:
23+
Starting items: 74
24+
Operation: new = old + 3
25+
Test: divisible by 17
26+
If true: throw to monkey 0
27+
If false: throw to monkey 1

day11/main.go

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"log"
8+
"math"
9+
"os"
10+
"sort"
11+
"strings"
12+
13+
"github.com/dwethmar/adventofcode2022/pkg/iterate"
14+
"github.com/dwethmar/adventofcode2022/pkg/number"
15+
)
16+
17+
func main() {
18+
rawBody, err := io.ReadAll(os.Stdin)
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
monkeys, err := CreateMonkeys(io.NopCloser(bytes.NewBuffer(rawBody)))
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
for i := 0; i < 20; i++ {
29+
PlayKeepAway(i+1, monkeys)
30+
}
31+
32+
fmt.Println()
33+
34+
for _, monkey := range monkeys {
35+
fmt.Printf("%s inspected items %d times.\n", monkey.Name, monkey.Inspected())
36+
}
37+
38+
// sort monkeys based on Inspected()
39+
sort.Slice(monkeys, func(i, j int) bool {
40+
return monkeys[i].Inspected() > monkeys[j].Inspected()
41+
})
42+
43+
answer := monkeys[0].Inspected() * monkeys[1].Inspected()
44+
45+
fmt.Printf("Part 1: %s(%d) * %s(%d) = %d\n", monkeys[0].Name, monkeys[0].Inspected(), monkeys[1].Name, monkeys[1].Inspected(), answer)
46+
}
47+
48+
func CreateMonkeys(input io.Reader) ([]*Monkey, error) {
49+
var monkeys []*Monkey
50+
51+
currentMonkey := &Monkey{}
52+
i := 0
53+
54+
err := iterate.Lines(input, func(instruction string) iterate.Step {
55+
if instruction == "" {
56+
return iterate.Continue
57+
}
58+
59+
v := strings.Split(instruction, ":")
60+
61+
switch i {
62+
case 0:
63+
currentMonkey = &Monkey{}
64+
monkeys = append(monkeys, currentMonkey)
65+
66+
currentMonkey.Name = strings.Trim(v[0], " ")
67+
case 1:
68+
currentMonkey.StartingItems = number.GetAllIntsFromString(v[1])
69+
case 2:
70+
currentMonkey.Operation = strings.Trim(v[1], " ")
71+
case 3:
72+
currentMonkey.Test = strings.Trim(v[1], " ")
73+
case 4:
74+
currentMonkey.TestTrue = strings.Trim(v[1], " ")
75+
case 5:
76+
currentMonkey.TestFalse = strings.Trim(v[1], " ")
77+
}
78+
79+
if i == 5 {
80+
i = 0
81+
} else {
82+
i++
83+
}
84+
85+
return iterate.Continue
86+
})
87+
88+
return monkeys, err
89+
}
90+
91+
/**
92+
Monkey 0:
93+
Monkey inspects an item with a worry level of 79.
94+
Worry level is multiplied by 19 to 1501.
95+
Monkey gets bored with item. Worry level is divided by 3 to 500.
96+
Current worry level is not divisible by 23.
97+
Item with worry level 500 is thrown to monkey 3.
98+
Monkey inspects an item with a worry level of 98.
99+
Worry level is multiplied by 19 to 1862.
100+
Monkey gets bored with item. Worry level is divided by 3 to 620.
101+
Current worry level is not divisible by 23.
102+
Item with worry level 620 is thrown to monkey 3.
103+
**/
104+
func PlayKeepAway(round int, monkeys []*Monkey) {
105+
106+
for _, monkey := range monkeys {
107+
fmt.Print(monkey.Name, ":\n")
108+
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)
124+
125+
monkey.Inspect()
126+
}
127+
128+
monkey.StartingItems = []int{}
129+
}
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+
}

0 commit comments

Comments
 (0)