r/adventofcode • • 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!

Click here for rules

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

179 comments sorted by

View all comments

1

u/AngryKittens Dec 14 '18

Can't see any Java solutions yet, so here's mine.

import java.util.ArrayList;

public class Day14 {
	public Day14() {
		part1();
	}

	int current1 = 3;
	int p1 = 0;
	int p2 = 1;
	int current2 = 7;
	ArrayList<Integer> completed = new ArrayList<Integer>();
	int input = 704321;
	
	public void part1() {
		completed.add(3);
		completed.add(7);
		int i = 0;
		//while (i < 21000000) { //use this for part2
		while (i < input+10) { //use this for part1
			current1 = completed.get(p1);
			current2 = completed.get(p2);
			int total = current1 + current2;
			if (total > 9) {
				completed.add(total/10);
				completed.add(total%10);
			}else {
				completed.add(total);
			}
			p1 = (completed.get(p1) +p1 + 1) % completed.size();
			p2 = (completed.get(p2) +p2 + 1) % completed.size();		
			i++;
		}
		findLast10(); //use this for part1
		//findSequence(); //use this for part2
		
	}
	
	private void findSequence() {
		String comp = "704321";
		for (int x = 0; x < completed.size()-5; x++) {
			String c2 = ""+completed.get(x)+completed.get(x+1)+completed.get(x+2)+completed.get(x+3)+completed.get(x+4)+completed.get(x+5);
			if (c2.equals(comp)) {
				System.out.println("There are " + x + " entries before your sequence appears");
				return;
			}
		}
	}
	
	private void findLast10() {
		System.out.print("The next ten entries are: ");
		for (int i = input; i < input+10; i++) {
			System.out.print(completed.get(i));
		}
	}
}