Skip to content

Instantly share code, notes, and snippets.

@gnarf
Last active December 22, 2021 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gnarf/1e2f54371c22a05b0cd3c31fc8f2be46 to your computer and use it in GitHub Desktop.
Save gnarf/1e2f54371c22a05b0cd3c31fc8f2be46 to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 19 Part 2 - Console Golfing
L=JSON.stringify
Z="map"
A=(x,y)=>Math.abs(x-y)**2
X='+!!!+!!!++!!!!-!+!+!!!-!!!-+!!!!+!-!!-!+!!!!+!!++!!!+!!+!+!!!!-!!-+!!!-!-!!!-!!!+-!!!!-!-!-!!!+!!!--!!!!+!+!!+!-!!!!+!!+-!!!-!!-!-!!!!-!!--!!!+!!!-!+!+!!!+!!!++!!!!+!-!+!!!-!!!-+!!!!-!-!-!!!-!!!+-!!!!+!+!-!!!+!!!--!!'
for(T=[],x=0;x<X.length;)z=()=>(+eval(X[x++]+1)),T.push([[z(),z(),z()],[z(),z(),z()],[z(),z(),z()]])
M=(m,t)=>m.map((_,i)=>m[0]*t[i][0]+m[1]*t[i][1]+m[2]*t[i][2])
Q=[];(await(await fetch("https://adventofcode.com/2021/day/19/input")).text()).split('\n').map(i=>i.replace(/ (\d+)/,(_,n)=>(q=n,Q[n]=[])).replace(/(-?\d+),(-?\d+),(-?\d+)/,(_,x,y,z)=>Q[q].push([+x,+y,+z])))
D=D=>D.map(o=>D.map(p=>A(o[0],p[0])+A(o[1],p[1])+A(o[2],p[2])))
for(R=Q[0],R.l=[0,0,0];Q.filter(q=>!q.l).find(q=>{H=[]
for(x in (e=D(R)))for(B in (d=D(q)))d[B].filter(a=>e[x].includes(a)).length>11&&H.push([x,B])
if(H.length>11){t=T.find(t=>{V={}
H.map(([U,u])=>{p=R[U]
P=M(q[u],t)
V[U=L([p[0]-P[0],p[1]-P[1],p[2]-P[2]])]|=0,V[U]++})
for(U in V)if(V[U]>11)return 1})
if(t){U=JSON.parse(U)
q.map(P=>{P=M(P,t)
P=[U[0]+P[0],U[1]+P[1],U[2]+P[2]]
R.every(o=>L(o)!=L(P))&&R.push(P)})
return q.l=U}}});)N=0
D=(a,b)=>Math.abs(a[0]-b[0])+Math.abs(a[1]-b[1])+Math.abs(a[2]-b[2])
Q.map(q=>Q.map(q2=>N=Math.max(N,D(q.l,q2.l))))
N
@gnarf
Copy link
Author

gnarf commented Dec 19, 2021

Just because I'm sure someone is interested, here is what it looked like when I submitted the answer:

// comment out top line if you are re-running it... don't blast that fetch!
I=(await(await fetch("https://adventofcode.com/2021/day/19/input")).text())
scanners=[];
A=(x,y)=>Math.abs(x-y)**2
T=[
    [[1,0,0],[0,1,0],[0,0,1]],
    [[1,0,0],[0,0,-1],[0,1,0]],
    [[1,0,0],[0,-1,0],[0,0,-1]],
    [[1,0,0],[0,0,1],[0,-1,0]],

    [[0,-1,0],[1,0,0],[0,0,1]],
    [[0,0,1],[1,0,0],[0,1,0]],
    [[0,1,0],[1,0,0],[0,0,-1]],
    [[0,0,-1],[1,0,0],[0,-1,0]],

    [[-1,0,0],[0,-1,0],[0,0,1]],
    [[-1,0,0],[0,0,-1],[0,-1,0]],
    [[-1,0,0],[0,1,0],[0,0,-1]],
    [[-1,0,0],[0,0,1],[0,1,0]],

    [[0,1,0],[-1,0,0],[0,0,1]],
    [[0,0,1],[-1,0,0],[0,-1,0]],
    [[0,-1,0],[-1,0,0],[0,0,-1]],
    [[0,0,-1],[-1,0,0],[0,1,0]],

    [[0,0,-1],[0,1,0],[1,0,0]],
    [[0,1,0],[0,0,1],[1,0,0]],
    [[0,0,1],[0,-1,0],[1,0,0]],
    [[0,-1,0],[0,0,-1],[1,0,0]],

    [[0,0,-1],[0,-1,0],[-1,0,0]],
    [[0,-1,0],[0,0,1],[-1,0,0]],
    [[0,0,1],[0,1,0],[-1,0,0]],
    [[0,1,0],[0,0,-1],[-1,0,0]]
]

MUL=(m,t)=>[
    m[0]*t[0][0]+m[1]*t[1][0]+m[2]*t[2][0],
    m[0]*t[0][1]+m[1]*t[1][1]+m[2]*t[2][1],
    m[0]*t[0][2]+m[1]*t[1][2]+m[2]*t[2][2]
]
I.split('\n').map(line=>{
    line.replace(/r (\d+)/,(_,n)=>(scanner=n,scanners[n]=[]))
        .replace(/(-?\d+),(-?\d+),(-?\d+)/,(_,x,y,z)=>scanners[scanner].push([+x,+y,+z]))
});
D=scan=>scan.map(o=>scan.map(p=>A(o[0],p[0])+A(o[1],p[1])+A(o[2],p[2])))
resolved=scanners.shift()
while(scanners.find(scanner=>!scanner.resolved))
scanners.map((scanner,i)=> {
    if(scanner.resolved)return;
    console.log('test', scanner);
    matches=[];
    for(x in (e=D(resolved))){distA=e[x]
        for(B in (d=D(scanner))){distB=d[B]
            if(distB.filter(a=>distA.includes(a)).length>11){
                // console.log(distA,distB)
                matches.push([x,B])
            }
        }
    }
    if (matches.length>11)
    {

        t=T.find(t=>{
            results={}
            test = matches.map(([indexA,indexB])=>{
                pointA=resolved[indexA]
                pointB=MUL(scanner[indexB],t)
                dist=JSON.stringify([pointA[0]-pointB[0],pointA[1]-pointB[1],pointA[2]-pointB[2]])
                console.log('matching', pointA, pointB, dist);
                results[dist]|=0,results[dist]++
            })
            for(dist in results)if(results[dist]>11)return true;
        });
        if (t) {
            console.log('matches!', matches, t, dist)
            dist=JSON.parse(dist)
            scanner.map(point=>{
                point=MUL(point, t)
                point=[dist[0]+point[0],dist[1]+point[1],dist[2]+point[2]]
                resolved.every(op=>JSON.stringify(op)!=JSON.stringify(point))&&resolved.push(point)&&console.log('discovered', point)
            })
            scanner.loc=dist
        }
        scanner.resolved = true;
    }
})
bigdist=0
D=(a,b)=>Math.abs(a[0]-b[0])+Math.abs(a[1]-b[1])+Math.abs(a[2]-b[2])
scanners.map(scanner=>{bigdist=Math.max(bigdist,D(scanner.loc,[0,0,0]))
scanners.map(scanner2=>{bigdist=Math.max(bigdist,D(scanner.loc,scanner2.loc))
})})
bigdist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment