Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AoC;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class Day1 {
- public static void partA(Scanner fileScanner) {
- int startValue = 50;
- int password = 0;
- while (fileScanner.hasNext()) {
- String line = fileScanner.nextLine();
- char direction = line.charAt(0);
- int rot = Integer.parseInt(line.substring(1));
- switch (direction) {
- case 'R':
- startValue += rot;
- startValue %= 100;
- break;
- case 'L':
- startValue -= rot;
- startValue %= 100;
- break;
- default:
- break;
- }
- if (startValue == 0)
- password += 1;
- }
- System.out.println(password);
- }
- static int countRotations(int start, int rot) {
- int newVal = start + rot;
- if (newVal >= 100) {
- return (start != 0 ? 1 : 0) + (rot - start) / 100;
- } else if (newVal <= 0) {
- return (start != 0 ? 1 : 0) + (- rot - start) / 100;
- } else {
- return 0;
- }
- }
- public static void partB(Scanner fileScanner) {
- int startValue = 50;
- int n_rotations = 0;
- while (fileScanner.hasNext()) {
- String line = fileScanner.nextLine();
- int commentStart = line.indexOf('#');
- int rotationsOnTurn = 0;
- if (commentStart != -1) {
- line = line.substring(0, commentStart).trim();
- }
- char direction = line.charAt(0);
- int rot = Integer.parseInt(line.substring(1));
- switch (direction) {
- case 'R':
- rotationsOnTurn = countRotations(startValue, rot);
- startValue += rot;
- break;
- case 'L':
- rotationsOnTurn = countRotations(startValue, -rot);
- startValue -= rot;
- break;
- default:
- break;
- }
- if (startValue < 0)
- startValue = (startValue % 100 + 100) % 100;
- else
- startValue = startValue % 100;
- n_rotations += rotationsOnTurn;
- System.out.println("Action: " + line + String.format(" : +%d, (%d), %d", rotationsOnTurn, startValue, n_rotations));
- }
- System.out.println(n_rotations);
- }
- public static void main(String[] args) {
- File file = new File("inputs/day1.txt");
- Scanner fileScanner = null;
- try {
- fileScanner = new Scanner(file);
- } catch (FileNotFoundException e) {
- System.out.println("File not found: " + e.getMessage());
- System.exit(1);
- }
- partB(fileScanner);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment