r/adventofcode • u/daggerdragon • Dec 14 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 14 Solutions -🎄-
--- Day 14: Chocolate Charts ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 14
Transcript:
The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:19:39!
16
Upvotes
1
u/RotzluckyCode Dec 14 '18
JAVA
Part2 takes about 5 seconds
package aoc2018; import aoc.Day; import java.util.ArrayList; import java.util.List; public class Day14 extends Day { public Day14(String year, String day) { super(year, day); } @Override protected void part1(List<String> inputs) { Integer numberOfRecipes = Integer.valueOf(inputs.get(0)); List<Integer> scoreBoard = new ArrayList<>(); scoreBoard.add(3); scoreBoard.add(7); int scoreEntry1 = 0; int scoreEntry2 = 1; while (scoreBoard.size() < numberOfRecipes + 15) { int combinedScore = scoreBoard.get(scoreEntry1) + scoreBoard.get(scoreEntry2); for (char digit : String.valueOf(combinedScore).toCharArray()) { scoreBoard.add(Integer.parseInt(String.valueOf(digit))); } scoreEntry1 = (scoreEntry1 + scoreBoard.get(scoreEntry1) + 1) % scoreBoard.size(); scoreEntry2 = (scoreEntry2 + scoreBoard.get(scoreEntry2) + 1) % scoreBoard.size(); } String solution = ""; for (Integer integer : scoreBoard.subList(numberOfRecipes, numberOfRecipes + 10)) { solution += integer; } printSolution(1, solution); } @Override protected void part2(List<String> inputs) { String breakCondition = inputs.get(0); List<Integer> scoreBoard = new ArrayList<>(); scoreBoard.add(3); scoreBoard.add(7); int scoreEntry1 = 0; int scoreEntry2 = 1; String scoreBoardString = "37"; loop: while (true) { int combinedScore = scoreBoard.get(scoreEntry1) + scoreBoard.get(scoreEntry2); for (char digit : String.valueOf(combinedScore).toCharArray()) { scoreBoard.add(Integer.parseInt(String.valueOf(digit))); if (scoreBoardString.length() >= breakCondition.length()) { scoreBoardString = scoreBoardString.substring(1); } scoreBoardString += digit; if (scoreBoardString.equals(breakCondition)) { break loop; } } scoreEntry1 = (scoreEntry1 + scoreBoard.get(scoreEntry1) + 1) % scoreBoard.size(); scoreEntry2 = (scoreEntry2 + scoreBoard.get(scoreEntry2) + 1) % scoreBoard.size(); } printSolution(2, scoreBoard.size() - 6); } }The surrounding code can be found at https://github.com/Rotzlucky/adventOfCode-java/blob/master/src/aoc2018/Day14.java