Skip to content

Instantly share code, notes, and snippets.

@msx80
Created December 16, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msx80/ebb46f42cec75a3693616a9b4f6e45b0 to your computer and use it in GitHub Desktop.
Save msx80/ebb46f42cec75a3693616a9b4f6e45b0 to your computer and use it in GitHub Desktop.
package aoc;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
public class AOC16xOPT {
private static final String INPUT = "59764635797473718052486376718142408346357676818478503599633670059885748195966091103097769012608550645686932996546030476521264521211192035231303791868456877717957482002303790897587593845163033589025995509264282936119874431944634114034231860653524971772670684133884675724918425789232716494769777580613065860450960426147822968107966020797566015799032373298777368974345143861776639554900206816815180398947497976797052359051851907518938864559670396616664893641990595511306542705720282494028966984911349389079744726360038030937356245125498836945495984280140199805250151145858084911362487953389949062108285035318964376799823425466027816115616249496434133896";
public static void main(String[] args) throws IOException {
partOne();
partTwo();
}
/** PART ONE STUFF **/
static int[] PATTERN = {0,1,0,-1};
public static String faseAlgorithmGood(String x)
{
var chars = x.toCharArray();
StringBuffer res = new StringBuffer();
for (int charIdxBeingGenerated = 0; charIdxBeingGenerated < chars.length; charIdxBeingGenerated++)
{
long acc = 0;
for (int charIdx = charIdxBeingGenerated /* skip initial zeroes */; charIdx < chars.length; charIdx++)
{
int n = chars[charIdx]-48;
int val = getMultiplier(charIdxBeingGenerated, charIdx);
if ((val == 0)) /* skip internal zero sequences*/
{
int skip = (charIdxBeingGenerated);
// System.out.println("Skipping "+skip);
charIdx +=skip;
continue;
}
acc = acc + n * val;
}
res.append( Math.abs(acc) % 10 );
}
return res.toString();
}
private static int getMultiplier(int charIdxBeingGenerated, int charIdx)
{
int repetition = charIdxBeingGenerated+1;
int idx = (charIdx+1) / repetition;
int val = PATTERN[idx % 4];
return val;
}
private static void partOne() {
String input = INPUT;
for (int i = 0; i < 100; i++) {
input = faseAlgorithmGood(input);
System.out.println(input);
}
}
/*** PART TWO STUFF **/
private static String faseAlgorithmBad(String inp)
{
StringBuffer b = new StringBuffer();
var chars = inp.toCharArray();
int acc = 0;
for (int i = 0; i < chars.length; i++) {
int n = chars[chars.length-i-1]-48;
acc += n;
b.append(acc % 10);
}
b.reverse();
return b.toString();
}
private static void partTwo() throws IOException {
String input = generateLongInput();
for (int i = 0; i < 100; i++) {
input = faseAlgorithmBad(input);
}
System.out.println(input.substring(5976463,5976463+8));
}
private static String generateLongInput() throws IOException {
// generate the long 10000x input and store it on a file to debug quickly :P
Path p = Paths.get("longinput.txt");
String input = "";
if(!Files.exists(p))
{
String a = INPUT;
for (int i = 0; i < 10000; i++) {
input=input+a;
}
System.out.println("Input generated "+input.length());
Files.writeString(p, input, Charset.forName("ASCII"));
System.out.println("File written");
}
else
{
input = Files.readString(p, Charset.forName("ASCII"));
System.out.println(new Date()+ "File read");
}
return input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment