-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart1.java
More file actions
50 lines (37 loc) · 1.21 KB
/
Copy pathPart1.java
File metadata and controls
50 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package y2016.d02;
import common.Files;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Part1 {
private static class NumberPad {
private int[][] pad = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
};
private int posX = 1;
private int posY = 1;
public int read() {
return pad[posY][posX];
}
public void move(char direction) {
switch (direction) {
case 'U': posY = Math.max(0, posY-1); break;
case 'L': posX = Math.max(0, posX-1); break;
case 'R': posX = Math.min(2, posX+1); break;
case 'D': posY = Math.min(2, posY+1); break;
}
}
}
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> instructions = Files.readByLines("src/main/java/y2016/d02/in.txt");
NumberPad pad = new NumberPad();
for (String line: instructions) {
for (int i = 0; i < line.length(); i++) {
pad.move(line.charAt(i));
}
System.out.print(pad.read());
}
System.out.println("");
}
}