-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
128 lines (112 loc) · 3.87 KB
/
Main.java
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
package day13;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
class Pattern {
List<String> lines;
Pattern(List<String> lines) {
this.lines = new ArrayList<String>(lines);
}
private int rows() {
return lines.size();
}
private int columns() {
return lines.get(0).length();
}
private int rowsDifferences(int i, int j) {
int differences = 0;
for (int k = 0; k < columns(); k++) {
if (lines.get(i).charAt(k) != lines.get(j).charAt(k)) {
differences++;
}
}
return differences;
}
private int colsDifferences(int i, int j) {
int differences = 0;
for (String line : lines) {
if (line.charAt(i) != line.charAt(j)) {
differences++;
}
}
return differences;
}
private int horizontalDifferences(int lines) {
int differences = 0;
for (int i = lines, j = lines+1; i >= 0 && j < rows(); i--, j++) {
differences += rowsDifferences(i, j);
}
return differences;
}
private int verticalDifferences(int col) {
int differences = 0;
for (int i = col, j = col+1; i >= 0 && j < columns(); i--, j++) {
differences += colsDifferences(i, j);
}
return differences;
}
int horizontalReflection(final int differences) {
for (int rowReflection = 0; (rowReflection + 1) < rows(); rowReflection++) {
if (horizontalDifferences(rowReflection) == differences) {
return rowReflection+1;
}
}
return -1;
}
public int verticalReflection(final int differences) {
for (int colReflection = 0; (colReflection + 1) < columns(); colReflection++) {
if (verticalDifferences(colReflection) == differences) {
return colReflection+1;
}
}
return -1;
}
}
public class Main {
private static List<Pattern> parseContent(String content) {
String[] lines = content.split("\\n");
List<String> currentPattern = new ArrayList<>();
List<Pattern> patterns = new ArrayList<>();
for (String line : lines) {
if (line.length() == 0) {
patterns.add(new Pattern(currentPattern));
currentPattern.clear();
continue;
}
currentPattern.add(line);
}
patterns.add(new Pattern(currentPattern));
return patterns;
}
private static int sumReflections(String filepath, final int differences) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filepath)));
List<Pattern> patterns = parseContent(content);
List<Integer> reflections = new ArrayList<>();
for (Pattern pattern : patterns) {
int verticalReflection = pattern.verticalReflection(differences);
int horizontalReflection = pattern.horizontalReflection(differences);
if (verticalReflection > horizontalReflection) {
reflections.add(verticalReflection);
} else {
reflections.add(100 * horizontalReflection);
}
}
return reflections.stream().reduce(0, Integer::sum);
}
public static int part01(String filepath) throws IOException {
return sumReflections(filepath, 0);
}
public static int part02(String filepath) throws IOException {
return sumReflections(filepath, 1);
}
public static void main(String[] args) {
try {
System.out.println("Part 01: " + part01("day13/input.txt"));
System.out.println("Part 02: " + part02("day13/input.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
}