commit 7968d9cf758aa41788c775127ff3bfd74129de41
parent 8be11ee4c0fabd21c922aeb1037e18a8bd6f2cb0
Author: Martin Ashby <martin@ashbysoft.com>
Date: Mon, 30 Dec 2024 22:58:06 +0000
day8 pt1
Diffstat:
3 files changed, 153 insertions(+), 0 deletions(-)
diff --git a/day8.txt b/day8.txt
@@ -0,0 +1,50 @@
+.....y..........................p................r
+........I.........................................
+......................4.s.........................
+..........4.......................................
+....y.............................................
+......................................p.........r.
+..........0..s......N..................1.....p....
+..y........4.......................p..............
+...............0..................................
+..............0....t....N....h....................
+.............N....................................
+......j...................s............H...l..O...
+..........q.................H................O....
+..f...e.qj.....y...0..............................
+...........t..........................k..Q..r.....
+.........6................Q..s...x......W.........
+....2..b...e....t..4.........c.....xW.j...........
+...e....................w................1.....O..
+..e..j..5...........................c.............
+.........B..2...............MK................H...
+...2......b...g..X...q..........h...............O.
+...q...2..........m....k...i...............QV.x...
+...................i.........W.k.............HQ...
+........b...X...............D..........c...N......
+................................l..........h.....I
+.m...........g......l.......c.............3......V
+....X.......m........g...V.K...7......F.d.........
+.........b.X...U..........................C.......
+.....................l..............o.1....C......
+............u.............K..............3...d....
+......................i.T....f................V...
+..............................1.k.................
+.B.....E......9..m....K..5.M......................
+...P...............M...95....o..i........I........
+...............................S......3......wI...
+.....EP...........9........5..T.R.................
+.P..........v..9......f.............R.Co..w3......
+..........h...SG..v.E...7..f.T....................
+..........6..........L.................Y.......d..
+..........B...............U........D..............
+....B................U.....8..M....n...J..........
+.........................L................Fw......
+....L6E.P.................7.UG....J.....Y.D.......
+........t........v...SJ........n..d...............
+......................8v.....uG...................
+..................L.....n.........................
+...............T..............n......D............
+..............o.........8................J.Y.R....
+..................S...............u....F.......R..
+........6..............u.....7.8..........Y..F....
diff --git a/day8_sample.txt b/day8_sample.txt
@@ -0,0 +1,12 @@
+............
+........0...
+.....0......
+.......0....
+....0.......
+......A.....
+............
+............
+........A...
+.........A..
+............
+............
diff --git a/src/main/java/day8_pt1.java b/src/main/java/day8_pt1.java
@@ -0,0 +1,91 @@
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.StringReader;
+import java.util.Arrays;
+import java.util.List;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.ArrayList;
+
+public class day8_pt1 {
+ record Point(int x, int y) {}
+
+ public static void main(String[] args) throws IOException {
+ var input = Files.readString(Path.of(args[0]));
+ var grid = new BufferedReader(new StringReader(input))
+ .lines()
+ .toList();
+ // Just gonna do this the naïve way
+ // First pass get the sets of antennas
+
+ var height = grid.size();
+ var width = grid.getFirst().length();
+ var antennas = new HashMap<Character, ArrayList<Point>>();
+ for (var x = 0; x < width; x++) {
+ for (var y = 0; y < height; y++) {
+ var ch = grid.get(y).charAt(x);
+ if (ch != '.') {
+ var al = antennas.computeIfAbsent(ch, ignored -> new ArrayList<>());
+ al.add(new Point(x, y));
+ }
+ }
+ }
+ //System.out.printf("antennas = %s\n", antennas);
+ // Second pass calculate the distance to each antenna and check if we're an antinode
+ var ans = new HashSet<Point>();
+ var antss = antennas.values();
+ for (var x = 0; x < width; x++) {
+ for (var y = 0; y < height; y++) {
+ var pt = new Point(x, y);
+ for (var ants: antss) {
+ if (isAn(ants, pt)) {
+ ans.add(pt);
+ break; // no need to test other antenna sets
+ }
+ }
+ }
+ }
+
+ // for (var y = 0; y < height; y++) {
+ // for (var x = 0; x < width; x++) {
+ // if (ans.contains(new Point(x, y))) {
+ // System.out.print('#');
+ // } else {
+ // System.out.print(grid.get(y).charAt(x));
+ // }
+ // }
+ // System.out.print('\n');
+ // }
+ System.out.printf("day8, pt1 = %d\n", ans.size());
+ }
+
+ static boolean isAn(List<Point> ants, Point pt) {
+ for (var i=0; i<ants.size(); i++) {
+ for (var j=0; j<ants.size(); j++) {
+ if (i == j) continue;
+ var ant1 = ants.get(i);
+ var ant2 = ants.get(j);
+ // are we perfectly in line, AND twice the distance?
+ // y distance has to be exactly twice and so does x distance
+ // and signs have to be right (either in same or opposing quadrants)
+ var d1x = ant1.x - pt.x;
+ var d1y = ant1.y - pt.y;
+ var d2x = ant2.x - pt.x;
+ var d2y = ant2.y - pt.y;
+ var s1 = sig(d1x) * sig(d1y);
+ var s2 = sig(d2x) * sig(d2y);
+ if (Math.abs(d1y) == 2 * Math.abs(d2y) &&
+ Math.abs(d1x) == 2 * Math.abs(d2x) &&
+ s1 == s2) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+ static int sig(int x) {
+ return x >= 0 ? 1 : -1;
+ }
+}