aoc2021day06 in java

 avatar
user_5259823477
java
3 years ago
890 B
7
Indexable
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Solution6B {
  public static final int SIZE = 9;
  public static final int DAYS = 256;
  public static void main(String[] args) {
    long[] ring = new long[SIZE];
    try {
      File myObj = new File(args[0]);
      Scanner myReader = new Scanner(myObj);
      String[] data = myReader.nextLine().split(",");
      for (String s : data) {
        ring[Integer.valueOf(s)]++;
      }
      myReader.close();
    } catch (FileNotFoundException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
    int index = 0;
    for (int day = 1; day <= DAYS; day++) {
      ring[(index+SIZE-2) % SIZE] += ring[index];
      index = (index+1) % SIZE;
    }
    long count = 0;
    for (int i = 0; i < SIZE; i++) count += ring[i];
    System.out.println(count);
  }
}
Editor is loading...