Skip to content

Commit aad7591

Browse files
committed
[AOC-12] Day 12 code. Typical navigation problem.
1 parent 321cd6f commit aad7591

File tree

3 files changed

+1013
-0
lines changed

3 files changed

+1013
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.nbrevu.advent2020;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
import com.google.common.base.Charsets;
9+
import com.google.common.io.Resources;
10+
11+
public class Advent12_2 {
12+
private final static String IN_FILE="Advent12.txt";
13+
14+
private final static Pattern LINE_PATTERN=Pattern.compile("^([NSEWLRF])(\\d+)");
15+
16+
private static class Waypoint {
17+
public int x;
18+
public int y;
19+
public Waypoint() {
20+
this.x=0;
21+
this.y=0;
22+
}
23+
public void forward(Position p,int amount) {
24+
p.x+=x*amount;
25+
p.y+=y*amount;
26+
}
27+
public void rotateLeft() {
28+
int newX=-y;
29+
int newY=x;
30+
x=newX;
31+
y=newY;
32+
}
33+
public void rotateRight() {
34+
int newX=y;
35+
int newY=-x;
36+
x=newX;
37+
y=newY;
38+
}
39+
}
40+
41+
private static class Position {
42+
public int x;
43+
public int y;
44+
public Position() {
45+
x=0;
46+
y=0;
47+
}
48+
}
49+
50+
private static enum Action {
51+
N {
52+
@Override
53+
public void act(Position pos,Waypoint wp,int amount) {
54+
wp.y+=amount;
55+
}
56+
},
57+
S {
58+
@Override
59+
public void act(Position pos,Waypoint wp,int amount) {
60+
wp.y-=amount;
61+
}
62+
},
63+
E {
64+
@Override
65+
public void act(Position pos,Waypoint wp,int amount) {
66+
wp.x+=amount;
67+
}
68+
},
69+
W {
70+
@Override
71+
public void act(Position pos,Waypoint wp,int amount) {
72+
wp.x-=amount;
73+
}
74+
},
75+
L {
76+
@Override
77+
public void act(Position pos,Waypoint wp,int amount) {
78+
int realAmount=amount/90;
79+
for (int i=0;i<realAmount;++i) wp.rotateLeft();
80+
}
81+
},
82+
R {
83+
@Override
84+
public void act(Position pos,Waypoint wp,int amount) {
85+
int realAmount=amount/90;
86+
for (int i=0;i<realAmount;++i) wp.rotateRight();
87+
}
88+
},
89+
F {
90+
@Override
91+
public void act(Position pos,Waypoint wp,int amount) {
92+
wp.forward(pos,amount);
93+
}
94+
};
95+
public abstract void act(Position pos,Waypoint wp,int amount);
96+
}
97+
98+
public static void main(String[] args) throws IOException {
99+
URL file=Resources.getResource(IN_FILE);
100+
Position p=new Position();
101+
Waypoint wp=new Waypoint();
102+
wp.x=10;
103+
wp.y=1;
104+
for (String line:Resources.readLines(file,Charsets.UTF_8)) {
105+
Matcher matcher=LINE_PATTERN.matcher(line);
106+
if (matcher.matches()) {
107+
Action a=Action.valueOf(matcher.group(1));
108+
int amount=Integer.parseInt(matcher.group(2));
109+
a.act(p,wp,amount);
110+
}
111+
}
112+
int result=Math.abs(p.x)+Math.abs(p.y);
113+
System.out.println(result);
114+
}
115+
}

0 commit comments

Comments
 (0)