package dev.forstenlechner.aoc; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Day12Part2 { public static void main(String[] args) throws Exception { new Day12Part2().solve(); } private enum Orientation { N(0), E(1), S(2), W(3); int value; Orientation(int value) { this.value = value; } int getValue() { return value; } static Orientation from(int value) { return switch (value) { case 1 -> E; case 2 -> S; case 3 -> W; default -> N; }; } } private enum Instruction { N { @Override Navigation move(Navigation current, int value) { return new Navigation(current.ship(), new WayPoint(current.waypoint().east(), current.waypoint().north() + value)); } }, S { @Override Navigation move(Navigation current, int value) { return new Navigation(current.ship(), new WayPoint(current.waypoint().east(), current.waypoint().north() - value)); } }, E { @Override Navigation move(Navigation current, int value) { return new Navigation(current.ship(), new WayPoint(current.waypoint().east() + value, current.waypoint().north())); } }, W { @Override Navigation move(Navigation current, int value) { return new Navigation(current.ship(), new WayPoint(current.waypoint().east() - value, current.waypoint().north())); } }, L { @Override Navigation move(Navigation current, int value) { WayPoint currentWayPoint = current.waypoint(); WayPoint newWayPoint = switch (value) { case 90 -> new WayPoint(-currentWayPoint.north, currentWayPoint.east); case 180 -> new WayPoint(-currentWayPoint.east, -currentWayPoint.north); case 270 -> new WayPoint(currentWayPoint.north, -currentWayPoint.east); default -> {throw new IllegalArgumentException();} }; return new Navigation(current.ship(), newWayPoint); } }, R { @Override Navigation move(Navigation current, int value) { WayPoint currentWayPoint = current.waypoint(); WayPoint newWayPoint = switch (value) { case 90 -> new WayPoint(currentWayPoint.north, -currentWayPoint.east); case 180 -> new WayPoint(-currentWayPoint.east, -currentWayPoint.north); case 270 -> new WayPoint(-currentWayPoint.north, currentWayPoint.east); default -> {throw new IllegalArgumentException();} }; return new Navigation(current.ship(), newWayPoint); } }, F { @Override Navigation move(Navigation current, int value) { Location shipLocation = current.ship(); WayPoint wayPoint = current.waypoint(); Location newLocation = new Location(shipLocation.east + (wayPoint.east * value), shipLocation.north + (wayPoint.north * value)); return new Navigation(newLocation, wayPoint); } }; abstract Navigation move(Navigation navigation, int value); } private record NavigationInstruction(Instruction ins, int value) {}; private record Location(int east, int north) {}; private record WayPoint(int east, int north) {}; private record Navigation(Location ship, WayPoint waypoint) {}; private void solve() throws Exception { Stream lines = Files.lines(Paths.get("day12/input")); List navigationInstructions = lines .map(l -> new NavigationInstruction(Instruction.valueOf(l.substring(0, 1)), Integer.parseInt(l.substring(1)))) .collect(Collectors.toList()); Navigation starting = new Navigation(new Location(0, 0), new WayPoint(10, 1)); Navigation result = navigationInstructions.stream().reduce(starting, (l, n) -> { return n.ins().move(l, n.value()); }, (l1, l2) -> l1); System.out.println(Math.abs(result.ship().east()) + Math.abs(result.ship().north())); } }