Skip to content

Instantly share code, notes, and snippets.

@remy
Created December 3, 2025 17:11
Show Gist options
  • Select an option

  • Save remy/24539571a53d74b21c15e0c6925fcd57 to your computer and use it in GitHub Desktop.

Select an option

Save remy/24539571a53d74b21c15e0c6925fcd57 to your computer and use it in GitHub Desktop.
jq -R -rn -f ./3-b.jq ./3.input
def findLargest($n):
. as $_ |
.[0:(if $n == 1 then length else 1-$n end)] | max as $max |
$_ | index($max) as $index |
if ($n - 1) > 0 then
[$max] + ($_[$index+1:] | findLargest($n-1))
else
[$max]
end
;
[inputs | split("") | map(tonumber) | findLargest(12) | join("") | tonumber] | add
@remy
Copy link
Author

remy commented Dec 3, 2025

This is the code translated to JS:

import { readFile } from 'fs/promises';

function findLargest(n) {
  return (_) => {
    const max = Math.max(..._.slice(0, n == 1 ? _.length : 1 - n));
    const trimmed = _.slice(_.indexOf(max) + 1);

    if (n - 1) {
      return [max].concat(findLargest(n - 1)(trimmed));
    } else {
      return [max];
    }
  };
}

const input = (await readFile('./3.sample', 'utf8'))
  .split('\n')
  .map((line) => line.trim().split('').map(Number))
  .map(findLargest(12))
  .map((_) => parseInt(_.join(''), 10));

console.log(input.reduce((a, b) => a + b, 0));

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