Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. package Advent2018;
  2.  
  3. import util.AdventOfCode;
  4.  
  5. import java.util.List;
  6.  
  7. public class Day14 extends AdventOfCode {
  8.  
  9.     int intInput;
  10.  
  11.     public Day14(List<String> input) {
  12.         super(input);
  13.         title = "Chocolate Charts";
  14.         part1Description = "Scores of ten recipes after goal: ";
  15.         part2Description = "Number of recipes to get to input sequence: ";
  16.     }
  17.  
  18.     StringBuilder makeRecipes(int goal) {
  19.         StringBuilder scores = new StringBuilder();
  20.         int a = 3;
  21.         int b = 7;
  22.         int aPos = 0;
  23.         int bPos = 1;
  24.         scores.append(String.valueOf(a));
  25.         scores.append(String.valueOf(b));
  26.         while (scores.length() < goal + 10) {
  27.             int sum = a + b;
  28.             if (sum > 9) {
  29.                 scores.append(String.valueOf(sum / 10));
  30.                 scores.append(String.valueOf(sum % 10));
  31.             } else {
  32.                 scores.append(String.valueOf(sum));
  33.             }
  34.             aPos = (aPos + (1 + a)) % scores.length();
  35.             bPos = (bPos + (1 + b)) % scores.length();
  36.             a = scores.charAt(aPos) - '0';
  37.             b = scores.charAt(bPos) - '0';
  38.         }
  39.         return scores;
  40.     }
  41.  
  42.     @Override
  43.     public Object part1() {
  44.         return makeRecipes(intInput).substring(intInput);
  45.     }
  46.  
  47.     @Override
  48.     public Object part2() {
  49.         return makeRecipes(30_000_000).indexOf(input.get(0));
  50.     }
  51.  
  52.     @Override
  53.     public void parse() {
  54.         intInput = Integer.parseInt(input.get(0));
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement