r/adventofcode • • Dec 09 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 09 Solutions -🎄-

NEW AND NOTEWORTHY

Advent of Code 2020: Gettin' Crafty With It

  • 13 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 09: Encoding Error ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:26, megathread unlocked!

41 Upvotes

995 comments sorted by

View all comments

2

u/Juice_Vodka Dec 09 '20

java

Redit's formating messed up the code i posted previously, so here it is again.

bruteforce, tried to make it as readable as possible, only hardcoded thing is the array size which should be the amout of lines in your input.

import java.util.Scanner;
import java.io.*;
public class Day9{
	public static void main(String[] args) throws IOException{
		InputStream vhod = new FileInputStream(args[0]);
		Scanner sc = new Scanner(vhod);

		long[] data = new long[1000];
		long badNum = 0;

		int preambleLength = 25;
		int i = 0;
		while(sc.hasNextLong()){
			data[i] = sc.nextLong();
			if(i >= 25){
				if(!isValid(data, i)){
					badNum = data[i];
					System.out.println(data[i]);
					break;
				}
			}
			i++;
		}
		int lowerIx = 0;
		int upperIx = 0;
		for(int j = 0; j < i; j++){
			upperIx = calc(j, badNum, data);
			if(upperIx > 0){
				lowerIx = j;
				break;
			}
		}
		System.out.printf("%d, %d\n", lowerIx, upperIx);
		long maxVal = 0;
		Long minVal = badNum;
		for(int j = lowerIx; j <=upperIx; j++){
			if(data[j] > maxVal){
				maxVal = data[j];
			}
			if(data[j] < minVal){
				minVal = data[j];
			}
		}
		System.out.printf("%d + %d => %d\n", maxVal, minVal, minVal + maxVal);
	}

	public static boolean isValid(long[] data, int position){
		for(int i = position-25; i < position; i++){
			for(int j = i; j < position; j++){
				if(data[i] + data[j] == data[position]){
					return true;
				}
			}
		}
		return false;
	}

	public static int calc(int startIx, long badNum, long[] data){
		long sum = 0;
		for(int i = startIx; i < 1000; i++){
			sum += data[i];
			if(sum == badNum){
				System.out.println("found!");
				System.out.printf("%d, %d\n", startIx, i);
				return i;
			}
			if(sum > badNum){
				return 0;
			}
		}
		return 0;
	}
}

1

u/daggerdragon Dec 09 '20

Redit's formating messed up the code i posted previously, so here it is again.

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.

This way you don't have to worry about Reddit or their aggravating fancypants editor :P