|
| 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