Skip to content

Instantly share code, notes, and snippets.

@torbiak
Created March 3, 2025 21:11
Show Gist options
  • Select an option

  • Save torbiak/9b8d4d60575ad48085ab48f4e3a4b10f to your computer and use it in GitHub Desktop.

Select an option

Save torbiak/9b8d4d60575ad48085ab48f4e3a4b10f to your computer and use it in GitHub Desktop.

Revisions

  1. torbiak created this gist Mar 3, 2025.
    15 changes: 15 additions & 0 deletions day1.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    const fs = require('node:fs');

    const left: number[] = [], right: number[] = [];
    const input = fs.readFileSync('input', {encoding: 'utf8'});
    for (const line of input.split(/\n/)) {
    if (line === '') continue;
    const [l, r] = line.split(/\s+/);
    left.push(Number(l));
    right.push(Number(r));
    }
    left.sort();
    right.sort();

    const distance = left.reduce((dist, l, i) => dist + Math.abs(l - right[i]));
    console.log(distance);