Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package adventofcode;
- import adventofcode.utils.Utils;
- import java.awt.Point;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- public class Day10 {
- public static void solve() {
- List<String> input = Utils.getInput(10);
- char[][] map = input.stream().map(String::toCharArray).toArray(char[][]::new);
- List<Point> points = new ArrayList<>();
- for(int y = 0 ; y < map.length ; y++) {
- for(int x = 0 ; x < map[y].length ; x++) {
- if(map[y][x] == '#') {
- points.add(new Point(x, y));
- }
- }
- }
- Map<Point, Set<Point>> visibleAsteroids = new HashMap<>();
- for(Point point : points) {
- visibleAsteroids.put(point, new HashSet<>());
- for(Point other : points) {
- if(point == other) {
- continue;
- }
- int dx = other.x - point.x;
- int dy = other.y - point.y;
- int gcd = gcd(dx, dy);
- visibleAsteroids.get(point).add(new Point(dx / gcd, dy / gcd));
- }
- }
- int mostVisible = visibleAsteroids.values().stream()
- .mapToInt(Set::size)
- .max().getAsInt();
- System.out.println(mostVisible);
- }
- private static int gcd(int a, int b) {
- if(a < 0) return gcd(-a, b);
- if(b < 0) return gcd(a, -b);
- if(a == b) return a;
- if(a == 0) return b;
- if(b == 0) return a;
- if((a & 1) == 0 && (b & 1) == 0) return gcd(a >> 1, b >> 1) << 1;
- if((a & 1) == 0) return gcd(a >> 1, b);
- if((b & 1) == 0) return gcd(a, b >> 1);
- if(a > b) return gcd((a - b) >> 1, b);
- return gcd((b - a) >> 1, a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement