Skip to content

Instantly share code, notes, and snippets.

@msx80
Created December 10, 2020 09:43
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/aba38ba91291fb196f3581cff64b7a35 to your computer and use it in GitHub Desktop.
Save msx80/aba38ba91291fb196f3581cff64b7a35 to your computer and use it in GitHub Desktop.
package aoc;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class Day10 {
public static void main(String[] args) throws IOException {
int[] vals = Files.readAllLines(Paths.get("input10.txt")).stream().mapToInt(n -> Integer.parseInt(n)).sorted().toArray();
int n1 = 0;
int n3 = 1; // counting the device too, it's a 3 difference
int prev = 0;
for (int i = 0; i < vals.length; i++) {
int diff = vals[i]-prev;
if(diff==1) n1++;
if(diff==3) n3++;
if(diff>3) throw new RuntimeException("hole!");
prev = vals[i];
}
System.out.println(n1*n3);
long n = countFrom(vals, -1, new HashMap<>()); // -1 since we start from the plug
System.out.println(n);
}
private static long countFrom(int[] vals, int pos, Map<Integer, Long> calculated) {
long res = 0;
if(calculated.containsKey(pos))
{
res = calculated.get(pos);
}
else
{
int val = pos <0 ? 0 : vals[pos];
for (int i = pos+1; i < vals.length; i++) {
int v2 = vals[i];
if(v2-val <= 3)
{
res += countFrom(vals, i, calculated);
}
else
{
break;
}
}
if(pos==(vals.length-1))
{
res++;
}
System.out.println("putting "+pos+" "+res);
calculated.put(pos, res);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment