aoc2024

Advent of Code 2024
Log | Files | Refs | README

day8_pt2.java (3259B)


      1 import java.nio.file.Files;
      2 import java.nio.file.Path;
      3 import java.io.IOException;
      4 import java.io.BufferedReader;
      5 import java.io.StringReader;
      6 import java.util.Arrays;
      7 import java.util.List;
      8 import java.util.HashMap;
      9 import java.util.HashSet;
     10 import java.util.ArrayList;
     11 
     12 public class day8_pt2 {
     13   record Point(int x, int y) {
     14     Vec diff(Point other) {
     15       return new Vec(this.x - other.x, this.y - other.y);
     16     }
     17   }
     18   record Vec(int x, int y) {
     19     Vec mul(int scalar) {
     20       return new Vec(this.x * scalar, this.y * scalar);
     21     }
     22     record DivRes(int scalar, boolean valid){}
     23     // Divide one vector by another
     24     // to return an exact integer result. Otherwise valid=false
     25     DivRes div(Vec other) {
     26       if (other.x == 0 && this.x != 0) {
     27         return new DivRes(0, false);
     28       } else if (other.y == 0 && this.y != 0) {
     29         return new DivRes(0, false);
     30       } else if (other.x == 0) {
     31         return new DivRes(this.y / other.y, this.y % other.y == 0);
     32       } else if (other.y == 0) {
     33         return new DivRes(this.x / other.x, this.x % other.x == 0);
     34       } else {
     35         var my = this.y / other.y;
     36         var mx = this.x / other.x;
     37         return new DivRes(my, my == mx && this.y % other.y == 0 && this.x % other.x == 0);
     38       }
     39     }
     40   }
     41   
     42   
     43   public static void main(String[] args) throws IOException {
     44     var input = Files.readString(Path.of(args[0]));
     45     var grid = new BufferedReader(new StringReader(input))
     46       .lines()
     47       .toList();
     48     // Just gonna do this the naïve way
     49     // First pass get the sets of antennas    
     50     var height = grid.size();
     51     var width = grid.getFirst().length();
     52     var antennas = new HashMap<Character, ArrayList<Point>>();
     53     for (var x = 0; x < width; x++) {
     54       for (var y = 0; y < height; y++) {
     55         var ch = grid.get(y).charAt(x);
     56         if (ch != '.') {
     57           var al = antennas.computeIfAbsent(ch, ignored -> new ArrayList<>());
     58           al.add(new Point(x, y));
     59         }
     60       }
     61     }
     62     //System.out.printf("antennas = %s\n", antennas);
     63     // Second pass calculate the distance to each antenna and check if we're an antinode
     64     var ans = new HashSet<Point>();
     65     var antss = antennas.values();
     66     for (var x = 0; x < width; x++) {
     67       for (var y = 0; y < height; y++) {
     68         var pt = new Point(x, y);
     69         for (var ants: antss) {
     70           if (isAn(ants, pt)) {
     71             ans.add(pt);
     72             break; // no need to test other antenna sets
     73           }
     74         }
     75       }
     76     }
     77   
     78     for (var y = 0; y < height; y++) {
     79       for (var x = 0; x < width; x++) {
     80         if (ans.contains(new Point(x, y))) {
     81           System.out.print('#');
     82         } else {
     83           System.out.print(grid.get(y).charAt(x));
     84         }
     85       }
     86       System.out.print('\n');
     87     }
     88     System.out.printf("day8, pt2 = %d\n", ans.size());
     89   }
     90 
     91   static boolean isAn(List<Point> ants, Point pt) {
     92     for (var i=0; i<ants.size(); i++) {
     93       for (var j=0; j<ants.size(); j++) {
     94         if (i == j) continue;
     95         var ant1 = ants.get(i);
     96         var ant2 = ants.get(j);
     97         var va = ant1.diff(ant2);
     98         var v1 = pt.diff(ant1);
     99         var dr = v1.div(va);
    100         if (dr.valid) {
    101           return true;
    102         }
    103       }
    104     }
    105     return false;
    106   }
    107 }