day 1 in the books

This commit is contained in:
2025-12-01 20:44:46 -05:00
parent 3eb6aa5002
commit b3521f6253
3 changed files with 93 additions and 7 deletions

69
src/Day01.java Normal file
View File

@@ -0,0 +1,69 @@
package src;
import java.io.FileReader;
import java.util.Scanner;
public class Day01 {
public static void main(String[] args) {
part1();
part2();
}
private static void part1() {
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/01/input")).useDelimiter("\\R")) {
int dial = 50;
int password = 0;
while (scanner.hasNext()) {
String instruction = scanner.next();
int change = Integer.parseInt(instruction.substring(1));
switch (instruction.charAt(0)) {
case 'L' -> {
dial = (dial - change + 100) % 100;
}
case 'R' -> {
dial = (dial + change) % 100;
}
}
if (dial == 0) {
password += 1;
}
}
System.out.println(password);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// this is the inefficient way to do it, but it's also accurate!
private static void part2() {
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/01/input")).useDelimiter("\\R")) {
int dial = 50;
int password = 0;
while (scanner.hasNext()) {
String instruction = scanner.next();
int change = Integer.parseInt(instruction.substring(1));
switch (instruction.charAt(0)) {
case 'L' -> {
for (int i = 0; i < change; i++) {
dial = (dial - 101) % 100;
if (dial == 0) {
password += 1;
}
}
}
case 'R' -> {
for (int i = 0; i < change; i++) {
dial = (dial + 1) % 100;
if (dial == 0) {
password += 1;
}
}
}
}
}
System.out.println(password);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}