mirror of
https://github.com/Aviortheking/advent-of-code.git
synced 2025-04-22 10:52:11 +00:00
38 lines
698 B
TypeScript
38 lines
698 B
TypeScript
import fs from 'fs'
|
|
|
|
const input = fs.readFileSync(__dirname + '/input.txt').toString()
|
|
.split(',')
|
|
.map((it) => parseInt(it))
|
|
.sort((a, b) => a - b)
|
|
|
|
function costToLocation(values: Array<number>, value: number) {
|
|
return values.reduce((p, c) => {
|
|
let diff = Math.abs(c - value)
|
|
let mod = 0
|
|
while (diff > 0) {
|
|
mod += diff
|
|
diff--
|
|
}
|
|
return p + mod
|
|
}, 0)
|
|
}
|
|
|
|
const min = input[0]
|
|
const max = input[input.length - 1]
|
|
|
|
let cost = Infinity
|
|
|
|
// this is so ugy it hurts me
|
|
// BUT it works like a charm!
|
|
for (let index = min; index < max; index++) {
|
|
const localCost = costToLocation(input, index)
|
|
if (localCost < cost) {
|
|
cost = localCost
|
|
}
|
|
|
|
}
|
|
|
|
console.log(
|
|
"least moves:", cost
|
|
)
|