advent of code
This commit is contained in:
51
solutions/advent-of-code/src/Problem1.java
Normal file
51
solutions/advent-of-code/src/Problem1.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Problem1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<String> allLines = new ArrayList<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader("inputs/inputProblem1.txt"))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
allLines.add(line);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Call solve and print result
|
||||
System.out.println(solve(allLines));
|
||||
}
|
||||
|
||||
public static int solve(ArrayList<String> allLines) {
|
||||
int dial = 50;
|
||||
|
||||
int total = 0;
|
||||
|
||||
for (String line : allLines) {
|
||||
int value = Integer.parseInt(line.substring(1));
|
||||
if (line.charAt(0)=='L'){
|
||||
System.out.println("Turned Left: " + value);
|
||||
dial -= value;
|
||||
} else {
|
||||
System.out.println("Turned Right: " + value);
|
||||
dial += value;
|
||||
}
|
||||
|
||||
int num = (dial % 100 + 100) % 100; // Ensure num is between 0-99
|
||||
System.out.println("Current Dial Position: " + num);
|
||||
|
||||
if (num == 0) {
|
||||
total++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
}
|
||||
46
solutions/advent-of-code/src/Problem1b.java
Normal file
46
solutions/advent-of-code/src/Problem1b.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Problem1b {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<String> allLines = new ArrayList<>();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader("inputs/inputProblem1.txt"))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
allLines.add(line);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Call solve and print result
|
||||
System.out.println(solve(allLines));
|
||||
}
|
||||
|
||||
public static int solve(ArrayList<String> allLines) {
|
||||
int dial = 50; // starting position
|
||||
int total = 0;
|
||||
|
||||
for (String line : allLines) {
|
||||
int value = Integer.parseInt(line.substring(1));
|
||||
int direction = (line.charAt(0) == 'L') ? -1 : 1;
|
||||
|
||||
// Step through each click to count zeros correctly
|
||||
for (int step = 0; step < value; step++) {
|
||||
dial = (dial + direction + 100) % 100; // wrap around 0-99
|
||||
if (dial == 0) {
|
||||
total++;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Current Dial Position: " + dial + " || Total: " + total);
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
}
|
||||
85
solutions/advent-of-code/src/Problem2.java
Normal file
85
solutions/advent-of-code/src/Problem2.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Problem2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String filePath = "inputs/inputProblem2.txt";
|
||||
|
||||
try {
|
||||
String input = readFileAsString(filePath);
|
||||
List<long[]> ranges = parseRanges(input);
|
||||
long tally = solve(ranges);
|
||||
System.out.println("Sum of invalid IDs: " + tally);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Solve by generating only repeated IDs
|
||||
public static long solve(List<long[]> ranges) {
|
||||
long sum = 0;
|
||||
|
||||
for (long[] range : ranges) {
|
||||
long start = range[0];
|
||||
long end = range[1];
|
||||
|
||||
for (long num = start; num <= end; num++) {
|
||||
String numStr = String.valueOf(num);
|
||||
int len = numStr.length();
|
||||
|
||||
if (len % 2 != 0) continue;
|
||||
|
||||
boolean repeated = false;
|
||||
for (int prefixLen = 1; prefixLen <= len / 2; prefixLen++) {
|
||||
if (len % prefixLen != 0) continue; // must divide evenly
|
||||
|
||||
String prefix = numStr.substring(0, prefixLen);
|
||||
String repeatedNum = prefix + prefix;
|
||||
if (repeatedNum.equals(numStr)) {
|
||||
repeated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (repeated) sum += num;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static String readFileAsString(String filePath) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line.trim());
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static List<long[]> parseRanges(String input) {
|
||||
List<long[]> result = new ArrayList<>();
|
||||
String[] ranges = input.split(",");
|
||||
|
||||
for (String range : ranges) {
|
||||
String[] parts = range.trim().split("-");
|
||||
if (parts.length != 2) {
|
||||
throw new IllegalArgumentException("Invalid range: " + range);
|
||||
}
|
||||
long start = Long.parseLong(parts[0]);
|
||||
long end = Long.parseLong(parts[1]);
|
||||
result.add(new long[]{start, end});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
87
solutions/advent-of-code/src/Problem2b.java
Normal file
87
solutions/advent-of-code/src/Problem2b.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Problem2b {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String filePath = "inputs/inputProblem2.txt";
|
||||
|
||||
try {
|
||||
String input = readFileAsString(filePath);
|
||||
List<long[]> ranges = parseRanges(input);
|
||||
long tally = solve(ranges);
|
||||
System.out.println("Sum of invalid IDs: " + tally);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static long solve(List<long[]> ranges) {
|
||||
long sum = 0;
|
||||
|
||||
for (long[] range : ranges) {
|
||||
long start = range[0];
|
||||
long end = range[1];
|
||||
|
||||
for (long num = start; num <= end; num++) {
|
||||
String numStr = String.valueOf(num);
|
||||
int len = numStr.length();
|
||||
|
||||
boolean repeated = false;
|
||||
|
||||
for (int prefixLen = 1; prefixLen <= len / 2; prefixLen++) {
|
||||
if (len % prefixLen != 0) continue;
|
||||
|
||||
String prefix = numStr.substring(0, prefixLen);
|
||||
int times = len / prefixLen;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int t = 0; t < times; t++) {
|
||||
sb.append(prefix);
|
||||
}
|
||||
|
||||
if (sb.toString().equals(numStr)) {
|
||||
repeated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (repeated) sum += num;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static String readFileAsString(String filePath) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line.trim());
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static List<long[]> parseRanges(String input) {
|
||||
List<long[]> result = new ArrayList<>();
|
||||
String[] ranges = input.split(",");
|
||||
|
||||
for (String range : ranges) {
|
||||
String[] parts = range.trim().split("-");
|
||||
if (parts.length != 2) {
|
||||
throw new IllegalArgumentException("Invalid range: " + range);
|
||||
}
|
||||
long start = Long.parseLong(parts[0]);
|
||||
long end = Long.parseLong(parts[1]);
|
||||
result.add(new long[]{start, end});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
41
solutions/advent-of-code/src/Problem3.java
Normal file
41
solutions/advent-of-code/src/Problem3.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Problem3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String filePath = "inputs/inputProblem3.txt";
|
||||
try (BufferedReader reader = new BueredReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
long total = 0;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
total += maxTwoDigit(line);
|
||||
}
|
||||
System.out.println(total);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static int maxTwoDigit(String digits) {
|
||||
int maxDigit = -1;
|
||||
int maxValue = 0;
|
||||
for (char c : digits.toCharArray()) {
|
||||
int digit = c - '0';
|
||||
if (maxDigit >= 0) {
|
||||
int value = maxDigit * 10 + digit;
|
||||
if (value > maxValue) {
|
||||
maxValue = value;
|
||||
}
|
||||
}
|
||||
if (digit > maxDigit) {
|
||||
maxDigit = digit;
|
||||
}
|
||||
}
|
||||
|
||||
return maxValue;
|
||||
}
|
||||
}
|
||||
39
solutions/advent-of-code/src/Problem3b.java
Normal file
39
solutions/advent-of-code/src/Problem3b.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Problem3b {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String filePath = "inputs/inputProblem3.txt";
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
long total = 0;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String maxDigits = maxKDigitsString(line, 12);
|
||||
total += Long.parseLong(maxDigits);
|
||||
}
|
||||
System.out.println(total);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String maxKDigitsString(String digits, int k) {
|
||||
int n = digits.length();
|
||||
char[] stack = new char[k];
|
||||
int top = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = digits.charAt(i);
|
||||
while (top > 0 && stack[top - 1] < c && (top - 1 + (n - i)) >= k) {
|
||||
top--;
|
||||
}
|
||||
if (top < k) {
|
||||
stack[top++] = c;
|
||||
}
|
||||
}
|
||||
return new String(stack, 0, k);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user