Repository navigation
Expand file tree
/
Copy pathDayFour.java
More file actions
148 lines (130 loc) · 5.3 KB
/
Copy pathDayFour.java
File metadata and controls
148 lines (130 loc) · 5.3 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.*;
import java.util.*;
public class DayFour {
private static Integer rowLength;
private static Integer getRow(Integer index) {
return index / rowLength;
}
private static boolean getUp(String input, Integer index) {
if(getRow(index) < 3) {
// if(index > 20){
// System.out.println(input.substring(index-15, index));
// }
return false;
}
return ("" + input.charAt(index-rowLength) +
input.charAt(index-(2*rowLength)) +
input.charAt(index-(3*rowLength))).equals("MAS");
}
private static boolean getDown(String input, Integer index) {
if(getRow(index) + 1 > (input.length() / rowLength) - 3) {
// if(index < 19500){
// System.out.println(input.substring(index, index+15));
// }
return false;
}
return ("" + input.charAt(index+rowLength) +
input.charAt(index+(2*rowLength)) +
input.charAt(index+(3*rowLength))).equals("MAS");
}
private static boolean getnwDiag(String input, Integer index) {
if(index % (rowLength) <= 2 || getRow(index) < 3) {
return false;
}
return ("" + input.charAt(index-rowLength-1) +
input.charAt(index-(2*rowLength)-2) +
input.charAt(index-(3*rowLength)-3)).equals("MAS");
}
private static boolean getneDiag(String input, Integer index) {
if(index % (rowLength) >= rowLength - 3 || getRow(index) < 3) {
return false;
}
return ("" + input.charAt(index-rowLength+1) +
input.charAt(index-(2*rowLength)+2) +
input.charAt(index-(3*rowLength)+3)).equals("MAS");
}
private static boolean getswDiag(String input, Integer index) {
if(index % (rowLength) <= 2 || getRow(index) + 1 > (input.length() / rowLength) - 3) {
return false;
}
return ("" + input.charAt(index+rowLength-1) +
input.charAt(index+(2*rowLength)-2) +
input.charAt(index+(3*rowLength)-3)).equals("MAS");
}
private static boolean getseDiag(String input, Integer index) {
if(index % (rowLength) >= rowLength - 3 || getRow(index) + 1 > (input.length() / rowLength) - 3) {
// if(index < 19500){
// System.out.println(input.substring(index-15, index));
// }
return false;
}
return ("" + input.charAt(index+rowLength+1) +
input.charAt(index+(2*rowLength)+2) +
input.charAt(index+(3*rowLength)+3)).equals("MAS");
}
//Use 2D arrys for christ sake
public static void main(String[] args){
Scanner input = new Scanner(System.in);
try {
File inputFile = new File("input.txt");
input = new Scanner(inputFile);
String fullInput = input.nextLine();
rowLength = fullInput.length();
while(input.hasNextLine()){
fullInput += input.nextLine();
}
input.close();
Integer sum = 0;
// CharacterIterator forward = new StringCharacterIterator(fullInput);
Integer index = fullInput.indexOf("X");
Integer maxIndex = index;
while(index >= 0) {
// if(index % (rowLength) >= rowLength - 3){
// System.out.println(fullInput.substring(index-15, index));
// }
if(index % (rowLength) < rowLength - 3 && fullInput.substring(index, index + 4).equals("XMAS")){
sum +=1;
System.out.println(index + " right " + sum);
}
// if(index % (rowLength) <= 3){
// System.out.println(fullInput.substring(index, index+15));
// }
if(index % (rowLength) > 2 && fullInput.substring(index - 3, index + 1).equals("SAMX")){
sum +=1;
System.out.println(index + " left " + sum);
}
if(getUp(fullInput, index)) {
sum +=1;
System.out.println(index + " up " + sum);
}
if(getDown(fullInput, index)) {
sum +=1;
System.out.println(index + " down " + sum);
}
if(getnwDiag(fullInput, index)) {
sum +=1;
System.out.println(index + " nw " + sum);
}
if(getneDiag(fullInput, index)) {
sum +=1;
System.out.println(index + " ne " + sum);
}
if(getswDiag(fullInput, index)) {
sum +=1;
System.out.println(index + " sw " + sum);
}
if(getseDiag(fullInput, index)) {
sum +=1;
System.out.println(index + " se " + sum);
}
maxIndex = index;
index = fullInput.indexOf("X", index + 1);
}
System.out.println(sum);
System.out.println(maxIndex);
// System.out.println(fullInput.substring(19599 - 20, 19599));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}