Files
AdventOfCode2025/src/Day01.java
2025-12-03 22:25:13 -05:00

68 lines
2.3 KiB
Java

package src;
import java.io.FileReader;
import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;
public class Day01 {
public static void main(String[] args) {
Instant start = Instant.now();
part1();
Instant middle = Instant.now();
part2();
Instant end = Instant.now();
IO.println("part 1: " + Duration.between(start, middle).toMillis() + " ms");
IO.println("part 2: " + Duration.between(middle, end).toMillis() + " ms");
}
private static void part1() {
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/01/input"))) {
int dial = 50;
int password = 0;
while (scanner.hasNextLine()) {
String instruction = scanner.nextLine();
int change = Integer.parseInt(instruction.substring(1));
int step = switch (instruction.charAt(0)) {
case 'L' -> -1;
case 'R' -> 1;
default -> 0; // impossible
};
dial = (dial + change * step) % 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"))) {
int dial = 50;
int password = 0;
while (scanner.hasNextLine()) {
String instruction = scanner.nextLine();
int change = Integer.parseInt(instruction.substring(1));
int step = switch (instruction.charAt(0)) {
case 'L' -> -1;
case 'R' -> 1;
default -> 0; // impossible
};
for (int i = 0; i < change; i++) {
dial = (dial + step) % 100;
if (dial == 0) {
password += 1;
}
}
}
System.out.println(password);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}