-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2.java
More file actions
93 lines (75 loc) · 2.13 KB
/
Copy pathPart2.java
File metadata and controls
93 lines (75 loc) · 2.13 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package y2016.d02;
import common.Files;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Part2 {
private static class NumberPad {
private String[][] pad = {
{"-", "-", "1", "-", "-"},
{"-", "2", "3", "4", "-"},
{"5", "6", "7", "8", "9"},
{"-", "A", "B", "C", "-"},
{"-", "-", "D", "-", "-"},
};
private int posX = 0;
private int posY = 2;
public String read() {
return pad[posY][posX];
}
public void move(char direction) {
switch (direction) {
case 'U': moveUp(); break;
case 'L': moveLeft(); break;
case 'R': moveRight(); break;
case 'D': moveDown(); break;
}
}
public void moveUp() {
if (posY <= 0) {
return;
}
if ("-".equals(pad[posY-1][posX])) {
return;
}
--posY;
}
public void moveDown() {
if (posY >= 4) {
return;
}
if ("-".equals(pad[posY+1][posX])) {
return;
}
++posY;
}
public void moveLeft() {
if (posX <= 0) {
return;
}
if ("-".equals(pad[posY][posX-1])) {
return;
}
--posX;
}
public void moveRight() {
if (posX >= 4) {
return;
}
if ("-".equals(pad[posY][posX+1])) {
return;
}
++posX;
}
}
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("");
}
}