-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay06.java
More file actions
139 lines (119 loc) · 3.66 KB
/
Day06.java
File metadata and controls
139 lines (119 loc) · 3.66 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
package org.ck.adventofclaude.year2025;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.ck.adventofclaude.util.AOCSolution;
public class Day06 extends AOCSolution {
@Override
protected void runPartOne(final Scanner in) {
run(in, false);
}
@Override
protected void runPartTwo(final Scanner in) {
run(in, true);
}
private void run(final Scanner in, final boolean rightToLeft) {
List<String> lines = new ArrayList<>();
while (in.hasNextLine()) {
lines.add(in.nextLine());
}
if (lines.isEmpty()) {
print(0);
return;
}
// Find the maximum width to handle all columns
int maxWidth = lines.stream().mapToInt(String::length).max().orElse(0);
long grandTotal = 0;
// Find problem regions (groups of columns separated by all-space columns)
int col = 0;
while (col < maxWidth) {
// Skip separator columns (all spaces)
boolean isAllSpaces = true;
for (String line : lines) {
if (col < line.length() && line.charAt(col) != ' ') {
isAllSpaces = false;
break;
}
}
if (isAllSpaces) {
col++;
continue;
}
// Found start of a problem region - find its end
int problemStart = col;
int problemEnd = col;
while (problemEnd < maxWidth) {
boolean allSpaces = true;
for (String line : lines) {
if (problemEnd < line.length() && line.charAt(problemEnd) != ' ') {
allSpaces = false;
break;
}
}
if (allSpaces) {
break;
}
problemEnd++;
}
// Extract numbers and operator from this problem region
List<Long> numbers = new ArrayList<>();
Character operator = null;
if (rightToLeft) {
// Part 2: Read columns right-to-left, digits top-to-bottom
for (int c = problemEnd - 1; c >= problemStart; c--) {
StringBuilder number = new StringBuilder();
for (String line : lines) {
if (c < line.length()) {
char ch = line.charAt(c);
if (Character.isDigit(ch)) {
number.append(ch);
} else if (ch == '*' || ch == '+') {
operator = ch;
}
}
}
if (number.length() > 0) {
numbers.add(Long.parseLong(number.toString()));
}
}
} else {
// Part 1: Read horizontally
for (String line : lines) {
// Extract the substring for this problem
int start = problemStart;
int end = Math.min(problemEnd, line.length());
if (start < end) {
String segment = line.substring(start, end).trim();
if (!segment.isEmpty()) {
// Check if it's an operator
if (segment.equals("*") || segment.equals("+")) {
operator = segment.charAt(0);
} else {
// Try to parse as number
try {
numbers.add(Long.parseLong(segment));
} catch (NumberFormatException e) {
// Ignore non-numeric segments
}
}
}
}
}
}
// Calculate result for this problem
if (!numbers.isEmpty() && operator != null) {
long result = numbers.get(0);
for (int i = 1; i < numbers.size(); i++) {
if (operator == '*') {
result *= numbers.get(i);
} else if (operator == '+') {
result += numbers.get(i);
}
}
grandTotal += result;
}
col = problemEnd;
}
print(grandTotal);
}
}