Advertisement
Guest User

Advent Of Code - Year 2024 - Day 15 - Java

a guest
Dec 15th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | Source Code | 0 0
  1. package aoc2024;
  2.  
  3. import ak.CharGrid;
  4. import ak.Fn;
  5. import ak.I1m;
  6. import ak.I2;
  7. import ak.I2m;
  8. import ak.Set;
  9.  
  10. public class D15 {
  11.  
  12.   public static boolean canPushUnit (CharGrid grid, I2 unit, I2 step, Set<I2> seen, boolean p2) {
  13.     if (seen.contains(unit)) { return true; }
  14.     seen.add(unit);
  15.     if (p2) {
  16.       if (grid.check(unit, '[')) {
  17.         if (!canPushUnit(grid, unit.add(I2.right), step, seen, p2)) { return false; }
  18.       }
  19.       else {
  20.         if (!canPushUnit(grid, unit.add(I2.left), step, seen, p2)) { return false; }
  21.       }
  22.     }
  23.     I2 next = unit.add(step);
  24.     if (grid.check(next, '#')) { return false; }
  25.     if (grid.check(next, '.')) { return true; }
  26.     return canPushUnit(grid, next, step, seen, p2);
  27.   }
  28.  
  29.   public static void pushUnit (CharGrid grid, I2 unit, I2 step, Set<I2> seen, boolean p2) {
  30.     if (seen.contains(unit)) { return; }
  31.     seen.add(unit);
  32.     if (p2) {
  33.       if (grid.check(unit, '[')) {
  34.         pushUnit(grid, unit.add(I2.right), step, seen, p2);
  35.       }
  36.       else {
  37.         pushUnit(grid, unit.add(I2.left), step, seen, p2);
  38.       }
  39.     }
  40.     char v = grid.get(unit);
  41.     grid.set(unit, '.');
  42.     I2 next = unit.add(step);
  43.     if (grid.check(next, '#')) { return; }
  44.     if (grid.checkAny(next, '[', ']', 'O')) { pushUnit(grid, next, step, seen, p2); }
  45.     grid.set(next, v);
  46.   }
  47.  
  48.   public static void useRobotCommand (CharGrid grid, I2m robot, char command, boolean p2) {
  49.     I2 step = commandToDir(command);
  50.     I2 previous = robot.close();
  51.     I2 next = previous.add(step);
  52.     if (grid.check(next, '#')) {
  53.       return;
  54.     }
  55.     else if (grid.checkAny(next, '[', ']', 'O')) {
  56.       boolean pushable = canPushUnit(grid, next, step, new Set<>(), p2);
  57.       if (!pushable) { return; }
  58.       pushUnit(grid, next, step, new Set<>(), p2);
  59.     }
  60.     grid.set(previous, '.');
  61.     grid.set(next, '@');
  62.     robot.set(next);
  63.   }
  64.  
  65.   public static I2 commandToDir (char command) {
  66.     return switch (command) {
  67.       case '>' -> I2.right;
  68.       case '<' -> I2.left;
  69.       case 'v' -> I2.up;
  70.       case '^' -> I2.down;
  71.       default -> throw Fn.notExhaustive(command);
  72.     };
  73.   }
  74.  
  75.   public static void main (String[] args) {
  76.     boolean sample = false;
  77.     CharGrid grid0 = Fn.inputLinesTrimmedAsCharGrid(sample ? "d15-sample-grid.txt" : "d15-grid.txt");
  78.     String commands = Fn.inputStripped(sample ? "d15-sample-commands.txt" : "d15-commands.txt");
  79.     {
  80.       CharGrid grid = grid0.copy();
  81.       I2m robot = new I2m();
  82.       grid.scan(robot, (c, g, x, y, v) -> {
  83.         if (v == '@') {
  84.           c.set(x, y);
  85.           return false;
  86.         }
  87.         return true;
  88.       });
  89.       for (int i = 0; i < commands.length(); i++) {
  90.         char command = commands.charAt(i);
  91.         useRobotCommand(grid, robot, command, false);
  92.       }
  93.       I1m p1 = new I1m(0);
  94.       grid.scan(p1, (c, g, x, y, v) -> {
  95.         if (v == 'O') { c.x += 100 * y + x; }
  96.         return true;
  97.       });
  98.       System.out.println("p1: " + p1);
  99.     }
  100.     {
  101.       CharGrid grid = new CharGrid(grid0.rows, grid0.columns * 2);
  102.       I2m robot = new I2m();
  103.       grid0.scan(grid, (c, g, x, y, v) -> {
  104.         switch (v) {
  105.           case '#' -> {
  106.             c.set(x * 2, y, '#');
  107.             c.set(x * 2 + 1, y, '#');
  108.           }
  109.           case '.' -> {
  110.             c.set(x * 2, y, '.');
  111.             c.set(x * 2 + 1, y, '.');
  112.           }
  113.           case 'O' -> {
  114.             c.set(x * 2, y, '[');
  115.             c.set(x * 2 + 1, y, ']');
  116.           }
  117.           case '@' -> {
  118.             c.set(x * 2, y, '@');
  119.             c.set(x * 2 + 1, y, '.');
  120.           }
  121.           default -> throw Fn.notExhaustive(v);
  122.         }
  123.         return true;
  124.       });
  125.       grid.scan(robot, (c, g, x, y, v) -> {
  126.         if (v == '@') {
  127.           c.set(x, y);
  128.           return false;
  129.         }
  130.         return true;
  131.       });
  132.       for (int i = 0; i < commands.length(); i++) {
  133.         char command = commands.charAt(i);
  134.         useRobotCommand(grid, robot, command, true);
  135.       }
  136.       I1m p2 = new I1m(0);
  137.       grid.scan(p2, (c, g, x, y, v) -> {
  138.         if (v == '[') { c.x += 100L * (long) y + (long) x; }
  139.         return true;
  140.       });
  141.       System.out.println("p2: " + p2);
  142.     }
  143.   }
  144.  
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement