Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.advent.of.code.y2022;
- import java.math.BigInteger;
- import java.util.*;
- public class Line11 {
- public static void main(String[] args) {
- // data = test;
- solution();
- }
- /***
- * Обезьяны по-очереди берут вещи и оценивают их. Первоначальная ценность указана (Starting items).
- * Произведя операцию оценки (Operation) и уменьшив ценность в 3 раза (для первого задания),
- * вещь передаётся одной из указанных обезьян.
- * Выбор обезьяны осуществляется путём вычисления кратности ценности к тестовому числу (Test).
- * Посчитать для каждой обезьяны количество оцененных ею вещей.
- * Перемножить количество проверенных вещей двух наиболее работящих обезьян.
- *
- * Для второго задания ценность проверенной вещи не уменьшается перед передачей другой обезьяне.
- */
- private static void solution() {
- LinkedList<Queue<BigInteger>> things = new LinkedList<>(); // things
- List<String> action = new ArrayList<>(); // + *
- List<String> actValue = new ArrayList<>();
- List<BigInteger> test = new ArrayList<>(); // divisible by
- List<Integer> ifTrue = new ArrayList<>();
- List<Integer> ifFalse = new ArrayList<>();
- List<Long> count = new ArrayList<>();
- // prepare
- Scanner in = new Scanner(data);
- while (in.hasNextLine()) {
- String l = in.nextLine();
- if (!l.startsWith("Monkey")) continue;
- l = in.nextLine();
- l = l.replaceAll("Starting items: ", "").trim();
- String[] th = l.split(", ");
- things.add(new LinkedList<>());
- for (String s : th) things.getLast().add(new BigInteger(s));
- l = in.nextLine();
- l = l.replaceAll("Operation: new = old ", "").trim();
- action.add(Character.toString(l.charAt(0)));
- actValue.add(l.substring(2));
- l = in.nextLine();
- l = l.replaceAll("Test: divisible by ", "").trim();
- test.add(new BigInteger(l));
- l = in.nextLine();
- l = l.replaceAll("If true: throw to monkey ", "").trim();
- ifTrue.add(Integer.parseInt(l));
- l = in.nextLine();
- l = l.replaceAll("If false: throw to monkey ", "").trim();
- ifFalse.add(Integer.parseInt(l));
- count.add(0L);
- }
- // for task 2
- BigInteger lcm = BigInteger.ONE;
- for (BigInteger t : test) {
- lcm = lcm.multiply(t);
- }
- // task
- for (int n = 0; n < 10000; n++) {
- int monkey = 0;
- for (Queue<BigInteger> queue : things) {
- while (!queue.isEmpty()) {
- BigInteger warnNew = queue.poll();
- count.set(monkey, count.get(monkey) + 1);
- if (action.get(monkey).equals("+")) {
- if (actValue.get(monkey).charAt(0) == 'o') {
- warnNew = warnNew.add(warnNew);
- } else {
- warnNew = warnNew.add(new BigInteger(actValue.get(monkey)));
- }
- } else {
- if (actValue.get(monkey).charAt(0) == 'o') {
- warnNew = warnNew.multiply(warnNew);
- } else {
- warnNew = warnNew.multiply(new BigInteger(actValue.get(monkey)));
- }
- }
- warnNew = warnNew.mod(lcm); // for task 2
- // warnNew /= 3; // for task 1
- if (warnNew.mod(test.get(monkey)).equals(BigInteger.ZERO)) {
- things.get(ifTrue.get(monkey)).add(warnNew);
- } else {
- things.get(ifFalse.get(monkey)).add(warnNew);
- }
- }
- monkey++;
- }
- if (((n + 1) == 1) || ((n + 1) == 20) || ((n + 1) % 1000 == 0)) {
- System.out.println("\nRound:" + (n + 1));
- int m = 0;
- for (Queue<BigInteger> queue : things) {
- long c = count.get(m);
- System.out.print("Monkey: " + m++ + " [");
- List<BigInteger> q = new LinkedList<>(queue);
- for (BigInteger i : q) {
- System.out.print(i + " ");
- }
- System.out.println("] (" + c + ")");
- }
- }
- }
- count.sort(Comparator.reverseOrder());
- System.out.println(new BigInteger(String.valueOf(count.get(0))).multiply(new BigInteger(String.valueOf(count.get(1)))));
- }
- private static String test = """
- Monkey 0:
- Starting items: 79, 98
- Operation: new = old * 19
- Test: divisible by 23
- If true: throw to monkey 2
- If false: throw to monkey 3
- Monkey 1:
- Starting items: 54, 65, 75, 74
- Operation: new = old + 6
- Test: divisible by 19
- If true: throw to monkey 2
- If false: throw to monkey 0
- Monkey 2:
- Starting items: 79, 60, 97
- Operation: new = old * old
- Test: divisible by 13
- If true: throw to monkey 1
- If false: throw to monkey 3
- Monkey 3:
- Starting items: 74
- Operation: new = old + 3
- Test: divisible by 17
- If true: throw to monkey 0
- If false: throw to monkey 1
- """;
- private static String data = """
- Monkey 0:
- Starting items: 77, 69, 76, 77, 50, 58
- Operation: new = old * 11
- Test: divisible by 5
- If true: throw to monkey 1
- If false: throw to monkey 5
- Monkey 1:
- Starting items: 75, 70, 82, 83, 96, 64, 62
- Operation: new = old + 8
- Test: divisible by 17
- If true: throw to monkey 5
- If false: throw to monkey 6
- Monkey 2:
- Starting items: 53
- Operation: new = old * 3
- Test: divisible by 2
- If true: throw to monkey 0
- If false: throw to monkey 7
- Monkey 3:
- Starting items: 85, 64, 93, 64, 99
- Operation: new = old + 4
- Test: divisible by 7
- If true: throw to monkey 7
- If false: throw to monkey 2
- Monkey 4:
- Starting items: 61, 92, 71
- Operation: new = old * old
- Test: divisible by 3
- If true: throw to monkey 2
- If false: throw to monkey 3
- Monkey 5:
- Starting items: 79, 73, 50, 90
- Operation: new = old + 2
- Test: divisible by 11
- If true: throw to monkey 4
- If false: throw to monkey 6
- Monkey 6:
- Starting items: 50, 89
- Operation: new = old + 3
- Test: divisible by 13
- If true: throw to monkey 4
- If false: throw to monkey 3
- Monkey 7:
- Starting items: 83, 56, 64, 58, 93, 91, 56, 65
- Operation: new = old + 5
- Test: divisible by 19
- If true: throw to monkey 1
- If false: throw to monkey 0
- """;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement