use switch assignment

This commit is contained in:
2025-12-01 21:11:04 -05:00
parent a06dacfbae
commit 1546b2cdeb

View File

@@ -16,14 +16,11 @@ public class Day01 {
while (scanner.hasNextLine()) {
String instruction = scanner.nextLine();
int change = Integer.parseInt(instruction.substring(1));
switch (instruction.charAt(0)) {
case 'L' -> {
dial = (dial - change + 100) % 100;
}
case 'R' -> {
dial = (dial + change) % 100;
}
}
dial = switch (instruction.charAt(0)) {
case 'L' -> (dial - change + 100) % 100;
case 'R' -> (dial + change) % 100;
default -> dial; // impossible
};
if (dial == 0) {
password += 1;
}
@@ -42,25 +39,18 @@ public class Day01 {
while (scanner.hasNextLine()) {
String instruction = scanner.nextLine();
int change = Integer.parseInt(instruction.substring(1));
switch (instruction.charAt(0)) {
case 'L' -> {
int step = switch (instruction.charAt(0)) {
case 'L' -> -1;
case 'R' -> 1;
default -> 0; // impossible
};
for (int i = 0; i < change; i++) {
dial = (dial - 1) % 100;
dial = (dial + step) % 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());