Skip to content

Commit faed968

Browse files
committed
Update
1 parent d70fff9 commit faed968

File tree

1 file changed

+54
-63
lines changed

1 file changed

+54
-63
lines changed

src/day11/Main.java

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,72 @@
11
package day11;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45

56
import util.ReadInputHelper;
67

78
public class Main {
8-
public static void main(String[] args) {
9-
ArrayList<String> lines = new ReadInputHelper(11).getLines();
10-
String line = lines.get(0);
11-
City city = new City();
12-
int rslt = 0;
13-
String[] ins = line.split(",");
14-
15-
for (int i = 0; i < ins.length; i++) {
16-
try {
17-
if (i > 0) {
18-
city.getClass().getMethod(ins[i], String.class).invoke(city, ins[i - 1]);
19-
} else {
20-
city.getClass().getMethod(ins[i], String.class).invoke(city, "");
21-
}
22-
} catch (Exception e){
23-
}
24-
25-
System.out.println(city.x + ", " + city.y + ", max: " + city.max());
26-
if (city.max() > rslt)
27-
rslt = city.max();
28-
}
29-
30-
System.out.println(city.x + ", " + city.y);
31-
System.out.println(city.max());
32-
System.out.println(rslt);
33-
}
9+
public static void main(String[] args) {
10+
ArrayList<String> lines = new ReadInputHelper(11).getLines();
11+
12+
String line = lines.get(0);
13+
// String line = "ne,s";
14+
City city = new City();
15+
int rslt = 0;
16+
String[] ins = line.split(",");
17+
18+
for (int i = 0; i < ins.length; i++) {
19+
20+
try {
21+
city.getClass().getMethod(ins[i]).invoke(city);
22+
} catch (Exception e) {
23+
System.out.println(e.getMessage());
24+
}
25+
26+
if (city.max() > rslt)
27+
rslt = city.max();
28+
}
29+
30+
System.out.println(city.x + ", " + city.y + ", " + city.z);
31+
System.out.println(city.max());
32+
System.out.println(rslt);
33+
}
3434
}
3535

3636
class City {
37-
public int x, y = 0;
37+
public int x = 0, y = 0, z = 0;
3838

39-
public void n(String last) {
40-
this.y--;
41-
}
39+
synchronized public void n() {
40+
y++;
41+
z--;
42+
}
4243

43-
public void ne(String last) {
44-
if (!last.equals("nw")) {
45-
this.y--;
46-
}
47-
this.x++;
48-
}
44+
synchronized public void ne() {
45+
x--;
46+
y++;
47+
}
4948

50-
public void se(String last) {
51-
if (!last.equals("sw"))
52-
y++;
53-
this.x++;
54-
}
49+
synchronized public void se() {
50+
x--;
51+
z++;
52+
}
5553

56-
public void s(String last) {
57-
y++;
58-
}
54+
synchronized public void s() {
55+
y--;
56+
z++;
57+
}
5958

60-
public void sw(String last) {
61-
if (!last.equals("se"))
62-
y++;
63-
x--;
64-
}
59+
synchronized public void sw() {
60+
x++;
61+
y--;
62+
}
6563

66-
public void nw(String last) {
67-
if (!last.equals("ne"))
68-
y--;
69-
x--;
70-
}
64+
synchronized public void nw() {
65+
z--;
66+
x++;
67+
}
7168

72-
public int max() {
73-
if ((x < 0 && y < 0) || (x > 0 && y > 0)) {
74-
return Math.abs(x + y);
75-
} else if (Math.abs(x) > Math.abs(y)) {
76-
return Math.abs(x);
77-
} else {
78-
return Math.abs(y);
79-
}
69+
synchronized public int max() {
70+
return (Math.abs(x) + Math.abs(y) + Math.abs(z))/2;
71+
}
8072
}
81-
}

0 commit comments

Comments
 (0)