advent-of-code-2022

My solutions for AoC 2022
git clone https://git.sr.ht/~jbauer/advent-of-code-2022
Log | Files | Refs | README | LICENSE

commit 843de3f009d6f87036f4e1851ff6edc8a96c04a3
parent 9695f4ac3efce74cd6473751dc766081acd8e8ef
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Fri, 16 Dec 2022 12:17:07 -0500

Attempt Day 11 but it's wrong for some reason and I don't care anymore

Diffstat:
Aday11/input.txt | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday11/main.c | 176+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 231 insertions(+), 0 deletions(-)

diff --git a/day11/input.txt b/day11/input.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 85, 77, 77 + Operation: new = old * 7 + Test: divisible by 19 + If true: throw to monkey 6 + If false: throw to monkey 7 + +Monkey 1: + Starting items: 80, 99 + Operation: new = old * 11 + Test: divisible by 3 + If true: throw to monkey 3 + If false: throw to monkey 5 + +Monkey 2: + Starting items: 74, 60, 74, 63, 86, 92, 80 + Operation: new = old + 8 + Test: divisible by 13 + If true: throw to monkey 0 + If false: throw to monkey 6 + +Monkey 3: + Starting items: 71, 58, 93, 65, 80, 68, 54, 71 + Operation: new = old + 7 + Test: divisible by 7 + If true: throw to monkey 2 + If false: throw to monkey 4 + +Monkey 4: + Starting items: 97, 56, 79, 65, 58 + Operation: new = old + 5 + Test: divisible by 5 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 5: + Starting items: 77 + Operation: new = old + 4 + Test: divisible by 11 + If true: throw to monkey 4 + If false: throw to monkey 3 + +Monkey 6: + Starting items: 99, 90, 84, 50 + Operation: new = old * old + Test: divisible by 17 + If true: throw to monkey 7 + If false: throw to monkey 1 + +Monkey 7: + Starting items: 50, 66, 61, 92, 64, 78 + Operation: new = old + 3 + Test: divisible by 2 + If true: throw to monkey 5 + If false: throw to monkey 1 diff --git a/day11/main.c b/day11/main.c @@ -0,0 +1,176 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <err.h> + +struct monkey { + int items[32]; + int numItems; + char operator; + int operand; + int modulus; + int trueMonkey; + int falseMonkey; + int inspections; +}; + +void +throwItem(struct monkey **monkeys, int from, int to, int itemIndex) +{ + printf("Throwing %d from %d to %d\n", monkeys[from]->items[itemIndex], from, to); + printf("Before:\n Monkey %d: ", from); + for (int i = 0; i < monkeys[from]->numItems; i++) + printf("%d ", monkeys[from]->items[i]); + printf("\n Monkey %d: ", to); + for (int i = 0; i < monkeys[to]->numItems; i++) + printf("%d ", monkeys[to]->items[i]); + printf("\n"); + + monkeys[to]->items[monkeys[to]->numItems++] = monkeys[from]->items[itemIndex]; + for (int i = itemIndex; i < monkeys[from]->numItems; i++) + { + monkeys[from]->items[i] = monkeys[from]->items[i+1]; + } + monkeys[from]->numItems--; + + printf("After:\n Monkey %d: ", from); + for (int i = 0; i < monkeys[from]->numItems; i++) + printf("%d ", monkeys[from]->items[i]); + printf("\n Monkey %d: ", to); + for (int i = 0; i < monkeys[to]->numItems; i++) + printf("%d ", monkeys[to]->items[i]); + printf("\n"); +} + +int +main (void) +{ + struct monkey *monkeys[8]; + for (int i = 0; i < 8; i++) + monkeys[i] = (struct monkey *)malloc(sizeof(struct monkey)); + + int items0[] = {85, 77, 77}; + monkeys[0]->numItems = 3; + for (int i = 0; i < monkeys[0]->numItems; i++) + { + monkeys[0]->items[i] = items0[i]; + } + monkeys[0]->operator = 'm'; + monkeys[0]->operand = 7; + monkeys[0]->modulus = 19; + monkeys[0]->trueMonkey = 6; + monkeys[0]->falseMonkey = 7; + monkeys[0]->inspections = 0; + + int items1[] = {80, 99}; + monkeys[1]->numItems = 2; + for (int i = 0; i < monkeys[1]->numItems; i++) + monkeys[1]->items[i] = items1[i]; + monkeys[1]->operator = 'm'; + monkeys[1]->operand = 11; + monkeys[1]->modulus = 3; + monkeys[1]->trueMonkey = 3; + monkeys[1]->falseMonkey = 5; + monkeys[1]->inspections = 0; + + int items2[] = {74, 60, 74, 63, 86, 92, 80}; + monkeys[2]->numItems = 7; + for (int i = 0; i < monkeys[2]->numItems; i++) + monkeys[2]->items[i] = items2[i]; + monkeys[2]->operator = 'a'; + monkeys[2]->operand = 18; + monkeys[2]->modulus = 13; + monkeys[2]->trueMonkey = 0; + monkeys[2]->falseMonkey = 6; + monkeys[2]->inspections = 0; + + int items3[] = {71, 58, 93, 65, 80, 68, 54, 71}; + monkeys[3]->numItems = 8; + for (int i = 0; i < monkeys[3]->numItems; i++) + monkeys[3]->items[i] = items3[i]; + monkeys[3]->operator = 'a'; + monkeys[3]->operand = 7; + monkeys[3]->modulus = 7; + monkeys[3]->trueMonkey = 2; + monkeys[3]->falseMonkey = 4; + monkeys[3]->inspections = 0; + + int items4[] = {97, 56, 79, 65, 58}; + monkeys[4]->numItems = 5; + for (int i = 0; i < monkeys[4]->numItems; i++) + monkeys[4]->items[i] = items4[i]; + monkeys[4]->operator = 'a'; + monkeys[4]->operand = 5; + monkeys[4]->modulus = 5; + monkeys[4]->trueMonkey = 2; + monkeys[4]->falseMonkey = 0; + monkeys[4]->inspections = 0; + + int items5[] = {77}; + monkeys[5]->numItems = 1; + for (int i = 0; i < monkeys[5]->numItems; i++) + monkeys[5]->items[i] = items5[i]; + monkeys[5]->operator = 'a'; + monkeys[5]->operand = 4; + monkeys[5]->modulus = 11; + monkeys[5]->trueMonkey = 4; + monkeys[5]->falseMonkey = 3; + monkeys[5]->inspections = 0; + + int items6[] = {99, 90, 84, 50}; + monkeys[6]->numItems = 4; + for (int i = 0; i < monkeys[6]->numItems; i++) + monkeys[6]->items[i] = items6[i]; + monkeys[6]->operator = 'm'; + monkeys[6]->operand = 0; + monkeys[6]->modulus = 17; + monkeys[6]->trueMonkey = 7; + monkeys[6]->falseMonkey = 1; + monkeys[6]->inspections = 0; + + int items7[] = {50, 66, 61, 92, 64, 78}; + monkeys[7]->numItems = 6; + for (int i = 0; i < monkeys[7]->numItems; i++) + monkeys[7]->items[i] = items7[i]; + monkeys[7]->operator = 'a'; + monkeys[7]->operand = 3; + monkeys[7]->modulus = 2; + monkeys[7]->trueMonkey = 5; + monkeys[7]->falseMonkey = 1; + monkeys[7]->inspections = 0; + + for(int i = 0; i < 20; i++) + { + printf("Round %d\n", i); + for (int j = 0; j < 8; j++) + { + for(int k = 0; k < monkeys[j]->numItems;) + { + printf("Monkey %d inspecting %d\n", j, monkeys[j]->items[k]); + monkeys[j]->inspections++; + + if (monkeys[j]->operator == 'm' && monkeys[j]->operand == 0) + monkeys[j]->items[k] *= monkeys[j]->items[k]; + else if (monkeys[j]->operator == 'm') + monkeys[j]->items[k] *= monkeys[j]->operand; + else + monkeys[j]->items[k] += monkeys[j]->operand; + + printf("Dividing %d by 3 = ", monkeys[j]->items[k]); + monkeys[j]->items[k] /= 3; + printf("%d\n", monkeys[j]->items[k]); + + if (monkeys[j]->items[k] % monkeys[j]->modulus == 0) + throwItem(monkeys, j, monkeys[j]->trueMonkey, k); + else + throwItem(monkeys, j, monkeys[j]->falseMonkey, k); + } + } + } + + for (int i = 0; i < 8; i++) + printf("Monkey %d inspected items %d times.\n", + i, monkeys[i]->inspections); + + exit(EXIT_SUCCESS); +}