1
1
package day11 ;
2
2
3
3
import java .util .ArrayList ;
4
+ import java .util .Collections ;
4
5
5
6
import util .ReadInputHelper ;
6
7
7
8
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
+ }
34
34
}
35
35
36
36
class City {
37
- public int x , y = 0 ;
37
+ public int x = 0 , y = 0 , z = 0 ;
38
38
39
- public void n (String last ) {
40
- this .y --;
41
- }
39
+ synchronized public void n () {
40
+ y ++;
41
+ z --;
42
+ }
42
43
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
+ }
49
48
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
+ }
55
53
56
- public void s (String last ) {
57
- y ++;
58
- }
54
+ synchronized public void s () {
55
+ y --;
56
+ z ++;
57
+ }
59
58
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
+ }
65
63
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
+ }
71
68
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
+ }
80
72
}
81
- }
0 commit comments