use switch assignment
This commit is contained in:
@@ -16,14 +16,11 @@ public class Day01 {
|
|||||||
while (scanner.hasNextLine()) {
|
while (scanner.hasNextLine()) {
|
||||||
String instruction = scanner.nextLine();
|
String instruction = scanner.nextLine();
|
||||||
int change = Integer.parseInt(instruction.substring(1));
|
int change = Integer.parseInt(instruction.substring(1));
|
||||||
switch (instruction.charAt(0)) {
|
dial = switch (instruction.charAt(0)) {
|
||||||
case 'L' -> {
|
case 'L' -> (dial - change + 100) % 100;
|
||||||
dial = (dial - change + 100) % 100;
|
case 'R' -> (dial + change) % 100;
|
||||||
}
|
default -> dial; // impossible
|
||||||
case 'R' -> {
|
};
|
||||||
dial = (dial + change) % 100;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (dial == 0) {
|
if (dial == 0) {
|
||||||
password += 1;
|
password += 1;
|
||||||
}
|
}
|
||||||
@@ -42,25 +39,18 @@ public class Day01 {
|
|||||||
while (scanner.hasNextLine()) {
|
while (scanner.hasNextLine()) {
|
||||||
String instruction = scanner.nextLine();
|
String instruction = scanner.nextLine();
|
||||||
int change = Integer.parseInt(instruction.substring(1));
|
int change = Integer.parseInt(instruction.substring(1));
|
||||||
switch (instruction.charAt(0)) {
|
int step = switch (instruction.charAt(0)) {
|
||||||
case 'L' -> {
|
case 'L' -> -1;
|
||||||
|
case 'R' -> 1;
|
||||||
|
default -> 0; // impossible
|
||||||
|
};
|
||||||
for (int i = 0; i < change; i++) {
|
for (int i = 0; i < change; i++) {
|
||||||
dial = (dial - 1) % 100;
|
dial = (dial + step) % 100;
|
||||||
if (dial == 0) {
|
if (dial == 0) {
|
||||||
password += 1;
|
password += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 'R' -> {
|
|
||||||
for (int i = 0; i < change; i++) {
|
|
||||||
dial = (dial + 1) % 100;
|
|
||||||
if (dial == 0) {
|
|
||||||
password += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println(password);
|
System.out.println(password);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user