Skip to content

Instantly share code, notes, and snippets.

@tluyben
Last active December 7, 2021 06:37
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 tluyben/304da8d487fee0f8f7431d0129c1cf22 to your computer and use it in GitHub Desktop.
Save tluyben/304da8d487fee0f8f7431d0129c1cf22 to your computer and use it in GitHub Desktop.
import { accessSync, promises as fs } from 'fs';
(async()=>{
const input = await fs.readFile("./interpretertests/aoc_21_7_input.txt", 'utf8')
const positions = input.split('\n')[0].split(',').map(Number)
const max = Math.max(...positions)
let horizontals = positions.reduce((acc, cur) => { acc[cur] = 0; return acc }, [])
let min = 900719925474099
for(let h in horizontals) {
for(let i of positions) {
horizontals[h] += Math.abs(i-h)
}
if (horizontals[h]<min)
min = horizontals[h]
}
console.table(min)
const sum = (n) => {
return ((n+1)*n)/2
}
min = 900719925474099
horizontals = [...Array(max)].map(x=>0)
for(let h in horizontals) {
for(let i of positions) {
horizontals[h] += sum(Math.abs(i-h))
}
if (horizontals[h]<min)
min = horizontals[h]
}
console.table(min)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment