Repository navigation
Expand file tree
/
Copy pathSolution6.java
More file actions
146 lines (128 loc) · 4.23 KB
/
Copy pathSolution6.java
File metadata and controls
146 lines (128 loc) · 4.23 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
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Solution6 extends Solution {
public Solution6() {
super(6);
}
public static void main(String[] args) {
Solution6 solution = new Solution6();
solution.testPartTwo();
}
private int[] parseLineToIntArray(String line, String delimiter) {
String[] parts = line.split(delimiter);
int[] nums = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
nums[i] = Integer.parseInt(parts[i]);
}
return nums;
}
@Override
String solvePartOne(String inputData) {
String[] lines = inputData.split("\n");
int rows = lines.length - 1;
int[][] values = new int[rows][];
String[] operators;
for (int i = 0; i < rows; i++) {
String line = lines[i].trim();
values[i] = parseLineToIntArray(line, "\\s+");
}
operators = lines[rows].split("\\s+");
BigInteger sum = BigInteger.ZERO;
int cols = values[0].length;
for (int i = 0; i < cols; i++) {
long result;
if (operators[i].equals("+")) {
result = 0L;
} else {
result = 1L;
}
for (int j = 0; j < rows; j++) {
if (operators[i].equals("+")) {
result += values[j][i];
} else {
result *= values[j][i];
}
}
sum = sum.add(BigInteger.valueOf(result));
}
return sum.toString();
}
@Override
String solvePartTwo(String inputData) {
String[] rows = inputData.split("\n");
int H = rows.length;
int W = 0;
for (String r : rows) W = Math.max(W, r.length());
// Normalize width
String[] grid = new String[H];
for (int i = 0; i < H; i++) {
String r = rows[i];
if (r.length() < W) r = r + " ".repeat(W - r.length());
grid[i] = r;
}
long total = 0;
int col = W - 1;
while (col >= 0) {
// Skip separator (all spaces)
boolean isSeparator = true;
for (int r = 0; r < H; r++) {
if (grid[r].charAt(col) != ' ') {
isSeparator = false;
break;
}
}
if (isSeparator) {
col--;
continue;
}
// Collect contiguous non-separator columns for this problem
List<Integer> problemCols = new ArrayList<>();
while (col >= 0) {
boolean sep = true;
for (int r = 0; r < H; r++) {
if (grid[r].charAt(col) != ' ') {
sep = false;
break;
}
}
if (sep) break;
problemCols.add(col);
col--;
}
// Reverse to left->right order
Collections.reverse(problemCols);
// Bottom row operator
char op = 0;
for (int pc : problemCols) {
char c = grid[H - 1].charAt(pc);
if (c == '+' || c == '*') {
op = c;
break;
}
}
if (op == 0) op = '+'; // default fallback
List<Long> numbers = new ArrayList<>();
// Extract numbers column-by-column (vertical)
for (int pc : problemCols) {
StringBuilder sb = new StringBuilder();
for (int r = 0; r < H - 1; r++) {
char c = grid[r].charAt(pc);
if (Character.isDigit(c)) sb.append(c);
}
if (sb.length() > 0) {
numbers.add(Long.parseLong(sb.toString()));
}
}
// Evaluate problem
long result = (op == '+') ? 0 : 1;
for (long v : numbers) {
if (op == '+') result += v;
else result *= v;
}
total += result;
}
return String.valueOf(total);
}
}