feat: make advance on part 2

Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
Florian Bouillon 2023-12-05 02:45:25 +01:00
parent 93f0a58796
commit 77088913bf

View File

@ -1,37 +1,71 @@
import fs from 'fs'
const input = fs.readFileSync(__dirname + '/input.txt').toString()
.split(',')
.map((it) => parseInt(it))
.sort((a, b) => a - b)
.split('\n')
.map((it) => it.split('|')[1].split(' '))
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)
function isOne(str: string): boolean {
return str.length === 2
}
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
function isFour(str: string): boolean {
return str.length === 4
}
function isSeven(str: string): boolean {
return str.length === 3
}
function isEight(str: string): boolean {
return str.length === 8
}
function is1478(str: string): boolean {
return isOne(str) || isEight(str) || isSeven(str) || isFour(str)
}
/**
*
* @param line the line to decipher
* @returns [top, topleft, topright, middle, bottomleft, bottomright, bottom] containing the letter assigned
*/
function decodeMapping(line: Array<string>): [string, string, string, string, string, string, string] {
let finalVal: [string, string, string, string, string, string, string] = ['', '', '', '', '', '', '']
for (const num of line) {
if (isEight(num)) continue
}
return finalVal
}
function decode(line: Array<string>): number {
let topRight: Array<string> = []
let bottomRight: Array<string> = []
let finalNum: string = ''
// detect unique ones
// detect and add globally
for (const num of line) {
if (isEight(num)) finalNum += '8'
}
return 0
}
let count = 0
for (const line of input) {
for (const item of line) {
if (is1478(item)) count++
}
}
console.log(
"least moves:", cost
"Result:", count
)