|
| 1 | +package com.nbrevu.advent2020; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.EnumMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.regex.Matcher; |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +import com.google.common.base.Charsets; |
| 11 | +import com.google.common.io.Resources; |
| 12 | + |
| 13 | +public class Advent12_1 { |
| 14 | + private final static String IN_FILE="Advent12.txt"; |
| 15 | + |
| 16 | + private final static Pattern LINE_PATTERN=Pattern.compile("^([NSEWLRF])(\\d+)"); |
| 17 | + |
| 18 | + private static enum Direction { |
| 19 | + EAST { |
| 20 | + @Override |
| 21 | + public void moveForward(Position p,int amount) { |
| 22 | + p.x+=amount; |
| 23 | + } |
| 24 | + }, |
| 25 | + NORTH { |
| 26 | + @Override |
| 27 | + public void moveForward(Position p,int amount) { |
| 28 | + p.y+=amount; |
| 29 | + } |
| 30 | + }, |
| 31 | + WEST { |
| 32 | + @Override |
| 33 | + public void moveForward(Position p,int amount) { |
| 34 | + p.x-=amount; |
| 35 | + } |
| 36 | + }, |
| 37 | + SOUTH { |
| 38 | + @Override |
| 39 | + public void moveForward(Position p,int amount) { |
| 40 | + p.y-=amount; |
| 41 | + } |
| 42 | + }; |
| 43 | + public abstract void moveForward(Position p,int amount); |
| 44 | + private final static Map<Direction,Direction> LEFT_ROTATIONS=createLeftRotations(); |
| 45 | + private final static Map<Direction,Direction> RIGHT_ROTATIONS=createRightRotations(); |
| 46 | + private static Map<Direction,Direction> createLeftRotations() { |
| 47 | + Map<Direction,Direction> result=new EnumMap<>(Direction.class); |
| 48 | + result.put(EAST,NORTH); |
| 49 | + result.put(NORTH,WEST); |
| 50 | + result.put(WEST,SOUTH); |
| 51 | + result.put(SOUTH,EAST); |
| 52 | + return result; |
| 53 | + } |
| 54 | + private static Map<Direction,Direction> createRightRotations() { |
| 55 | + Map<Direction,Direction> result=new EnumMap<>(Direction.class); |
| 56 | + result.put(EAST,SOUTH); |
| 57 | + result.put(NORTH,EAST); |
| 58 | + result.put(WEST,NORTH); |
| 59 | + result.put(SOUTH,WEST); |
| 60 | + return result; |
| 61 | + } |
| 62 | + public Direction rotateLeft() { |
| 63 | + return LEFT_ROTATIONS.get(this); |
| 64 | + } |
| 65 | + public Direction rotateRight() { |
| 66 | + return RIGHT_ROTATIONS.get(this); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static class Position { |
| 71 | + public Direction dir; |
| 72 | + public int x; |
| 73 | + public int y; |
| 74 | + public Position() { |
| 75 | + dir=Direction.EAST; |
| 76 | + x=0; |
| 77 | + y=0; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static enum Action { |
| 82 | + N { |
| 83 | + @Override |
| 84 | + public void act(Position pos,int amount) { |
| 85 | + Direction.NORTH.moveForward(pos,amount); |
| 86 | + } |
| 87 | + }, |
| 88 | + S { |
| 89 | + @Override |
| 90 | + public void act(Position pos,int amount) { |
| 91 | + Direction.SOUTH.moveForward(pos,amount); |
| 92 | + } |
| 93 | + }, |
| 94 | + E { |
| 95 | + @Override |
| 96 | + public void act(Position pos,int amount) { |
| 97 | + Direction.EAST.moveForward(pos,amount); |
| 98 | + } |
| 99 | + }, |
| 100 | + W { |
| 101 | + @Override |
| 102 | + public void act(Position pos,int amount) { |
| 103 | + Direction.WEST.moveForward(pos,amount); |
| 104 | + } |
| 105 | + }, |
| 106 | + L { |
| 107 | + @Override |
| 108 | + public void act(Position pos,int amount) { |
| 109 | + int realAmount=amount/90; |
| 110 | + for (int i=0;i<realAmount;++i) pos.dir=pos.dir.rotateLeft(); |
| 111 | + } |
| 112 | + }, |
| 113 | + R { |
| 114 | + @Override |
| 115 | + public void act(Position pos,int amount) { |
| 116 | + int realAmount=amount/90; |
| 117 | + for (int i=0;i<realAmount;++i) pos.dir=pos.dir.rotateRight(); |
| 118 | + } |
| 119 | + }, |
| 120 | + F { |
| 121 | + @Override |
| 122 | + public void act(Position pos,int amount) { |
| 123 | + pos.dir.moveForward(pos,amount); |
| 124 | + } |
| 125 | + }; |
| 126 | + public abstract void act(Position pos,int amount); |
| 127 | + } |
| 128 | + |
| 129 | + public static void main(String[] args) throws IOException { |
| 130 | + URL file=Resources.getResource(IN_FILE); |
| 131 | + Position p=new Position(); |
| 132 | + for (String line:Resources.readLines(file,Charsets.UTF_8)) { |
| 133 | + Matcher matcher=LINE_PATTERN.matcher(line); |
| 134 | + if (matcher.matches()) { |
| 135 | + Action a=Action.valueOf(matcher.group(1)); |
| 136 | + int amount=Integer.parseInt(matcher.group(2)); |
| 137 | + a.act(p,amount); |
| 138 | + } |
| 139 | + } |
| 140 | + int result=Math.abs(p.x)+Math.abs(p.y); |
| 141 | + System.out.println(result); |
| 142 | + } |
| 143 | +} |
0 commit comments