mirror of
https://github.com/Aviortheking/advent-of-code.git
synced 2025-04-22 10:52:11 +00:00
feat: make advance on part 2
Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
parent
93f0a58796
commit
77088913bf
@ -1,37 +1,71 @@
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
const input = fs.readFileSync(__dirname + '/input.txt').toString()
|
const input = fs.readFileSync(__dirname + '/input.txt').toString()
|
||||||
.split(',')
|
.split('\n')
|
||||||
.map((it) => parseInt(it))
|
.map((it) => it.split('|')[1].split(' '))
|
||||||
.sort((a, b) => a - b)
|
|
||||||
|
|
||||||
function costToLocation(values: Array<number>, value: number) {
|
function isOne(str: string): boolean {
|
||||||
return values.reduce((p, c) => {
|
return str.length === 2
|
||||||
let diff = Math.abs(c - value)
|
|
||||||
let mod = 0
|
|
||||||
while (diff > 0) {
|
|
||||||
mod += diff
|
|
||||||
diff--
|
|
||||||
}
|
|
||||||
return p + mod
|
|
||||||
}, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const min = input[0]
|
function isFour(str: string): boolean {
|
||||||
const max = input[input.length - 1]
|
return str.length === 4
|
||||||
|
|
||||||
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 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(
|
console.log(
|
||||||
"least moves:", cost
|
"Result:", count
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user