Advertisement
VirKato

AoC 2022 Day 11

Dec 11th, 2022 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.28 KB | Source Code | 0 0
  1. package org.advent.of.code.y2022;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.*;
  5.  
  6.  
  7. public class Line11 {
  8.     public static void main(String[] args) {
  9. //        data = test;
  10.         solution();
  11.     }
  12.  
  13.  
  14.     /***
  15.      * Обезьяны по-очереди берут вещи и оценивают их. Первоначальная ценность указана (Starting items).
  16.      * Произведя операцию оценки (Operation) и уменьшив ценность в 3 раза (для первого задания),
  17.      * вещь передаётся одной из указанных обезьян.
  18.      * Выбор обезьяны осуществляется путём вычисления кратности ценности к тестовому числу (Test).
  19.      * Посчитать для каждой обезьяны количество оцененных ею вещей.
  20.      * Перемножить количество проверенных вещей двух наиболее работящих обезьян.
  21.      *
  22.      * Для второго задания ценность проверенной вещи не уменьшается перед передачей другой обезьяне.
  23.      */
  24.     private static void solution() {
  25.         LinkedList<Queue<BigInteger>> things = new LinkedList<>(); // things
  26.         List<String> action = new ArrayList<>(); // + *
  27.         List<String> actValue = new ArrayList<>();
  28.         List<BigInteger> test = new ArrayList<>(); // divisible by
  29.         List<Integer> ifTrue = new ArrayList<>();
  30.         List<Integer> ifFalse = new ArrayList<>();
  31.         List<Long> count = new ArrayList<>();
  32.  
  33.         // prepare
  34.         Scanner in = new Scanner(data);
  35.         while (in.hasNextLine()) {
  36.             String l = in.nextLine();
  37.             if (!l.startsWith("Monkey")) continue;
  38.  
  39.             l = in.nextLine();
  40.             l = l.replaceAll("Starting items: ", "").trim();
  41.             String[] th = l.split(", ");
  42.             things.add(new LinkedList<>());
  43.             for (String s : th) things.getLast().add(new BigInteger(s));
  44.  
  45.             l = in.nextLine();
  46.             l = l.replaceAll("Operation: new = old ", "").trim();
  47.             action.add(Character.toString(l.charAt(0)));
  48.             actValue.add(l.substring(2));
  49.  
  50.             l = in.nextLine();
  51.             l = l.replaceAll("Test: divisible by ", "").trim();
  52.             test.add(new BigInteger(l));
  53.  
  54.             l = in.nextLine();
  55.             l = l.replaceAll("If true: throw to monkey ", "").trim();
  56.             ifTrue.add(Integer.parseInt(l));
  57.  
  58.             l = in.nextLine();
  59.             l = l.replaceAll("If false: throw to monkey ", "").trim();
  60.             ifFalse.add(Integer.parseInt(l));
  61.  
  62.             count.add(0L);
  63.         }
  64.  
  65.         // for task 2
  66.         BigInteger lcm = BigInteger.ONE;
  67.         for (BigInteger t : test) {
  68.             lcm = lcm.multiply(t);
  69.         }
  70.  
  71.         // task
  72.         for (int n = 0; n < 10000; n++) {
  73.             int monkey = 0;
  74.             for (Queue<BigInteger> queue : things) {
  75.                 while (!queue.isEmpty()) {
  76.                     BigInteger warnNew = queue.poll();
  77.                     count.set(monkey, count.get(monkey) + 1);
  78.                     if (action.get(monkey).equals("+")) {
  79.                         if (actValue.get(monkey).charAt(0) == 'o') {
  80.                             warnNew = warnNew.add(warnNew);
  81.                         } else {
  82.                             warnNew = warnNew.add(new BigInteger(actValue.get(monkey)));
  83.                         }
  84.                     } else {
  85.                         if (actValue.get(monkey).charAt(0) == 'o') {
  86.                             warnNew = warnNew.multiply(warnNew);
  87.                         } else {
  88.                             warnNew = warnNew.multiply(new BigInteger(actValue.get(monkey)));
  89.                         }
  90.                     }
  91.  
  92.                     warnNew = warnNew.mod(lcm); // for task 2
  93.  
  94. //                    warnNew /= 3; // for task 1
  95.  
  96.                     if (warnNew.mod(test.get(monkey)).equals(BigInteger.ZERO)) {
  97.                         things.get(ifTrue.get(monkey)).add(warnNew);
  98.                     } else {
  99.                         things.get(ifFalse.get(monkey)).add(warnNew);
  100.                     }
  101.                 }
  102.                 monkey++;
  103.             }
  104.  
  105.             if (((n + 1) == 1) || ((n + 1) == 20) || ((n + 1) % 1000 == 0)) {
  106.                 System.out.println("\nRound:" + (n + 1));
  107.                 int m = 0;
  108.                 for (Queue<BigInteger> queue : things) {
  109.                     long c = count.get(m);
  110.                     System.out.print("Monkey: " + m++ + " [");
  111.                     List<BigInteger> q = new LinkedList<>(queue);
  112.                     for (BigInteger i : q) {
  113.                         System.out.print(i + " ");
  114.                     }
  115.                     System.out.println("] (" + c + ")");
  116.                 }
  117.             }
  118.         }
  119.  
  120.         count.sort(Comparator.reverseOrder());
  121.         System.out.println(new BigInteger(String.valueOf(count.get(0))).multiply(new BigInteger(String.valueOf(count.get(1)))));
  122.     }
  123.  
  124.  
  125.     private static String test = """
  126.            Monkey 0:
  127.              Starting items: 79, 98
  128.              Operation: new = old * 19
  129.              Test: divisible by 23
  130.                If true: throw to monkey 2
  131.                If false: throw to monkey 3
  132.                        
  133.            Monkey 1:
  134.              Starting items: 54, 65, 75, 74
  135.              Operation: new = old + 6
  136.              Test: divisible by 19
  137.                If true: throw to monkey 2
  138.                If false: throw to monkey 0
  139.                        
  140.            Monkey 2:
  141.              Starting items: 79, 60, 97
  142.              Operation: new = old * old
  143.              Test: divisible by 13
  144.                If true: throw to monkey 1
  145.                If false: throw to monkey 3
  146.                        
  147.            Monkey 3:
  148.              Starting items: 74
  149.              Operation: new = old + 3
  150.              Test: divisible by 17
  151.                If true: throw to monkey 0
  152.                If false: throw to monkey 1
  153.            """;
  154.  
  155.  
  156.     private static String data = """
  157.            Monkey 0:
  158.              Starting items: 77, 69, 76, 77, 50, 58
  159.              Operation: new = old * 11
  160.              Test: divisible by 5
  161.                If true: throw to monkey 1
  162.                If false: throw to monkey 5
  163.                        
  164.            Monkey 1:
  165.              Starting items: 75, 70, 82, 83, 96, 64, 62
  166.              Operation: new = old + 8
  167.              Test: divisible by 17
  168.                If true: throw to monkey 5
  169.                If false: throw to monkey 6
  170.                        
  171.            Monkey 2:
  172.              Starting items: 53
  173.              Operation: new = old * 3
  174.              Test: divisible by 2
  175.                If true: throw to monkey 0
  176.                If false: throw to monkey 7
  177.                        
  178.            Monkey 3:
  179.              Starting items: 85, 64, 93, 64, 99
  180.              Operation: new = old + 4
  181.              Test: divisible by 7
  182.                If true: throw to monkey 7
  183.                If false: throw to monkey 2
  184.                        
  185.            Monkey 4:
  186.              Starting items: 61, 92, 71
  187.              Operation: new = old * old
  188.              Test: divisible by 3
  189.                If true: throw to monkey 2
  190.                If false: throw to monkey 3
  191.                        
  192.            Monkey 5:
  193.              Starting items: 79, 73, 50, 90
  194.              Operation: new = old + 2
  195.              Test: divisible by 11
  196.                If true: throw to monkey 4
  197.                If false: throw to monkey 6
  198.                        
  199.            Monkey 6:
  200.              Starting items: 50, 89
  201.              Operation: new = old + 3
  202.              Test: divisible by 13
  203.                If true: throw to monkey 4
  204.                If false: throw to monkey 3
  205.                        
  206.            Monkey 7:
  207.              Starting items: 83, 56, 64, 58, 93, 91, 56, 65
  208.              Operation: new = old + 5
  209.              Test: divisible by 19
  210.                If true: throw to monkey 1
  211.                If false: throw to monkey 0
  212.            """;
  213. }
  214.  
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement