Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aoc2024;
- import java.util.List;
- import aoc2024.ak.CharGrid;
- import aoc2024.ak.Fn;
- import aoc2024.ak.I1m;
- import aoc2024.ak.I2;
- import aoc2024.ak.I2m;
- public class D4 {
- public static void main(String... args) {
- CharGrid grid = Fn.inputLinesTrimmedAsCharGrid("d4.txt");
- { // p1
- I1m p1 = new I1m(0);
- List<I2> velocities = List.of(
- new I2(1, 0),
- new I2(-1, 0),
- new I2(0, -1),
- new I2(0, 1),
- new I2(1, 1),
- new I2(1, -1),
- new I2(-1, 1),
- new I2(-1, -1)
- );
- String target = "XMAS";
- // on each grid-slot, send out our probes
- grid.scan(p1, (c, g, x, y, v) -> {
- PROBE:
- for (I2 velocity : velocities) {
- Probe probe = new Probe(x, y, velocity);
- // move the probe, checking for our desired characters
- for (int i = 0; i < target.length(); i++) {
- char t = target.charAt(i);
- if (!g.check(probe.position.x(), probe.position.y(), t)) { continue PROBE; }
- probe.position.add(probe.velocity);
- }
- c.x += 1;
- }
- return true;
- });
- System.out.println("p1: " + p1.x);
- }
- { // p2
- I1m p2 = new I1m(0);
- List<Probe.Spawn> spawns = List.of(
- new Probe.Spawn(new I2(-1, -1), new I2(1, 1)),
- new Probe.Spawn(new I2(-1, 1), new I2(1, -1)),
- new Probe.Spawn(new I2(1, -1), new I2(-1, 1)),
- new Probe.Spawn(new I2(1, 1), new I2(-1, -1))
- );
- String target = "MAS";
- // on each grid-slot, send out our probes
- grid.scan(p2, (c, g, x, y, v) -> {
- int n = 0;
- PROBE:
- for (Probe.Spawn spawn : spawns) {
- Probe probe = new Probe(x + spawn.offset.x(), y + spawn.offset.y(), spawn.velocity);
- // move the probe, checking for our desired characters
- for (int i = 0; i < target.length(); i++) {
- char t = target.charAt(i);
- if (!g.check(probe.position.x(), probe.position.y(), t)) { continue PROBE; }
- probe.position.add(probe.velocity);
- }
- n += 1;
- }
- if (n >= 2) { c.x += 1; }
- return true;
- });
- System.out.println("p2: " + p2.x);
- }
- }
- public static class Probe {
- public final I2m position;
- public final I2 velocity;
- public Probe (int x, int y, I2 velocity) {
- position = new I2m(x, y);
- this.velocity = velocity;
- }
- public static record Spawn (I2 offset, I2 velocity) {}
- }
- /* ~~~~~~~~~~
- * CharGrid
- * ~~~~~~~~~~
- * public <C> void scan (C context, Scanner<C> scanner) {
- * SCAN:
- * for (int y = 0; y < rows; y++) {
- * for (int x = 0; x < columns; x++) {
- * boolean _continue = scanner.visit(context, this, x, y, get(x, y));
- * if (!_continue) { break SCAN; }
- * }
- * }
- * }
- *
- * public boolean check (int x, int y, char v) {
- * if (!withinBounds(x, y)) { return false; }
- * return get(x, y) == v;
- * }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment