From b8e4f210739dc3de5e21b63d7053a17fdfd0316e Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Thu, 12 Dec 2024 09:41:56 +0100 Subject: [PATCH] 2019 day 23 part 1 --- 2019/23/task.ts | 300 +++++++++++++++++++++++++++++++++++++++++++++++- 2019/README.md | 2 +- README.md | 4 +- 3 files changed, 301 insertions(+), 5 deletions(-) diff --git a/2019/23/task.ts b/2019/23/task.ts index 3d8caba..5ed2f2e 100644 --- a/2019/23/task.ts +++ b/2019/23/task.ts @@ -1,7 +1,303 @@ +import { Queue } from '../../base' + export async function taskOne(input: string[]): Promise { - console.log("Unimplemented"); + const _list: Record = {} + input[0].split(',').map(Number).forEach((i,idx) => _list[idx] = i) + + const lists: Record[] = Array.from({length:50}, () => JSON.parse(JSON.stringify(_list))) + const is: number[] = Array.from({length: 50}, () => 0) + const relBases: number[] = Array.from({length: 50}, () => 0) + const inputs: Queue[] = Array.from({length: 50}, () => new Queue()) + const outputs: number[][] = Array.from({length: 50}, () => []) + + for (let unit = 0; unit < 50; unit++) { + inputs[unit].push(unit) + } + + + while (true) { + for (let unit = 0; unit < 50; unit++) { + const i = is[unit] + if(lists[unit][i] != 99) { + const op = lists[unit][i] % 100 + switch (op) { + case 1: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c + b + is[unit]+=4 + break + + } + case 2: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c * b + is[unit]+=4 + break + } + case 3: { + if(inputs[unit].isEmpty()) { + lists[unit][getGoalIndex(i+1, getAtDigit(lists[unit][i], 100), unit)] = -1 + } else { + lists[unit][getGoalIndex(i+1, getAtDigit(lists[unit][i], 100), unit)] = inputs[unit].pop() + } + is[unit] += 2 + break + } + case 4: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + outputs[unit].push(c) + if (outputs[unit].length == 3) { + const goal = outputs[unit][0] + const x = outputs[unit][1] + const y = outputs[unit][2] + if (goal == 255) { + console.log(y) + return + } + outputs[unit] = [] + inputs[goal].push(x) + inputs[goal].push(y) + } + is[unit] += 2 + break + } + case 5: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + if (c != 0) { + is[unit] = b + } else { + is[unit] += 3 + } + break + } + case 6: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + if (c == 0) { + is[unit] = b + } else { + is[unit] += 3 + } + break + } + case 7: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c < b ? 1:0 + is[unit] += 4 + break + } + case 8: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c == b ? 1:0 + is[unit] += 4 + break + } + case 9: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + relBases[unit] += c + is[unit]+=2 + break + } + default: { + throw i + } + } + } + + + } + } + + function getVal(i: number, mode: number, unit: number) { + switch (mode) { + case 0: { + return lists[unit][lists[unit][i]] + } + case 1: { + return lists[unit][i] + } + case 2: { + return lists[unit][lists[unit][i]+relBases[unit]] + } + default: + throw "unknown Mode" + } + } + + function getGoalIndex(i: number, mode: number, unit: number) { + switch (mode) { + case 0: { + return lists[unit][i] + } + case 1: { + throw "not supported" + } + case 2: { + return lists[unit][i]+relBases[unit] + } + default: + throw "unknown Mode" + } + } + + function getAtDigit(num: number, digit: number) { + return Math.floor(num / digit) % 10 + } } export async function taskTwo(input: string[]): Promise { - console.log("Unimplemented"); + const _list: Record = {} + input[0].split(',').map(Number).forEach((i,idx) => _list[idx] = i) + + const lists: Record[] = Array.from({length:50}, () => JSON.parse(JSON.stringify(_list))) + const is: number[] = Array.from({length: 50}, () => 0) + const relBases: number[] = Array.from({length: 50}, () => 0) + const inputs: Queue[] = Array.from({length: 50}, () => new Queue()) + const outputs: number[][] = Array.from({length: 50}, () => []) + let NAT = [-1, -1] + let lastNumbers + + for (let unit = 0; unit < 50; unit++) { + inputs[unit].push(unit) + } + + + while (true) { + for (let unit = 0; unit < 50; unit++) { + const i = is[unit] + if(lists[unit][i] != 99) { + const op = lists[unit][i] % 100 + switch (op) { + case 1: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c + b + is[unit]+=4 + break + + } + case 2: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c * b + is[unit]+=4 + break + } + case 3: { + if(inputs[unit].isEmpty()) { + lists[unit][getGoalIndex(i+1, getAtDigit(lists[unit][i], 100), unit)] = -1 + } else { + lists[unit][getGoalIndex(i+1, getAtDigit(lists[unit][i], 100), unit)] = inputs[unit].pop() + } + is[unit] += 2 + break + } + case 4: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + outputs[unit].push(c) + if (outputs[unit].length == 3) { + const goal = outputs[unit][0] + const x = outputs[unit][1] + const y = outputs[unit][2] + outputs[unit] = [] + if (goal == 255) { + NAT = [x,y] + } else { + inputs[goal].push(x) + inputs[goal].push(y) + } + } + is[unit] += 2 + break + } + case 5: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + if (c != 0) { + is[unit] = b + } else { + is[unit] += 3 + } + break + } + case 6: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + if (c == 0) { + is[unit] = b + } else { + is[unit] += 3 + } + break + } + case 7: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c < b ? 1:0 + is[unit] += 4 + break + } + case 8: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + const b = getVal(i+2, getAtDigit(lists[unit][i], 1000), unit) + lists[unit][getGoalIndex(i+3, getAtDigit(lists[unit][i], 10000), unit)] = c == b ? 1:0 + is[unit] += 4 + break + } + case 9: { + const c = getVal(i+1, getAtDigit(lists[unit][i], 100), unit) + relBases[unit] += c + is[unit]+=2 + break + } + default: { + throw i + } + } + } + + + } + } + + function getVal(i: number, mode: number, unit: number) { + switch (mode) { + case 0: { + return lists[unit][lists[unit][i]] + } + case 1: { + return lists[unit][i] + } + case 2: { + return lists[unit][lists[unit][i]+relBases[unit]] + } + default: + throw "unknown Mode" + } + } + + function getGoalIndex(i: number, mode: number, unit: number) { + switch (mode) { + case 0: { + return lists[unit][i] + } + case 1: { + throw "not supported" + } + case 2: { + return lists[unit][i]+relBases[unit] + } + default: + throw "unknown Mode" + } + } + + function getAtDigit(num: number, digit: number) { + return Math.floor(num / digit) % 10 + } } \ No newline at end of file diff --git a/2019/README.md b/2019/README.md index 04e4cf5..e6aab0e 100644 --- a/2019/README.md +++ b/2019/README.md @@ -1,4 +1,4 @@ # 2019 -![](https://img.shields.io/badge/stars%20⭐-45-yellow) +![](https://img.shields.io/badge/stars%20⭐-46-yellow) Puzzles not solved during event. \ No newline at end of file diff --git a/README.md b/README.md index b2c532a..ec91906 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # My Advent of Code Solutions -![](https://img.shields.io/badge/Total%20stars%20⭐-469yellow) +![](https://img.shields.io/badge/Total%20stars%20⭐-470yellow) ## What language do I use? Whatever I feel like today. But it will most likely either be Java, Python, C++, Typescript or Haskell @@ -45,7 +45,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

2019

-![](https://img.shields.io/badge/stars%20⭐-45-yellow) +![](https://img.shields.io/badge/stars%20⭐-46yellow)

2019