From a2bdb41a434d65346d328415c93a95fd0afa68c6 Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Sat, 23 Dec 2023 17:14:06 +0100 Subject: [PATCH] 2021 18 --- 2021/18/task.ts | 194 ++++++++---------------------------------------- 2021/README.md | 2 +- README.md | 2 +- 3 files changed, 33 insertions(+), 165 deletions(-) diff --git a/2021/18/task.ts b/2021/18/task.ts index a5db61e..08f1b23 100644 --- a/2021/18/task.ts +++ b/2021/18/task.ts @@ -2,13 +2,21 @@ export async function taskOne(input: string[]): Promise { let sum = input[0] for (let i = 1; i < input.length; i++) { sum = add(sum, input[i]) - console.log("sum" + sum) - console.log(" ") } + console.log(magnitude(sum)) } export async function taskTwo(input: string[]): Promise { - console.log("Unimplemented"); + let max = 0 + for (let i = 0; i < input.length; i++) { + for (let j = 0; j < input.length; j++) { + const mag = magnitude(add(input[i], input[j])) + if (mag > max) { + max = mag + } + } + } + console.log(max) } const NUMS = [0,1,2,3,4,5,6,7,8,9].map(i=>i.toString()) @@ -21,33 +29,34 @@ function reduce(s: string) { let didAction = false let _i = 0 do { - console.log(s) - _i++ - if (_i > 20 ) return "" let open = 0 didAction = false for (let i = 0; i < s.length; i++) { if (s[i] == '[') open++ if (s[i] == ']') open-- if (open > 4) { - console.log(1) const pairEnd = s.indexOf(']', i+1) const toMuch = s.slice(i,pairEnd+1) - //console.log(toMuch) const r = /\[([0-9]+),([0-9]+)\]/.exec(toMuch) if (r == null) throw toMuch for (let j = pairEnd+1; j < s.length; j++) { if (NUMS.includes(s[j])) { - const goal = (parseInt(s[j]) + parseInt(r[2])).toString() - s = s.slice(0, j) + goal + s.slice(j+1) + const st = j + let en = j + while(NUMS.includes(s[en])) {en++} + const goal = (parseInt(s.slice(st,en)) + parseInt(r[2])).toString() + s = s.slice(0, j) + goal + s.slice(en) break; } } s = s.slice(0, i) + '0' + s.slice(pairEnd+1) for (let j = i-1; j >= 0; j--) { if (NUMS.includes(s[j])) { - const goal = (parseInt(s[j]) + parseInt(r[1])).toString() - s = s.slice(0, j) +goal+s.slice(j+1) + const en = j + 1 + let st = j + while(NUMS.includes(s[st])) {st--} + const goal = (parseInt(s.slice(st+1, en)) + parseInt(r[1])).toString() + s = s.slice(0, st+1) +goal+s.slice(en) break; } } @@ -66,168 +75,27 @@ function reduce(s: string) { while(NUMS.includes(s[j])) {j++} const en = j if (en-st > 1) { - console.log(2) const num = parseInt(s.slice(st,en)) - //console.log(num, s.slice(0, st), s.slice(en)) s = s.slice(0,st) + '['+Math.floor(num/2).toString()+','+ Math.ceil(num/2).toString() + ']'+ s.slice(en) didAction = true break; } } - //console.log("2", s) } while(didAction) return s } - - -type Pair = { - left: Pair|number - right: Pair|number - parent: Pair - depth: number -} -/* -function add(a:Pair, b:Pair) { - console.log("add") - function increaseD(x: Pair) { - x.depth++ - if ((x.left as Pair).depth != undefined) increaseD(x.left as Pair) - if ((x.right as Pair).depth != undefined) increaseD(x.right as Pair) - } - const temp: Pair = {left:a,right:b, depth:0, parent: {} as Pair} - a.parent = temp - b.parent = temp - console.log("added to: ", toString(temp)) - increaseD(a) - increaseD(b) - console.log("increased depth") - - function split(x: Pair): boolean { - if((x.left as Pair).depth != undefined) { - if (split(x.left as Pair)) return true - } else if ((x.left as number) > 9) { - const num = x.left as number - x.left = { - left: Math.floor(num/2), - right: Math.ceil(num/2), - parent: x, - depth: x.depth + 1 - } - return true - } - if((x.right as Pair).depth != undefined) { - if (split(x.right as Pair)) return true - } else if ((x.right as number) > 9) { - const num = x.right as number - x.left = { - left: Math.floor(num/2), - right: Math.ceil(num/2), - parent: x, - depth: x.depth + 1 - } - return true - } - return false - } - function explode(x: Pair): boolean { - if (x.depth >= 3) { - if (x.parent?.left == x) { - const newR = (x.parent.right as number) + (x.right as number) - x.parent.right = newR - - if (x.parent.parent.left != x.parent) { - const newL = (x.parent.parent.left as number) + (x.left as number) - x.parent.parent.left = newL - } else if (x.parent.parent.parent.left != x.parent.parent) { - const newL = (x.parent.parent.parent.left as number) + (x.left as number) - x.parent.parent.parent.left = newL - } - } else { - const newL = (x.parent.left as number) + (x.left as number) - x.parent.left = newL - - if (x.parent.parent.right != x.parent) { - const newR = (x.parent.parent.right as number) + (x.right as number) - x.parent.parent.right = newR - } else if (x.parent.parent.parent?.right != x.parent?.parent) { - const newR = (x.parent.right as number) + (x.right as number) - x.parent.parent.parent.right = newR - } - } - return true - } - if ((x.left as Pair) != undefined) { - if (explode(x.left as Pair)) { - return true - } - } - if ((x.right as Pair) != undefined) { - if (explode(x.right as Pair)) { - return true - } - } - return false - } - let didAction = true - while(didAction) { - didAction = false - if (explode(temp)) { - didAction = true - console.log("explode", toString(temp)) - break - } - if (split(temp)) { - console.log("split") - didAction = true - } - } - - return temp -} - -function parse(input: string) { - type _P = [_P|number, _P|number] - const inp = JSON.parse(input) as _P - function par(x: _P, d: number, p?: Pair) { - let left = x[0] - let right = x[1] - const temp = { - depth: d, - parent: p, - left: undefined as unknown, - right: undefined as unknown - } - - if ((left as _P).length != undefined) { - temp.left = par(left as _P, d+1, temp as Pair) - } else { - temp.left = left - } - if ((right as _P).length != undefined) { - temp.right = par(right as _P, d+1, temp as Pair) +function magnitude(num: string) { + type Pair =number|[Pair,Pair] + const snail = JSON.parse(num) as Pair + function mag(p: Pair): number { + if ((p as any[]).length != undefined) { + const _p = p as [Pair, Pair] + return 3*mag(_p[0]) + 2*mag(_p[1]) } else { - temp.right = right + return p as number } - return temp as Pair - } - return par(inp, 0, {} as Pair) -} - -function toString(p: Pair): string { - let temp = '[' - if ((p.left as Pair).left != undefined) { - temp += toString(p.left as Pair) - } else { - temp += p.left - } - temp += ',' - if ((p.right as Pair).left != undefined) { - temp += toString(p.right as Pair) - } else { - temp += p.right } - temp += ']' - return temp -}*/ \ No newline at end of file + return mag(snail) +} \ No newline at end of file diff --git a/2021/README.md b/2021/README.md index bafd1a3..982e774 100644 --- a/2021/README.md +++ b/2021/README.md @@ -1,4 +1,4 @@ # 2021 -![](https://img.shields.io/badge/stars%20⭐-24-yellow) +![](https://img.shields.io/badge/stars%20⭐-36-yellow) Puzzles not solved during event. \ No newline at end of file diff --git a/README.md b/README.md index 20c040d..27f3434 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

2021

-![](https://img.shields.io/badge/stars%20⭐-24-yellow) +![](https://img.shields.io/badge/stars%20⭐-36-yellow) ## Try it yourself https://adventofcode.com