From 1546b2cdebc1ba7436df723f1b3225b7dfb76eed Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 1 Dec 2025 21:11:04 -0500 Subject: [PATCH] use switch assignment --- src/Day01.java | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) 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; } } }