Unable to load image

day 15 thread of aoc i guess lol: diamonds are forever

see leaderboard on geeses post

33
Jump in the discussion.

No email address required.

asscript:

type str = string;
type Arr<T> = Array<T>;

class Pnt {
  x: i32; y:i32;
  constructor(x:i32, y:i32) {this.x=x; this.y=y};
  toString():str { return `${this.x},${this.y}`};
};

function calcDst(a:Pnt, b:Pnt): i32 {
  return i32(Math.abs(a.x-b.x)) + i32(Math.abs(a.y-b.y));
}

class SnrDat {
  snrPnt: Pnt;
  bcnPnt: Pnt;
  dst: i32;
  constructor(sX:i32, sY:i32, bX:i32, bY:i32){
    this.snrPnt = new Pnt(sX, sY);
    this.bcnPnt = new Pnt(bX, bY);
    this.dst = calcDst(this.snrPnt, this.bcnPnt);
  };
  toString():str {
    return `S:${this.snrPnt} B:${this.bcnPnt} dst:${this.dst}`;
  };
  excludesPnt(p:Pnt): boolean {
    if (p.x === this.bcnPnt.x && p.y === this.bcnPnt.y) return false;
    const dst = calcDst(this.snrPnt,p);
    return dst<=this.dst;
  }
}

function setup(input:str): Array<SnrDat> {
  const strLines: Arr<str> = input.split('\n');
  return strLines.map<SnrDat>((l:str) => {
    const n = l.split(',').map((s:str) => i32.parse(s));
    const sX = n[0], sY = n[1], bX = n[2], bY = n[3]
    return new SnrDat(sX, sY, bX, bY);
  });
}

const p = new Pnt(0,0);
export function run(inputData:str, maxDir:str):void {
  const snrData = setup(inputData);
  const max = i32.parse(maxDir);
  while(true) {
    const search = snrData.findIndex(dat => dat.excludesPnt(p));
    if (search == -1) break;
    const excDat = snrData[search];
    p.x = excDat.snrPnt.x + (excDat.dst - i32(Math.abs(p.y - excDat.snrPnt.y))) + 1;
    if (p.x > max) {
      p.x = 0; p.y = p.y+1;
    }
    if (p.y > max) {
      break;
    }
  }

  console.log(
    (u64(p.x) * 4000000 + p.y).toString()
  );
};

calling typescript:

import * as fs from 'fs';
import {run} from '../build/release.js';

const input = fs.readFileSync(process.argv[2], 'utf-8').split('\n').map(l =>
  [...l.matchAll(/[x,y]=(-*\d+)+/g)].map(m => Number(m[1]))
);
run(
  input.map(l => l.join(',')).join('\n'),
  process.argv[3]
);
Jump in the discussion.

No email address required.

still unemployed then?

Jump in the discussion.

No email address required.

not yet!

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.