diff --git a/src/Day01.java b/src/Day01.java index f1eee64..7f15b5c 100644 --- a/src/Day01.java +++ b/src/Day01.java @@ -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,22 +39,15 @@ public class Day01 { while (scanner.hasNextLine()) { String instruction = scanner.nextLine(); int change = Integer.parseInt(instruction.substring(1)); - switch (instruction.charAt(0)) { - case 'L' -> { - for (int i = 0; i < change; i++) { - dial = (dial - 1) % 100; - if (dial == 0) { - password += 1; - } - } - } - case 'R' -> { - for (int i = 0; i < change; i++) { - dial = (dial + 1) % 100; - if (dial == 0) { - password += 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; } } }