|
| 1 | +import java.util.*; |
| 2 | +import java.util.function.Predicate; |
| 3 | + |
| 4 | +public class Prism { |
| 5 | + |
| 6 | + public record LaserInfo(double x, double y, double angle, Integer prismId) { |
| 7 | + public LaserInfo(double x, double y, double angle) { |
| 8 | + this(x, y, angle, null); |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | + public record PrismInfo(int id, double x, double y, double angle) { |
| 13 | + } |
| 14 | + |
| 15 | + private static final int DECIMAL_PLACES = 3; |
| 16 | + |
| 17 | + private static final double ROUND_FACTOR = Math.pow(10, DECIMAL_PLACES); |
| 18 | + |
| 19 | + public static List<Integer> findSequence(LaserInfo laser, List<PrismInfo> prisms) { |
| 20 | + LaserInfo last = laser; |
| 21 | + Optional<PrismInfo> lastPrism = Optional.empty(); |
| 22 | + List<Integer> sequence = new ArrayList<>(); |
| 23 | + |
| 24 | + do { |
| 25 | + lastPrism = prisms.stream().filter(new TouchesPrism(last)).min(new CompareDistance(last)); |
| 26 | + if (lastPrism.isPresent()) { |
| 27 | + PrismInfo nextPrism = lastPrism.get(); |
| 28 | + sequence.add(nextPrism.id); |
| 29 | + last = new LaserInfo(nextPrism.x, nextPrism.y, |
| 30 | + normalizeDegrees(nextPrism.angle + last.angle), nextPrism.id); |
| 31 | + } |
| 32 | + } while (lastPrism.isPresent()); |
| 33 | + return sequence; |
| 34 | + } |
| 35 | + |
| 36 | + private static double normalizeDegrees(double degrees) { |
| 37 | + if (degrees < 0) { |
| 38 | + return (degrees % 360) + 360; |
| 39 | + } |
| 40 | + return degrees % 360; |
| 41 | + } |
| 42 | + |
| 43 | + private static class CompareDistance implements Comparator<PrismInfo> { |
| 44 | + private final LaserInfo laser; |
| 45 | + |
| 46 | + public CompareDistance(LaserInfo laser) { |
| 47 | + this.laser = laser; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int compare(PrismInfo o1, PrismInfo o2) { |
| 52 | + final double d1 = Math.hypot(o1.x - laser.x, o1.y - laser.y); |
| 53 | + final double d2 = Math.hypot(o2.x - laser.x, o2.y - laser.y); |
| 54 | + return Double.compare(d1, d2); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static class TouchesPrism implements Predicate<PrismInfo> { |
| 59 | + private final LaserInfo laser; |
| 60 | + private final double sinAngle; |
| 61 | + private final double cosAngle; |
| 62 | + |
| 63 | + public TouchesPrism(LaserInfo laser) { |
| 64 | + this.laser = laser; |
| 65 | + |
| 66 | + double angleRadians = Math.toRadians(laser.angle); |
| 67 | + this.sinAngle = Math.sin(angleRadians); |
| 68 | + this.cosAngle = Math.cos(angleRadians); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean test(PrismInfo prism) { |
| 73 | + if (laser.prismId != null && laser.prismId == prism.id) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + double dx = prism.x - laser.x; |
| 78 | + double dy = prism.y - laser.y; |
| 79 | + double hyp = Math.hypot(dx, dy); |
| 80 | + |
| 81 | + return isClose(hyp * cosAngle, dx) && isClose(hyp * sinAngle, dy); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static boolean isClose(double a, double b) { |
| 86 | + return Math.abs(Math.round(a * ROUND_FACTOR - b * ROUND_FACTOR)) <= 1; |
| 87 | + } |
| 88 | +} |
0 commit comments