Created
December 15, 2018 10:54
-
-
Save jemmyw/6bcac7eead3e359a449a9ffa331b2c69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gs = 300; | |
const serial = 4151; | |
function value(x: number, y: number) { | |
const rackId = x + 10; | |
const s2 = rackId * y; | |
const s3 = s2 + serial; | |
const s4 = s3 * rackId; | |
const s5 = s4 < 100 ? 0 : Number(String(s4).slice(-3, -2)); | |
const s6 = s5 - 5; | |
return s6; | |
} | |
type GridMap = Map<number, number>; | |
function key(x: number, y: number) { | |
return x >= y ? | |
x * x + x + y : | |
x + y * y; | |
} | |
const rngS = [...Array(gs - 1).keys()].map(k => k + 1); | |
const grid = rngS.reduce((acc: GridMap, y) => { | |
return rngS.reduce((acc: GridMap, x) => { | |
acc.set(key(x, y), value(x, y)); | |
return acc; | |
}, acc) | |
}, new Map()) as GridMap; | |
function gv(x, y) { return grid.get(key(x, y)) } | |
function vs(x: number, y: number, s: number) { | |
const rng = [...Array(s).keys()]; | |
return rng.reduce((acc, vy) => { | |
return rng.reduce((acc, vx) => { | |
return acc + gv(x + vx, y + vy); | |
}, acc); | |
}, 0) | |
} | |
function v3(x: number, y: number) { | |
return vs(x, y, 3); | |
} | |
function largestReducer(s: number) { | |
const firstT: [[number, number], number] = [[1, 1], vs(1, 1, s)]; | |
const rng = [...Array(gs - s).keys()].map(k => k + 1); | |
return rng.reduce((acc, y) => { | |
return rng.reduce((acc, x) => { | |
const v = vs(x, y, s); | |
if (v > acc[1]) { | |
return [[x, y], v]; | |
} | |
return acc; | |
}, acc) | |
}, firstT) | |
} | |
// Part 1 | |
const result3 = largestReducer(3); | |
console.log(result3); | |
// Part 2 | |
const sr = [...Array(gs - 1).keys()].map(k => k + 1); | |
const firstT: [[number, number], number, number] = [[1, 1], 1, vs(1, 1, 1)]; | |
const result = sr.slice(0, 20).reduce((acc, s) => { | |
const lr = largestReducer(s); | |
console.log('Checking size', s, lr); | |
return lr[1] > acc[2] ? [lr[0], s, lr[1]] : acc; | |
}, firstT) | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment