Skip to content

Instantly share code, notes, and snippets.

@gjuchault
Last active October 5, 2019 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjuchault/a47f8ca23a4518a88a0b30e9042ed6ef to your computer and use it in GitHub Desktop.
Save gjuchault/a47f8ca23a4518a88a0b30e9042ed6ef to your computer and use it in GitHub Desktop.
How many triangles?
const { equal } = require('assert')
const UNIT_SIDE = 28
const getNumberOfTrianglesOfLengthOne = (lineLength = UNIT_SIDE) => lineLength ** 2
equal(1, getNumberOfTrianglesOfLengthOne(1))
equal(4, getNumberOfTrianglesOfLengthOne(2))
equal(9, getNumberOfTrianglesOfLengthOne(3))
const getAllTriangles = (fullLineLength = UNIT_SIDE, log = console.log) => {
let sum = getNumberOfTrianglesOfLengthOne(fullLineLength)
if (typeof log !== 'function') log = () => {}
for (let lengthOfTriangle = 2; lengthOfTriangle <= fullLineLength; lengthOfTriangle++) {
log('check for', lengthOfTriangle)
let sumForLengthOfTriangle = 0
let sumForLengthOfTriangleReversed = 0
// triangles pointing up
let startLine = 0
let stopLine = fullLineLength - lengthOfTriangle
for (; startLine <= stopLine; startLine++) {
const lengthOfThisLine = fullLineLength - startLine;
sumForLengthOfTriangle += lengthOfThisLine - (lengthOfTriangle - 1)
log(' → I can put', lengthOfThisLine - (lengthOfTriangle - 1), 'of size', lengthOfTriangle, 'on a line sized', lengthOfThisLine)
}
log(' → I can put', sumForLengthOfTriangle, 'of length', lengthOfTriangle)
sum += sumForLengthOfTriangle
// triangles pointing downx
startLine = lengthOfTriangle - 1
stopLine = fullLineLength - lengthOfTriangle - 1
for (; startLine <= stopLine; startLine++) {
const lengthOfThisLine = fullLineLength - startLine;
sumForLengthOfTriangleReversed += lengthOfThisLine - lengthOfTriangle
log(' → I can put', lengthOfThisLine - lengthOfTriangle, 'reversed of size', lengthOfTriangle, 'on a line sized', lengthOfThisLine)
}
log(' → I can put', sumForLengthOfTriangleReversed, 'reversed of length', lengthOfTriangle)
sum += sumForLengthOfTriangleReversed
}
return sum
}
// checked by hand (cf. helper.png)
equal(1, getAllTriangles(1, null))
equal(5, getAllTriangles(2, null))
equal(13, getAllTriangles(3, null))
equal(27, getAllTriangles(4, null))
equal(48, getAllTriangles(5, null))
equal(78, getAllTriangles(6, null))
// more checks I found online after solution
equal(118, getAllTriangles(7, null))
equal(170, getAllTriangles(8, null))
console.log('getAllTriangles(1) =', getAllTriangles(1, null))
console.log('getAllTriangles(2) =', getAllTriangles(2, null))
console.log('getAllTriangles(3) =', getAllTriangles(3, null))
console.log('getAllTriangles(4) =', getAllTriangles(4, null))
console.log('getAllTriangles(5) =', getAllTriangles(5, null))
console.log('getAllTriangles(6) =', getAllTriangles(6, null))
console.log('getAllTriangles(28) =', getAllTriangles(28, null))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment