61 lines
2.0 KiB
Java
61 lines
2.0 KiB
Java
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"))) {
|
|
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());
|
|
}
|
|
}
|
|
}
|