Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aoc2024;
- import ak.CharGrid;
- import ak.Fn;
- import ak.I1m;
- import ak.I2;
- import ak.I2m;
- import ak.Set;
- public class D15 {
- public static boolean canPushUnit (CharGrid grid, I2 unit, I2 step, Set<I2> seen, boolean p2) {
- if (seen.contains(unit)) { return true; }
- seen.add(unit);
- if (p2) {
- if (grid.check(unit, '[')) {
- if (!canPushUnit(grid, unit.add(I2.right), step, seen, p2)) { return false; }
- }
- else {
- if (!canPushUnit(grid, unit.add(I2.left), step, seen, p2)) { return false; }
- }
- }
- I2 next = unit.add(step);
- if (grid.check(next, '#')) { return false; }
- if (grid.check(next, '.')) { return true; }
- return canPushUnit(grid, next, step, seen, p2);
- }
- public static void pushUnit (CharGrid grid, I2 unit, I2 step, Set<I2> seen, boolean p2) {
- if (seen.contains(unit)) { return; }
- seen.add(unit);
- if (p2) {
- if (grid.check(unit, '[')) {
- pushUnit(grid, unit.add(I2.right), step, seen, p2);
- }
- else {
- pushUnit(grid, unit.add(I2.left), step, seen, p2);
- }
- }
- char v = grid.get(unit);
- grid.set(unit, '.');
- I2 next = unit.add(step);
- if (grid.check(next, '#')) { return; }
- if (grid.checkAny(next, '[', ']', 'O')) { pushUnit(grid, next, step, seen, p2); }
- grid.set(next, v);
- }
- public static void useRobotCommand (CharGrid grid, I2m robot, char command, boolean p2) {
- I2 step = commandToDir(command);
- I2 previous = robot.close();
- I2 next = previous.add(step);
- if (grid.check(next, '#')) {
- return;
- }
- else if (grid.checkAny(next, '[', ']', 'O')) {
- boolean pushable = canPushUnit(grid, next, step, new Set<>(), p2);
- if (!pushable) { return; }
- pushUnit(grid, next, step, new Set<>(), p2);
- }
- grid.set(previous, '.');
- grid.set(next, '@');
- robot.set(next);
- }
- public static I2 commandToDir (char command) {
- return switch (command) {
- case '>' -> I2.right;
- case '<' -> I2.left;
- case 'v' -> I2.up;
- case '^' -> I2.down;
- default -> throw Fn.notExhaustive(command);
- };
- }
- public static void main (String[] args) {
- boolean sample = false;
- CharGrid grid0 = Fn.inputLinesTrimmedAsCharGrid(sample ? "d15-sample-grid.txt" : "d15-grid.txt");
- String commands = Fn.inputStripped(sample ? "d15-sample-commands.txt" : "d15-commands.txt");
- {
- CharGrid grid = grid0.copy();
- I2m robot = new I2m();
- grid.scan(robot, (c, g, x, y, v) -> {
- if (v == '@') {
- c.set(x, y);
- return false;
- }
- return true;
- });
- for (int i = 0; i < commands.length(); i++) {
- char command = commands.charAt(i);
- useRobotCommand(grid, robot, command, false);
- }
- I1m p1 = new I1m(0);
- grid.scan(p1, (c, g, x, y, v) -> {
- if (v == 'O') { c.x += 100 * y + x; }
- return true;
- });
- System.out.println("p1: " + p1);
- }
- {
- CharGrid grid = new CharGrid(grid0.rows, grid0.columns * 2);
- I2m robot = new I2m();
- grid0.scan(grid, (c, g, x, y, v) -> {
- switch (v) {
- case '#' -> {
- c.set(x * 2, y, '#');
- c.set(x * 2 + 1, y, '#');
- }
- case '.' -> {
- c.set(x * 2, y, '.');
- c.set(x * 2 + 1, y, '.');
- }
- case 'O' -> {
- c.set(x * 2, y, '[');
- c.set(x * 2 + 1, y, ']');
- }
- case '@' -> {
- c.set(x * 2, y, '@');
- c.set(x * 2 + 1, y, '.');
- }
- default -> throw Fn.notExhaustive(v);
- }
- return true;
- });
- grid.scan(robot, (c, g, x, y, v) -> {
- if (v == '@') {
- c.set(x, y);
- return false;
- }
- return true;
- });
- for (int i = 0; i < commands.length(); i++) {
- char command = commands.charAt(i);
- useRobotCommand(grid, robot, command, true);
- }
- I1m p2 = new I1m(0);
- grid.scan(p2, (c, g, x, y, v) -> {
- if (v == '[') { c.x += 100L * (long) y + (long) x; }
- return true;
- });
- System.out.println("p2: " + p2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement