|
| 1 | +package net.leibi.adventofcode2023.day16; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import net.leibi.helpers.InputHelper; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | + |
| 10 | +@Slf4j |
| 11 | +public class Day16 { |
| 12 | + |
| 13 | + public static final int ANSWERDAY1SMALL = 46; |
| 14 | + private static final Map<MovingDecisionKey, MovingDirection> movingDirectionsMap = getMovingDirectionsMap(); |
| 15 | + private static char[][] charMatrixFromInput; |
| 16 | + private static char[][] energizedCharMatrix; |
| 17 | + |
| 18 | + public static long day1(String input) { |
| 19 | + |
| 20 | + charMatrixFromInput = InputHelper.getCharMatrixFromInput(input); |
| 21 | + InputHelper.printCharArray(charMatrixFromInput); |
| 22 | + var width = charMatrixFromInput.length; |
| 23 | + var hight = charMatrixFromInput[0].length; |
| 24 | + |
| 25 | + log.info("The array is {} by {}", width, hight); |
| 26 | + |
| 27 | + energizedCharMatrix = new char[width][hight]; |
| 28 | + |
| 29 | + final var start = new Point(0, 0); |
| 30 | + followBeam(new DirectedPoint(start, MovingDirection.RIGHT)); |
| 31 | + |
| 32 | + int sum = 0; |
| 33 | + for (char[] charMatrix : energizedCharMatrix) { |
| 34 | + for (char element : charMatrix) { |
| 35 | + if (element == '#') sum++; |
| 36 | + } |
| 37 | + } |
| 38 | + InputHelper.printCharArray(energizedCharMatrix); |
| 39 | + log.info("sum: {}", sum); |
| 40 | + |
| 41 | + |
| 42 | + return ANSWERDAY1SMALL; |
| 43 | + } |
| 44 | + |
| 45 | + static Map<MovingDecisionKey, MovingDirection> getMovingDirectionsMap() { |
| 46 | + Map<MovingDecisionKey, MovingDirection> movingDirectionsMap = new HashMap<>(); |
| 47 | + |
| 48 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '|'), MovingDirection.UPANDDOWN); |
| 49 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '|'), MovingDirection.UPANDDOWN); |
| 50 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '|'), MovingDirection.UP); |
| 51 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '|'), MovingDirection.DOWN); |
| 52 | + |
| 53 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '/'), MovingDirection.UP); |
| 54 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '/'), MovingDirection.DOWN); |
| 55 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '/'), MovingDirection.RIGHT); |
| 56 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '/'), MovingDirection.LEFT); |
| 57 | + |
| 58 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '\\'), MovingDirection.DOWN); |
| 59 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '\\'), MovingDirection.UP); |
| 60 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '\\'), MovingDirection.LEFT); |
| 61 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '\\'), MovingDirection.RIGHT); |
| 62 | + |
| 63 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '-'), MovingDirection.RIGHT); |
| 64 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '-'), MovingDirection.LEFT); |
| 65 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '-'), MovingDirection.LEFTANDRIGHT); |
| 66 | + movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '-'), MovingDirection.LEFTANDRIGHT); |
| 67 | + return movingDirectionsMap; |
| 68 | + } |
| 69 | + |
| 70 | + static MovingDirection getMovingDirection(MovingDirection direction, Character device) { |
| 71 | + log.info("Getting moving direction on device {} with direction {}", device, direction); |
| 72 | + return switch (device) { |
| 73 | + case '.' -> direction; |
| 74 | + default -> movingDirectionsMap.get(new MovingDecisionKey(direction, device)); |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + private static void followBeam(DirectedPoint directedPoint) { |
| 79 | + if (directedPoint == null || directedPoint.point == null) return; |
| 80 | + log.info("Following beam: {}", directedPoint); |
| 81 | + var nextMovingDirection = getNewMovingDirection(directedPoint); |
| 82 | + final var nextPoints = directedPoint.point.getNextPoint(nextMovingDirection); |
| 83 | + |
| 84 | + followBeam(nextPoints.p1); |
| 85 | + followBeam(nextPoints.p2); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + private static MovingDirection getNewMovingDirection(DirectedPoint directedPoint) { |
| 90 | + if (directedPoint.point == null) return directedPoint.movingDirection; |
| 91 | + var device = getDevice(directedPoint); |
| 92 | + return getMovingDirection(directedPoint.movingDirection, device); |
| 93 | + } |
| 94 | + |
| 95 | + private static char getDevice(DirectedPoint directedPoint) { |
| 96 | + energizedCharMatrix[directedPoint.point.x][directedPoint.point.y] = '#'; |
| 97 | + return charMatrixFromInput[directedPoint.point.x][directedPoint.point.y]; |
| 98 | + } |
| 99 | + |
| 100 | + enum MovingDirection { |
| 101 | + RIGHT, |
| 102 | + LEFT, |
| 103 | + UP, |
| 104 | + DOWN, |
| 105 | + LEFTANDRIGHT, |
| 106 | + UPANDDOWN |
| 107 | + } |
| 108 | + |
| 109 | + record MovingDecisionKey(MovingDirection movingDirection, Character Device) { |
| 110 | + } |
| 111 | + |
| 112 | + record Point(int x, int y) { |
| 113 | + Point { |
| 114 | + if (x < 0 || y < 0 || x >= charMatrixFromInput.length || y >= charMatrixFromInput[0].length) { |
| 115 | + throw new IllegalArgumentException(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + NextDirections getNextPoint(MovingDirection movingDirection) { |
| 120 | + return switch (movingDirection) { |
| 121 | + case RIGHT -> new NextDirections(new DirectedPoint(x , y+1, MovingDirection.RIGHT), null); |
| 122 | + case LEFT -> new NextDirections(new DirectedPoint(x , y-1, MovingDirection.LEFT), null); |
| 123 | + case UP -> new NextDirections(new DirectedPoint(x-1, y , MovingDirection.UP), null); |
| 124 | + case DOWN -> new NextDirections(new DirectedPoint(x+1, y , MovingDirection.DOWN), null); |
| 125 | + case UPANDDOWN -> |
| 126 | + new NextDirections(new DirectedPoint(x+1, y , MovingDirection.DOWN), new DirectedPoint(x-1, y , MovingDirection.UP)); |
| 127 | + case LEFTANDRIGHT -> |
| 128 | + new NextDirections(new DirectedPoint(x , y+1, MovingDirection.LEFT), new DirectedPoint(x , y+1, MovingDirection.RIGHT)); |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + record NextDirections(DirectedPoint p1, DirectedPoint p2) { |
| 136 | + } |
| 137 | + |
| 138 | + record DirectedPoint(Point point, MovingDirection movingDirection) { |
| 139 | + public DirectedPoint(int x, int y, MovingDirection movingDirection) { |
| 140 | + Point p = null; |
| 141 | + try { |
| 142 | + p = new Point(x, y); |
| 143 | + } catch (IllegalArgumentException e) { |
| 144 | + log.info("Point hitting a wall: {},{}", x,y); |
| 145 | + } |
| 146 | + this(p, movingDirection); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | + |
0 commit comments