Skip to content

Instantly share code, notes, and snippets.

crazy4groovy

View GitHub Profile
@crazy4groovy
crazy4groovy / Dockerfile
Created Jun 10, 2020
Build a "distroless" NodeJS docker image
View Dockerfile
FROM node:12.18.0 AS build-env
ADD . /app
WORKDIR /app
RUN npm ci --only=production
# https://github.com/GoogleContainerTools/distroless
# - considered experimental and not recommended for production usage!
# https://github.com/GoogleContainerTools/distroless/blob/master/examples/nodejs/Dockerfile
FROM gcr.io/distroless/nodejs
COPY --from=build-env /app /app
View little-vdom-decompiled.js
/* eslint-disable no-unused-vars */
/* eslint-disable no-else-return */
// JSX constructor, similar to createElement()
export const h = (type, props, ...children) => {
return {
type,
// Props will be an object for components and DOM nodes, but a string for
// text nodes
props,
View Advent1-mass.js
function main(input) {
const lines = input.split('\n');
const results = lines.map(n => Math.floor(Number(n) / 3) - 2)
const total = results.reduce((sum, n) => sum + n, 0);
console.log(total); // <<<<<<<<<<<<<<<<<
}
main(`120150
105328
70481
@crazy4groovy
crazy4groovy / jsonblob.js
Last active Feb 12, 2020
API for jsonblob.com
View jsonblob.js
// https://jsonblob.com/
fetch = fecth || require('cross-fetch')
async function jsonblob(
json /* : Object */,
method /* : string */,
id /* : string */
) /* : Promise */ {
const isUpsert = method.toUpperCase() !== 'GET' && method.toUpperCase() !== 'DELETE'
@crazy4groovy
crazy4groovy / numberOfCarryOperations.js
Last active Aug 28, 2019
Count how many times a carry operation is performed while adding two numbers
View numberOfCarryOperations.js
function numberOfCarryOperations(a, b) {
const aDigits = String(a).split('').reverse()
const bDigits = String(b).split('').reverse()
const maxDigits = aDigits.length > bDigits.length ? aDigits : bDigits
const minDigits = maxDigits === aDigits ? bDigits : aDigits
/// console.log({a, b, maxDigits, minDigits})
let carry = 0
const carryCount = maxDigits.reduce((c, d, i) => {
const dA = Number(d) || 0
View cache_stampede.js
const refPromises = {};
const workflowWithOneCacheMiss = async (key) => {
const dataFromCache = await getCache(key);
if (dataFromCache !== undefined) {
return dataFromCache;
}
if (!refPromises[key]) {
@crazy4groovy
crazy4groovy / sort-by.js
Last active Nov 11, 2019
custom array sort, by field path and weighted values
View sort-by.js
function getVal(obj, fields) {
if (!(obj && fields[0])) return obj
return getVal(obj[fields.shift()], fields)
}
export const customSort = (field, byVals) => {
let sorter
if (byVals) {
const sortByWeight = {}
byVals.forEach((val, weight) => {
@crazy4groovy
crazy4groovy / intersection-lines.js
Created Aug 8, 2019
How many straight lines, starting from point {x: 0, y: 0}, are needed to intersect with all the points in a given array?
View intersection-lines.js
function normalize({x, y}) {
const nx = x / Math.abs(x)
const ny = y / Math.abs(x)
return nx + '|' + ny
}
/*
console.log(normalize({x: 1, y: 2}))
console.log(normalize({x: 2, y: 4}))
@crazy4groovy
crazy4groovy / splittable-sort-array.js
Created Aug 7, 2019
How many groups (more than 1 number per group) of an integer list can be split out, each sorted, and concatenated together to produce the same result as sorting the list?
View splittable-sort-array.js
function solution(A) {
let index = 0
let results = 0
let remainder = 0
let number
while (index < A.length) {
number = A[index]
for (let i = index + 1; i < A.length; i++) {
if (number > A[i]) {
index = i
View min-positive-int.js
// given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A
function solution(A) {
const sortedUnique = [...new Set([...A])]
.filter((n) => (n > 0))
.sort((a, b) => (a - b))
if (sortedUnique.length === 0) return 1
let invalidIdx = sortedUnique.findIndex((n, i) => n !== (i + 1))
if (invalidIdx === -1) return sortedUnique[sortedUnique.length - 1] + 1
You can’t perform that action at this time.