UNPKG

@assadk/aoc-utils

Version:

An Advent of Code utility library

3 lines (2 loc) 5.85 kB
function y(...t){let r=[...t].map(o=>o.length);return Math.max(...r)}function h(t,r,o=void 0){let n=y(t,r);return[...Array(n)].map((s,p)=>[t[p]?t[p]:t[p]=o,r[p]?r[p]:r[p]=o])}function l(t,r={}){let{separator:o=` `,trim:n=!0,filterEmpty:s=!0}=r;return t.split(o).map(f=>n?f.trim():f).filter(f=>s?f.length>0:!0)}function i(t,r,o,n={}){let s={writable:!1,enumerable:!1,configurable:!1,...n};Object.prototype.hasOwnProperty.call(t,r)||Object.defineProperty(t,r,{value:o,...s})}function d(t){if(!Number.isInteger(t)||t<=0)throw new TypeError("Chunk size must be a positive integer.");return this.length===0?[]:this.reduce((r,o,n)=>(n%t===0&&r.push(this.slice(n,n+t)),r),[])}function g(t,r=!0){return[...new Set(this)].map(n=>t.reduce((s,p)=>s+(r?p===n?1:0:p==n?1:0),0))}function A(t){return t instanceof RegExp?r=>t.test(String(r)):t instanceof Function?t:t&&typeof t=="object"&&!Array.isArray(t)?r=>!r||typeof r!="object"?!1:Object.keys(t).every(o=>r[o]===t[o]):Array.isArray(t)?r=>Array.isArray(r)?t.every(o=>r.includes(o)):!1:t==null?r=>r===t:r=>r==t}function x(t){let r=A(t);return this.reduce((n,s,p,f)=>n+(r(s,p,f)?1:0),0)}function b(){if(this.length<2)throw new Error("Array length must be greater than or equal to two.");return this.slice(0,-1).map((t,r)=>t-this[r+1])}function E(t,r=!1){if(this.length!==t.length)throw new Error("The two arrays are different lengths.");return this.map((o,n)=>Math.abs(r?t[n]-o:o-t[n]))}function S(){return this.length>0?this[0]:void 0}function u(){return[...new Set(this)].map(t=>{let r=this.reduce((o,n)=>t===n?o+1:o,0);return[t,r]})}function q(){return i(Array.prototype,"freq",u),Object.fromEntries(this.freq())}function O(){return i(Array.prototype,"freq",u),new Map(this.freq())}function P(){return this.length>0?this[this.length-1]:void 0}function w(){return this.length>0?this.reduce((t,r)=>t*r,1):0}function M(){return this.reduce((t,r)=>t+r,0)}function v(){if(!Array.isArray(this)||this.length===0||!this.every(Array.isArray))throw new TypeError("transpose can only be called on a 2D array.");return this[0].map((t,r)=>this.map(o=>o[r]))}function j(){return[...new Set(this)]}function I(){return this.length===0}i(Array.prototype,"isEmpty",I);function L(t,r=!1){if(this.isEmpty())return[];if(typeof t!="number"||t<=0||!Number.isInteger(t))throw new TypeError("windowSize must be a positive integer");if(typeof r!="boolean")throw new TypeError("wrap parameter is not a boolean");if(!r&&t>=this.length)return[this];if(r){if(r)return this.map((o,n)=>Array(t).fill(0).map((s,p)=>this[(n+p)%this.length]))}else return this.slice(0,this.length-t+1).map((o,n)=>this.slice(n,t+n))}var J={chunk:d,countMatches:x,countIn:g,deltas:b,displacement:E,first:S,freq:u,freqDict:q,freqMap:O,last:P,product:w,sum:M,transpose:v,uniq:j,window:L};function T(){Object.entries(J).forEach(([t,r])=>{i(Array.prototype,t,r)})}function C(){return[...this].map(([r,o])=>r*o)}var K={freqMapProduct:C};function F(){Object.entries(K).forEach(([t,r])=>{i(Map.prototype,t,r)})}function a(){return parseInt(this,10)%2===0}function m(){return parseInt(this,10)%2!==0}var Q={isEven:a,isOdd:m};function _(){Object.entries(Q).forEach(([t,r])=>{i(Number.prototype,t,r)})}function D(){return this.length>0?this[0]:void 0}function R(){return this.length>0?this[this.length-1]:void 0}function W(){return[...this].reverse().join("")}function N(){if(typeof this!="string")throw new TypeError("Expected a string");return parseInt(this,10)}function U(){return+this}var X={first:D,last:R,reverse:W,toInt:N,toNum:U,isEven:a,isOdd:m};function $(){Object.entries(X).forEach(([t,r])=>{i(String.prototype,t,r)})}function e(t,r,o){let n=Object.getOwnPropertyDescriptor(t,o);if(!n){console.warn(`The property "${o}" does not exist on the provided prototype.`);return}let{value:s,get:p,set:f}=n;if(typeof s=="function"){let c=t[o];Object.defineProperty(t,r,{value(...H){return c.apply(this,H)},configurable:!0,writable:!0,enumerable:!1})}else Object.defineProperty(t,r,{get(){return this[o]},set(c){this[o]=c},configurable:!0,enumerable:!1})}function B(){e(Array.prototype,"cat","concat"),e(Array.prototype,"cp","copyWithin"),e(Array.prototype,"e","every"),e(Array.prototype,"f","filter"),e(Array.prototype,"fd","find"),e(Array.prototype,"fin","findIndex"),e(Array.prototype,"fl","findLast"),e(Array.prototype,"flin","findLastIndex"),e(Array.prototype,"flm","flatMap"),e(Array.prototype,"fe","forEach"),e(Array.prototype,"has","includes"),e(Array.prototype,"iOf","indexOf"),e(Array.prototype,"j","join"),e(Array.prototype,"len","length"),e(Array.prototype,"lio","lastIndexOf"),e(Array.prototype,"m","map"),e(Array.prototype,"pp","pop"),e(Array.prototype,"ps","push"),e(Array.prototype,"r","reduce"),e(Array.prototype,"rv","toReversed"),e(Array.prototype,"sh","shift"),e(Array.prototype,"sl","slice"),e(Array.prototype,"sm","some"),e(Array.prototype,"st","sort"),e(Array.prototype,"ts","toString"),e(Array.prototype,"uns","unshift"),e(Array.prototype,"prod","product")}function G(){e(String.prototype,"lower","toLowerCase"),e(String.prototype,"upper","toUpperCase"),e(String.prototype,"ord","charCodeAt"),e(String.prototype,"has","includes"),e(String.prototype,"len","length"),e(String.prototype,"sp","split"),e(String.prototype,"tr","trim"),e(String.prototype,"trs","trimStart"),e(String.prototype,"tre","trimEnd"),e(String.prototype,"iOf","indexOf"),e(String.prototype,"ma","match"),e(String.prototype,"sw","startsWith"),e(String.prototype,"ew","endsWith"),e(String.prototype,"sb","substring"),e(String.prototype,"sl","slice"),e(String.prototype,"chr","charAt"),e(String.prototype,"re","replace"),e(String.prototype,"rea","replaceAll"),e(String.prototype,"ps","padStart"),e(String.prototype,"pe","padEnd"),e(String.prototype,"rep","repeat"),e(String.prototype,"rv","reverse")}T();F();$();_();var wr={maxLength:y,parseInput:l,zip:h};B();G();export{wr as AOC};