Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package Advent2018;
  2.  
  3. import util.AdventOfCode;
  4.  
  5. import java.util.ArrayDeque;
  6. import java.util.Arrays;
  7. import java.util.List;
  8.  
  9. public class Day9 extends AdventOfCode {
  10.  
  11.     int players;
  12.     int end;
  13.  
  14.  
  15.     class CircleDeque<T> extends ArrayDeque<T> {
  16.         void rotate(int num) {
  17.             if (num == 0) return;
  18.             if (num > 0) {
  19.                 for (int i = 0; i < num; i++) {
  20.                     T t = this.removeLast();
  21.                     this.addFirst(t);
  22.                 }
  23.             } else {
  24.                 for (int i = 0; i < Math.abs(num) - 1; i++) {
  25.                     T t = this.remove();
  26.                     this.addLast(t);
  27.                 }
  28.             }
  29.  
  30.         }
  31.     }
  32.  
  33.     public Day9(List<String> input) {
  34.         super(input);
  35.         title = "Marble Mania";
  36.         part1Description = "Winning Elf score: ";
  37.         part2Description = "Winning Elf score with end marble * 100: ";
  38.     }
  39.  
  40.     long game(int players, int end) {
  41.         CircleDeque<Integer> circle = new CircleDeque<>();
  42.         circle.addFirst(0);
  43.         long[] scores = new long[players];
  44.         for (int i = 1; i <= end; i++) {
  45.             if (i % 23 == 0) {
  46.                 circle.rotate(-7);
  47.                 scores[i % players] += i + circle.pop();
  48.  
  49.             } else {
  50.                 circle.rotate(2);
  51.                 circle.addLast(i);
  52.             }
  53.         }
  54.         return Arrays.stream(scores).max().getAsLong();
  55.     }
  56.  
  57.     @Override
  58.     public Object part1() {
  59.         return game(players, end);
  60.     }
  61.  
  62.     @Override
  63.     public Object part2() {
  64.         return game(players, end * 100);
  65.     }
  66.  
  67.     @Override
  68.     public void parse() {
  69.         String[] split = input.get(0).split(" ");
  70.         players = Integer.parseInt(split[0]);
  71.         end = Integer.parseInt(split[6]);
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement