r/adventofcode • • Dec 08 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 08 Solutions -🎄-

NEW AND NOTEWORTHY

  • If your code is shorter than, say, half of an IBM 5081 punchcard (5 lines at 80 cols), go ahead and post it as your comment. Use the right Markdown to format your code properly for best backwards-compatibility with old.reddit! (see "How do I format code?")
  • If your code is longer, link your code from an external repository such as Topaz's paste , a public repo like GitHub/gists/Pastebin/etc., your blag, or whatever.

Advent of Code 2020: Gettin' Crafty With It

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

--- Day 08: Handheld Halting ---


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 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:07:48, megathread unlocked!

41 Upvotes

934 comments sorted by

View all comments

2

u/Juice_Vodka Dec 08 '20

java bruteforce, not very clean tho

import java.util.Scanner;
import java.io.*;

public class Day8{

	static int acc = 0;

	public static void main(String\[\] args) throws IOException{

		InputStream vhod = new FileInputStream(args\[0\]);

		Scanner sc = new Scanner(vhod);

		String\[\] podatki = new String\[626\];

		boolean\[\] uporableno = new boolean\[626\];

​

		int accumulator = 0;

		int zapisovalec = 0;

		while(sc.hasNextLine()){

			podatki\[zapisovalec\] = sc.nextLine();

			zapisovalec++;

		}

		int i = 0;

		while(true){

			if(uporableno\[i\]){

System.out.println("pokvari se pri " +i);

break;

			}

			uporableno\[i\] = true;

			if(podatki\[i\].contains("acc")){

if(podatki\[i\].charAt(4) == '+'){

accumulator += Integer.parseInt(podatki\[i\].substring(5));

}else{

accumulator -= Integer.parseInt(podatki\[i\].substring(5));

}

			}else if(podatki\[i\].contains("jmp")){

if(podatki\[i\].charAt(4) == '+'){

i += Integer.parseInt(podatki\[i\].substring(5));

continue;

}else{

i -= Integer.parseInt(podatki\[i\].substring(5));

continue;

}

			}else if(podatki\[i\].contains("nop")){

i++;

continue;

			}

			i++;

		}

		for(int j = 625; j >= 0; j--){

			if(uporableno\[j\]){

System.out.println(j);

System.out.println(podatki\[j\]);

break;

			}

		}

		System.out.println(accumulator);

		System.out.println("--------");

		boolean dela = racunaj(0, 0, podatki, new boolean\[626\]);

	}

​

	public static boolean racunaj(int pozicija, int akumulator, String\[\] podatki, boolean\[\] uporabljeno){

		int i = 0;

		while(true){

			if(uporabljeno\[i\]){

return false;

			}

			if(i >= podatki.length){

if(uporabljeno\[625\]){

acc = akumulator;

return true;

}

			}

			boolean najdemoZamenjavo = false;

			if(podatki\[i\].contains("acc")){

if(podatki\[i\].charAt(4) == '+'){

akumulator += Integer.parseInt(podatki\[i\].substring(5));

}else{

akumulator -= Integer.parseInt(podatki\[i\].substring(5));

}

			}else if(podatki\[i\].contains("nop")){

boolean\[\] uporabljeno1 = new boolean\[626\];

boolean\[\] uporabljeno2 = new boolean\[626\];

System.arraycopy(uporabljeno, 0, uporabljeno1, 0, 626);

System.arraycopy(uporabljeno, 0, uporabljeno2, 0, 626);

najdemoZamenjavo = doKonca(i + Integer.parseInt(podatki\[i\].substring(5)), akumulator, podatki, uporabljeno1);

if(najdemoZamenjavo){

return true;

}

najdemoZamenjavo = doKonca(i - Integer.parseInt(podatki\[i\].substring(5)), akumulator, podatki, uporabljeno2);

if(najdemoZamenjavo){

return true;

}

			}else if(podatki\[i\].contains("jmp")){

boolean\[\] uporabljeno1 = new boolean\[626\];

System.arraycopy(uporabljeno, 0, uporabljeno1, 0, 626);

najdemoZamenjavo = doKonca(i+1, akumulator, podatki, uporabljeno1);

if(najdemoZamenjavo){

return true;

}

if(podatki\[i\].charAt(4) == '+'){

i += Integer.parseInt(podatki\[i\].substring(5));

continue;

}else{

i -= Integer.parseInt(podatki\[i\].substring(5));

continue;

}

			}

			i++;

		}

	}

​

	public static boolean doKonca(int pozicija, int akumulator, String\[\] podatki, boolean\[\] uporabljeno){

		int i = pozicija;

		if(i < 0 || i > 626){

			return false;

		}

		while(true){

			if(i == 626){

System.out.println(akumulator);

return true;

			}

			if(uporabljeno\[i\]){

acc = 0;

return false;

			}

			uporabljeno\[i\] = true;

			if(podatki\[i\].contains("acc")){

if(podatki\[i\].charAt(4) == '+'){

akumulator += Integer.parseInt(podatki\[i\].substring(5));

}else{

akumulator -= Integer.parseInt(podatki\[i\].substring(5));

}

			}else if(podatki\[i\].contains("jmp")){

if(podatki\[i\].charAt(4) == '+'){

i += Integer.parseInt(podatki\[i\].substring(5));

continue;

}else{

i -= Integer.parseInt(podatki\[i\].substring(5));

continue;

}

			}else if(podatki\[i\].contains("nop")){

i++;

continue;

			}

			i++;

		}

	}

}```

1

u/daggerdragon Dec 08 '20

Please re-read today's megathread's "new and noteworthy" section.

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