Unable to load image

AoC Day 14 : Sands of time

I won't paste my code yet since my parser just does the needful, and I somehow manage to get overlapping grains of sand, though that doesn't matter in the end.

I'm also seeing a lot of pythonistas having high runtime (1-10s+ and more) and I'm here with the bruteforce grain-by-grain taking 150ms for part 2 (including drawing the final graph lmao). Matlab chads can't stop winning :gigachad2:

EDIT : part 2 webm

16
Jump in the discussion.

No email address required.

little late guys:

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

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

const AIR: u8 = 0;
const ROCK: u8 = 1;
const SAND: u8 = 2;

let shiftX: u16 = 490;
let sandX: u16 = 500 - shiftX;
let depth: u16 = 13;
let width: u16 = 20;
let grid: u8[][] = [];
let maxY:u16=0, minX:u16=1000, maxX:u16=0;

function setup(input:str):void {
  const strLines: Arr<str> = input.split('\n');
  const pntLines
    = strLines.map<Arr<Pnt>>((l:str) => l.split(' -> ').map<Pnt>(
      (ln:str) => {
        const p = ln.split(',').map((p:str)=>u16.parse(p));
        return new Pnt(p[0],p[1]);
      }
    ));
  // console.log(pntLines.map(
  //   (pln:Arr<Pnt>)=>pln.toString()).join('\n')
  // );
  pntLines.forEach((pln:Arr<Pnt>) => {
    pln.forEach((p:Pnt) => {
      if (p.y > maxY) maxY=p.y;
      if (p.x < minX) minX=p.x;
      if (p.x > maxX) maxX=p.x;
    })
  });

  console.log(`${minX} ${maxX} ${maxY}`);

  depth = maxY+3;
  width = depth*2;
  shiftX = 500 - u16(Math.floor(width/2));
  sandX = 500 - shiftX;
  grid = new Array<u8>(depth).map(
    () => new Array<u8>(width).fill(AIR)
  );

  console.log(`${depth} ${width} ${shiftX} ${sandX}`)

  pntLines.forEach((pln:Arr<Pnt>) => {
    let curPnt:Pnt = pln[0];
    for (let i = 0; i < pln.length; i++) {
      const p:Pnt = pln[i];
      const dx:i16 = i16(curPnt.x) - i16(p.x);
      const dy:i16 = i16(curPnt.y) - i16(p.y);
      // console.log(`---- ${dx} ${dy}`)
      for (let x:u16 = p.x;
        dx<0 ? x>=curPnt.x : x<=curPnt.x;
        dx<0 ? x-- : x++) {
        for (let y:u16 = p.y; 
          dy<0 ? y>=curPnt.y : y<=curPnt.y;
          dy<0 ? y-- : y++) {
          grid[y][x-shiftX] = ROCK;
          // console.log(`${y} ${x-SHIFT_X}`)
        }
      }
      curPnt=p;
    }
  });

  for (let x:u16 = 0; x < width; x++) {
    grid[maxY+2][x] = ROCK;
  }
}

function print(): void {
  console.log(grid.map((row:Arr<u8>) => row.map(
    (c:u8) => c===ROCK?'#' : c===SAND?'o' : '.').join('')
  ).join('\n'));
}

function doOneSand(): boolean {
  let snd = new Pnt(sandX, 0);

  if (grid[0][500-shiftX]) {
    console.log('sand creation blocked');
    return false;
  };

  while (true) {
    if (snd.y > depth-1 || snd.x < 0 || snd.x >= width-1) {
      console.log('abyss reached');
      return false;
    }
    if (!grid[snd.y+1][snd.x]) {
      snd.y = snd.y+1;
    } else if (!grid[snd.y+1][snd.x-1]) {
      snd.y = snd.y+1;
      snd.x = snd.x-1;
    } else if (!grid[snd.y+1][snd.x+1]) {
      snd.y = snd.y+1;
      snd.x = snd.x+1;
    } else {
      break;
    }
  }

  grid[snd.y][snd.x] = SAND;
  return true;
};

export function run(input: str):void {
  setup(input);

  let numSnd: u32 = 0;
  while(doOneSand()) numSnd++;
  console.log(numSnd.toString());

  print();
};

too lazy to remove debug code

Jump in the discussion.

No email address required.

All them words won't bring your pa back.

Jump in the discussion.

No email address required.

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