From 77088913bf0c7ba50a359aa655ef1f56155a0c74 Mon Sep 17 00:00:00 2001 From: Florian BOUILLON Date: Tue, 5 Dec 2023 02:45:25 +0100 Subject: [PATCH] feat: make advance on part 2 Signed-off-by: Florian BOUILLON --- 2021/day-8/part-2.ts | 82 +++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/2021/day-8/part-2.ts b/2021/day-8/part-2.ts index f2a200a..daa1e11 100644 --- a/2021/day-8/part-2.ts +++ b/2021/day-8/part-2.ts @@ -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, 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] +function isFour(str: string): boolean { + return str.length === 4 +} -let cost = Infinity +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] { + let finalVal: [string, string, string, string, string, string, string] = ['', '', '', '', '', '', ''] + for (const num of line) { + if (isEight(num)) continue -// 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 } - + return finalVal } +function decode(line: Array): number { + let topRight: Array = [] + let bottomRight: Array = [] + + 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 )