Guest User

AoC 2024 Day 4 - Java

a guest
Dec 4th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | Source Code | 0 0
  1. package aoc2024;
  2.  
  3. import java.util.List;
  4.  
  5. import aoc2024.ak.CharGrid;
  6. import aoc2024.ak.Fn;
  7. import aoc2024.ak.I1m;
  8. import aoc2024.ak.I2;
  9. import aoc2024.ak.I2m;
  10.  
  11. public class D4 {
  12.  
  13.   public static void main(String... args) {
  14.     CharGrid grid = Fn.inputLinesTrimmedAsCharGrid("d4.txt");
  15.     { // p1
  16.       I1m p1 = new I1m(0);
  17.       List<I2> velocities = List.of(
  18.         new I2(1, 0),
  19.         new I2(-1, 0),
  20.         new I2(0, -1),
  21.         new I2(0, 1),
  22.         new I2(1, 1),
  23.         new I2(1, -1),
  24.         new I2(-1, 1),
  25.         new I2(-1, -1)
  26.       );
  27.       String target = "XMAS";
  28.       // on each grid-slot, send out our probes
  29.       grid.scan(p1, (c, g, x, y, v) -> {
  30.         PROBE:
  31.         for (I2 velocity : velocities) {
  32.           Probe probe = new Probe(x, y, velocity);
  33.           // move the probe, checking for our desired characters
  34.           for (int i = 0; i < target.length(); i++) {
  35.             char t = target.charAt(i);
  36.             if (!g.check(probe.position.x(), probe.position.y(), t)) { continue PROBE; }
  37.             probe.position.add(probe.velocity);
  38.           }
  39.           c.x += 1;
  40.         }
  41.         return true;
  42.       });
  43.       System.out.println("p1: " + p1.x);
  44.     }
  45.     { // p2
  46.       I1m p2 = new I1m(0);
  47.       List<Probe.Spawn> spawns = List.of(
  48.         new Probe.Spawn(new I2(-1, -1), new I2(1, 1)),
  49.         new Probe.Spawn(new I2(-1, 1), new I2(1, -1)),
  50.         new Probe.Spawn(new I2(1, -1), new I2(-1, 1)),
  51.         new Probe.Spawn(new I2(1, 1), new I2(-1, -1))
  52.       );
  53.       String target = "MAS";
  54.       // on each grid-slot, send out our probes
  55.       grid.scan(p2, (c, g, x, y, v) -> {
  56.         int n = 0;
  57.         PROBE:
  58.         for (Probe.Spawn spawn : spawns) {
  59.           Probe probe = new Probe(x + spawn.offset.x(), y + spawn.offset.y(), spawn.velocity);
  60.           // move the probe, checking for our desired characters
  61.           for (int i = 0; i < target.length(); i++) {
  62.             char t = target.charAt(i);
  63.             if (!g.check(probe.position.x(), probe.position.y(), t)) { continue PROBE; }
  64.             probe.position.add(probe.velocity);
  65.           }
  66.           n += 1;
  67.         }
  68.         if (n >= 2) { c.x += 1; }
  69.         return true;
  70.       });
  71.       System.out.println("p2: " + p2.x);
  72.     }
  73.   }
  74.  
  75.   public static class Probe {
  76.     public final I2m position;
  77.     public final I2 velocity;
  78.  
  79.     public Probe (int x, int y, I2 velocity) {
  80.       position = new I2m(x, y);
  81.       this.velocity = velocity;
  82.     }
  83.  
  84.     public static record Spawn (I2 offset, I2 velocity) {}
  85.   }
  86.  
  87.   /* ~~~~~~~~~~
  88.    *  CharGrid
  89.    * ~~~~~~~~~~
  90.    * public <C> void scan (C context, Scanner<C> scanner) {
  91.    *   SCAN:
  92.    *   for (int y = 0; y < rows; y++) {
  93.    *     for (int x = 0; x < columns; x++) {
  94.    *       boolean _continue = scanner.visit(context, this, x, y, get(x, y));
  95.    *       if (!_continue) { break SCAN; }
  96.    *     }
  97.    *   }
  98.    * }
  99.    *
  100.    * public boolean check (int x, int y, char v) {
  101.    *   if (!withinBounds(x, y)) { return false; }
  102.    *   return get(x, y) == v;
  103.    * }
  104.    */
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment