Guest User

Untitled

a guest
Dec 9th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | Source Code | 0 0
  1. package AoC;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5.  
  6. public class Day1 {
  7.   public static void partA(Scanner fileScanner) {
  8.  
  9.     int startValue = 50;
  10.     int password = 0;
  11.     while (fileScanner.hasNext()) {
  12.       String line = fileScanner.nextLine();
  13.       char direction = line.charAt(0);
  14.       int rot = Integer.parseInt(line.substring(1));
  15.       switch (direction) {
  16.       case 'R':
  17.         startValue += rot;
  18.         startValue %= 100;
  19.         break;
  20.       case 'L':
  21.         startValue -= rot;
  22.         startValue %= 100;
  23.         break;
  24.       default:
  25.         break;
  26.       }
  27.  
  28.       if (startValue == 0)
  29.         password += 1;
  30.     }
  31.     System.out.println(password);
  32.   }
  33.   static int countRotations(int start, int rot) {
  34.     int newVal = start + rot;
  35.     if (newVal >= 100) {
  36.       return (start != 0 ? 1 : 0) + (rot - start) / 100;
  37.     } else if (newVal <= 0) {
  38.       return (start != 0 ? 1 : 0) + (- rot - start) / 100;
  39.     } else {
  40.       return 0;
  41.     }
  42.   }
  43.  
  44.   public static void partB(Scanner fileScanner) {
  45.     int startValue = 50;
  46.     int n_rotations = 0;
  47.     while (fileScanner.hasNext()) {
  48.       String line = fileScanner.nextLine();
  49.       int commentStart = line.indexOf('#');
  50.       int rotationsOnTurn = 0;
  51.       if (commentStart != -1) {
  52.         line = line.substring(0, commentStart).trim();
  53.       }
  54.       char direction = line.charAt(0);
  55.       int rot = Integer.parseInt(line.substring(1));
  56.       switch (direction) {
  57.       case 'R':
  58.         rotationsOnTurn = countRotations(startValue, rot);
  59.         startValue += rot;
  60.         break;
  61.       case 'L':
  62.         rotationsOnTurn = countRotations(startValue, -rot);
  63.         startValue -= rot;
  64.         break;
  65.       default:
  66.         break;
  67.       }
  68.       if (startValue < 0)
  69.         startValue = (startValue % 100 + 100) % 100;
  70.       else
  71.         startValue = startValue % 100;
  72.       n_rotations += rotationsOnTurn;
  73.       System.out.println("Action: " + line + String.format(" : +%d, (%d), %d", rotationsOnTurn, startValue, n_rotations));
  74.     }
  75.     System.out.println(n_rotations);
  76.   }
  77.  
  78.   public static void main(String[] args) {
  79.     File file = new File("inputs/day1.txt");
  80.     Scanner fileScanner = null;
  81.     try {
  82.       fileScanner = new Scanner(file);
  83.     } catch (FileNotFoundException e) {
  84.       System.out.println("File not found: " + e.getMessage());
  85.       System.exit(1);
  86.     }
  87.     partB(fileScanner);
  88.   }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment