day 1 in the books
This commit is contained in:
69
src/Day01.java
Normal file
69
src/Day01.java
Normal file
@@ -0,0 +1,69 @@
|
||||
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")).useDelimiter("\\R")) {
|
||||
int dial = 50;
|
||||
int password = 0;
|
||||
while (scanner.hasNext()) {
|
||||
String instruction = scanner.next();
|
||||
int change = Integer.parseInt(instruction.substring(1));
|
||||
switch (instruction.charAt(0)) {
|
||||
case 'L' -> {
|
||||
dial = (dial - change + 100) % 100;
|
||||
}
|
||||
case 'R' -> {
|
||||
dial = (dial + change) % 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")).useDelimiter("\\R")) {
|
||||
int dial = 50;
|
||||
int password = 0;
|
||||
while (scanner.hasNext()) {
|
||||
String instruction = scanner.next();
|
||||
int change = Integer.parseInt(instruction.substring(1));
|
||||
switch (instruction.charAt(0)) {
|
||||
case 'L' -> {
|
||||
for (int i = 0; i < change; i++) {
|
||||
dial = (dial - 101) % 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user