Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advent2018;
- import util.AdventOfCode;
- import java.util.ArrayDeque;
- import java.util.Arrays;
- import java.util.List;
- public class Day9 extends AdventOfCode {
- int players;
- int end;
- class CircleDeque<T> extends ArrayDeque<T> {
- void rotate(int num) {
- if (num == 0) return;
- if (num > 0) {
- for (int i = 0; i < num; i++) {
- T t = this.removeLast();
- this.addFirst(t);
- }
- } else {
- for (int i = 0; i < Math.abs(num) - 1; i++) {
- T t = this.remove();
- this.addLast(t);
- }
- }
- }
- }
- public Day9(List<String> input) {
- super(input);
- title = "Marble Mania";
- part1Description = "Winning Elf score: ";
- part2Description = "Winning Elf score with end marble * 100: ";
- }
- long game(int players, int end) {
- CircleDeque<Integer> circle = new CircleDeque<>();
- circle.addFirst(0);
- long[] scores = new long[players];
- for (int i = 1; i <= end; i++) {
- if (i % 23 == 0) {
- circle.rotate(-7);
- scores[i % players] += i + circle.pop();
- } else {
- circle.rotate(2);
- circle.addLast(i);
- }
- }
- return Arrays.stream(scores).max().getAsLong();
- }
- @Override
- public Object part1() {
- return game(players, end);
- }
- @Override
- public Object part2() {
- return game(players, end * 100);
- }
- @Override
- public void parse() {
- String[] split = input.get(0).split(" ");
- players = Integer.parseInt(split[0]);
- end = Integer.parseInt(split[6]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement