restructured + input for 2
This commit is contained in:
51
src/Problem1.java
Normal file
51
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
src/Problem1b.java
Normal file
46
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;
|
||||
}
|
||||
}
|
||||
56
src/Problem2.java
Normal file
56
src/Problem2.java
Normal file
@@ -0,0 +1,56 @@
|
||||
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> numbers = parseRanges(input);
|
||||
|
||||
for (Long num : numbers) {
|
||||
System.out.println(num);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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()); // remove extra spaces/newlines
|
||||
}
|
||||
}
|
||||
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.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(start);
|
||||
result.add(end);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user