Advertisement
Guest User

AoC15

a guest
Dec 17th, 2022
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.54 KB | Source Code | 0 0
  1. // Class Coord
  2. package helpers;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Objects;
  6.  
  7. public class Coord implements Comparable<Coord> {
  8.     public int x = 0;
  9.     public int y = 0;
  10.  
  11.     public static final Coord UP = new Coord(0,-1);
  12.     public static final Coord DOWN = new Coord(0,1);
  13.     public static final Coord LEFT = new Coord(-1,0);
  14.     public static final Coord RIGHT = new Coord(1,0);
  15.  
  16.     public Coord(int r, int c) {
  17.         x = r;
  18.         y = c;
  19.     }
  20.  
  21.     public Coord(String s) {
  22.         x = Integer.parseInt(s.split(",")[0].trim());
  23.         y = Integer.parseInt(s.split(",")[1].trim());
  24.     }
  25.  
  26.     public Coord() {
  27.         x = 0;
  28.         y = 0;
  29.     }
  30.  
  31.     @Override
  32.     public int hashCode() {
  33.         return Objects.hash(y,x);
  34.     }
  35.  
  36.     @Override
  37.     public boolean equals(Object obj) {
  38.         if (this == obj)
  39.             return true;
  40.         if (obj == null)
  41.             return false;
  42.         if (getClass() != obj.getClass())
  43.             return false;
  44.         Coord other = (Coord) obj;
  45.         if (x != other.x)
  46.             return false;
  47.         if (y != other.y)
  48.             return false;
  49.         return true;
  50.     }
  51.  
  52.     public String toString() {
  53.         return "(" + x + "," + y + ")";
  54.     }
  55.  
  56.     //adds values of o to self
  57.     public void sumSelf(Coord o) {
  58.         x += o.x;
  59.         y += o.y;
  60.     }
  61.  
  62.     //returns a new coordinate that is the result of this + o
  63.     public Coord sum(Coord o) {
  64.         return new Coord(x + o.x, y + o.y);
  65.     }
  66.  
  67.     //returns Manhattan distance to coord o
  68.     public int dist(Coord o) {
  69.         return Math.abs(x - o.x) + Math.abs(y - o.y);
  70.     }
  71.  
  72.     //returns copy of self
  73.     public Coord copy() {
  74.         return new Coord(x,y);
  75.     }
  76.  
  77.     //returns version with x and y swapped
  78.     public Coord invert() {
  79.         return new Coord(y,x);
  80.     }
  81.  
  82.     public ArrayList<Coord> directNeighbors() {
  83.         ArrayList<Coord> list = new ArrayList<Coord>();
  84.         for(int yOff = -1; yOff < 2; yOff++) {
  85.             for(int xOff = -1; xOff < 2; xOff++) {
  86.                 //if not diagonal or self
  87.                 if(xOff == 0 ^ yOff == 0) {
  88.                     list.add(new Coord(x+xOff,y+yOff));
  89.                 }
  90.             }
  91.         }
  92.         return list;
  93.     }
  94.  
  95.     public ArrayList<Coord> allNeighbors() {
  96.         ArrayList<Coord> list = new ArrayList<Coord>();
  97.         for(int yOff = -1; yOff < 2; yOff++) {
  98.             for(int xOff = -1; xOff < 2; xOff++) {
  99.                 //if not self
  100.                 if(!(xOff == 0 && yOff == 0)) {
  101.                     list.add(new Coord(x+xOff,y+yOff));
  102.                 }
  103.             }
  104.         }
  105.         return list;
  106.     }
  107.  
  108.     //orders coordinates in left-to-right, top-to-bottom order
  109.     @Override
  110.     public int compareTo(Coord o) {
  111.         if(this.equals(o))
  112.             return 0;
  113.         else if(o.y > this.y)
  114.             return -1;
  115.         else if(o.y < this.y)
  116.             return 1;
  117.         else
  118.             return (o.x > this.x ? -1 : 1);
  119.     }
  120.  
  121.     public Coord left() {
  122.         return new Coord(y,-x);
  123.     }
  124.  
  125.     public Coord right() {
  126.         return new Coord(-y,x);
  127.     }
  128. }
  129. // Class Range
  130. package helpers;
  131.  
  132. import java.util.Objects;
  133.  
  134. public class Range {
  135.     public int from = 0;
  136.     public int until = 0;
  137.  
  138.     public Range(int fr, int un) {
  139.         from = fr;
  140.         until = un;
  141.     }
  142.  
  143.     public Range(String s) {
  144.         from = Integer.parseInt(s.split(",")[0].trim());
  145.         until = Integer.parseInt(s.split(",")[1].trim());
  146.     }
  147.  
  148.     public Range() {
  149.         from = 0;
  150.         until = 0;
  151.     }
  152.  
  153.     @Override
  154.     public int hashCode() {
  155.         return Objects.hash(from,until);
  156.     }
  157.  
  158.     @Override
  159.     public boolean equals(Object obj) {
  160.         if (this == obj)
  161.             return true;
  162.         if (obj == null)
  163.             return false;
  164.         if (getClass() != obj.getClass())
  165.             return false;
  166.         Range other = (Range) obj;
  167.         if (from != other.from)
  168.             return false;
  169.         if (until != other.until)
  170.             return false;
  171.         return true;
  172.     }
  173.  
  174.     public String toString() {
  175.         return "(" + from + "," + until + ")";
  176.     }
  177.  
  178. }
  179.  
  180. // Main Class
  181. import com.google.gson.Gson;
  182. import helpers.Coord;
  183. import helpers.Range;
  184. import helpers.Tunnel;
  185.  
  186.  
  187. import java.io.IOException;
  188. import java.nio.file.Files;
  189. import java.nio.file.Paths;
  190. import java.util.*;
  191. import java.util.stream.Collectors;
  192. import java.util.stream.IntStream;
  193.  
  194. public class AdventOfCode {
  195.     public static void main(String[] args) {
  196.         ex15();
  197.  
  198.     }
  199.  
  200.     public static void ex15() {
  201.         String[] inhalt;
  202.         HashMap<Coord, Coord> sensorsToBeacons = new HashMap<>();
  203.         Coord beac, sensor;
  204.         int dist = 0;
  205.         int highestX = 0, lowestX = Integer.MAX_VALUE;
  206.         int highestY = 0, lowestY = Integer.MAX_VALUE;
  207.         try {
  208.             inhalt = Files.readString(Paths.get("./files/AoC/AoC15.txt")).split("\n");
  209.         } catch (IOException e) {
  210.             System.out.println("file error");
  211.             return;
  212.         }
  213.         for (String zeile : inhalt) {
  214.             sensor = new Coord(zeile.split(": closest beacon is at ")[0].replace("Sensor at x=", "").replace("y=", ""));
  215.             beac = new Coord(zeile.split(": closest beacon is at ")[1].replace("x=", "").replace("y=", ""));
  216.             sensorsToBeacons.put(sensor, beac);
  217.             lowestX = Math.min(sensor.x, lowestX);
  218.             lowestX = Math.min(beac.x, lowestX);
  219.             lowestY = Math.min(sensor.y, lowestY);
  220.             lowestY = Math.min(beac.y, lowestY);
  221.             highestX = Math.max(sensor.x, highestX);
  222.             highestX = Math.max(beac.x, highestX);
  223.             highestY = Math.max(sensor.y, highestY);
  224.             highestY = Math.max(beac.y, highestY);
  225.             dist = Math.max(sensor.dist(beac), dist);
  226.         }
  227.         System.out.println("ex15: " + ex15_getNumberOfBeaconsNotPossibleForY(2000000, sensorsToBeacons));
  228.  
  229.         long t0 = System.currentTimeMillis();
  230.  
  231.         int minX = 0, maxX = 4_000_000;
  232.  
  233.         HashSet<Range> beaconsCantBeHere;
  234.         for(int i = minX ; i <= maxX ; ++i) {
  235.             beaconsCantBeHere = ex15_getBeaconPossible(i, minX, maxX, sensorsToBeacons);
  236.             if(beaconsCantBeHere.size() > 0) {
  237.                 int untilMin = Integer.MAX_VALUE;
  238.                 for(Range beacons : beaconsCantBeHere) {
  239.                     untilMin = Math.min(beacons.until, untilMin);
  240.                 }
  241.                 System.out.println("ex15_2: y: " + i + " - " + beaconsCantBeHere + " - " + ((long)(untilMin+1) * 4_000_000 + i));
  242.             }
  243.         }
  244.         System.out.println("Time Used in millis: " + (System.currentTimeMillis() - t0));
  245.  
  246.     }
  247.  
  248.     public static int ex15_getNumberOfBeaconsNotPossibleForY(int rowToCheck, HashMap<Coord, Coord> sensorsToBeacons) {
  249.         Coord beac;
  250.         int dist, bss = 0;
  251.         HashSet<Integer> beaconsCantBeHere = new HashSet<>();
  252.         for(Coord sens : sensorsToBeacons.keySet()) {
  253.             beac = sensorsToBeacons.get(sens);
  254.             if(beac.y == rowToCheck) {
  255.                 beaconsCantBeHere.add(beac.x);
  256.             }
  257.             if(sens.y == rowToCheck) {
  258.                 beaconsCantBeHere.add(sens.x);
  259.             }
  260.         }
  261.         bss = beaconsCantBeHere.size();
  262.         for(Coord sens : sensorsToBeacons.keySet()) {
  263.             beac = sensorsToBeacons.get(sens);
  264.             dist = sens.dist(beac);
  265.             int distToRow = Math.abs(sens.y - rowToCheck);
  266.             if(distToRow > dist) {
  267.                 continue;
  268.             }
  269.             for(int i = 0 ; i <= (dist - distToRow) ; ++i) {
  270.                 beaconsCantBeHere.add(sens.x + i);
  271.                 beaconsCantBeHere.add(sens.x - i);
  272.             }
  273.         }
  274.         return beaconsCantBeHere.size() - bss;
  275.     }
  276.  
  277.     public static HashSet<Range> ex15_getBeaconPossible(int rowToCheck, int minX, int maxX, HashMap<Coord, Coord> sensorsToBeacons) {
  278.         Coord beac;
  279.         int dist;
  280.         HashSet<Range> beaconsCantBeHere = new HashSet<>();
  281.         for(Coord sens : sensorsToBeacons.keySet()) {
  282.             beac = sensorsToBeacons.get(sens);
  283.             if(beac.y == rowToCheck && (beac.x <= maxX) && (beac.x >= minX) ) {
  284.                 beaconsCantBeHere.add(new Range(beac.x, beac.x));
  285.             }
  286.             if(sens.y == rowToCheck && (sens.x <= maxX) && (sens.x >= minX) ) {
  287.                 beaconsCantBeHere.add(new Range(sens.x, sens.x));
  288.             }
  289.         }
  290.         int min, max;
  291.         for(Coord sens : sensorsToBeacons.keySet()) {
  292.             beac = sensorsToBeacons.get(sens);
  293.             dist = sens.dist(beac);
  294.             int distToRow = Math.abs(sens.y - rowToCheck);
  295.             if(distToRow > dist) {
  296.                 continue;
  297.             }
  298.             min = Math.max(Math.min(sens.x - (dist - distToRow), maxX), minX);
  299.             max = Math.min(Math.max(sens.x + (dist - distToRow), minX), maxX);
  300.             beaconsCantBeHere.add(new Range(min, max));
  301.             beaconsCantBeHere = ex15_collapseHashSet(beaconsCantBeHere);
  302.         }
  303.         beaconsCantBeHere = ex15_collapseHashSet(beaconsCantBeHere);
  304.         beaconsCantBeHere = ex15_collapseHashSet(beaconsCantBeHere);
  305.         beaconsCantBeHere = ex15_collapseHashSet(beaconsCantBeHere);
  306.         if(beaconsCantBeHere.size() > 1) {
  307.             return beaconsCantBeHere;
  308.         }
  309.         return new HashSet<Range>();
  310.     }
  311.  
  312.     public static HashSet<Range> ex15_collapseHashSet(HashSet<Range> beaconsCantBeHere) {
  313.         HashSet<Range> newBeacons = new HashSet<>();
  314.         HashSet<Range> toRemove = new HashSet<>();
  315.         for(Range beacons : beaconsCantBeHere) {
  316.             newBeacons.add(beacons);
  317.             for (Range beaconsOther : beaconsCantBeHere) {
  318.                 if(beacons == beaconsOther) {
  319.                     continue;
  320.                 }
  321.                 if(beacons.from <= (beaconsOther.until + 1) && (beacons.until + 1) >= beaconsOther.from) {
  322.                     Range newBeacon = new Range(Math.min(beacons.from, beaconsOther.from), Math.max(beacons.until, beaconsOther.until));
  323.                     newBeacons.add(newBeacon);
  324.                     if(!newBeacon.equals(beacons)) {
  325.                         toRemove.add(beacons);
  326.                     }
  327.                     if(!newBeacon.equals(beaconsOther)) {
  328.                         toRemove.add(beaconsOther);
  329.                     }
  330.                 }
  331.             }
  332.         }
  333.         for(Range beacons : toRemove) {
  334.             newBeacons.remove(beacons);
  335.         }
  336.         return newBeacons;
  337.     }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement