Skip to content

Commit

Permalink
2019 day 23 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 12, 2024
1 parent 5fb8bf6 commit b8e4f21
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 5 deletions.
300 changes: 298 additions & 2 deletions 2019/23/task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,303 @@
import { Queue } from '../../base'

export async function taskOne(input: string[]): Promise<void> {
console.log("Unimplemented");
const _list: Record<number, number> = {}
input[0].split(',').map(Number).forEach((i,idx) => _list[idx] = i)

const lists: Record<number, number>[] = 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<number>[] = Array.from({length: 50}, () => new Queue<number>())
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<void> {
console.log("Unimplemented");
const _list: Record<number, number> = {}
input[0].split(',').map(Number).forEach((i,idx) => _list[idx] = i)

const lists: Record<number, number>[] = 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<number>[] = Array.from({length: 50}, () => new Queue<number>())
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
}
}
2 changes: 1 addition & 1 deletion 2019/README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,7 +45,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3><a href="2019/README.md">2019</a></h3>

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

<h3><a href="2018/README.md">2019</a></h3>

Expand Down

0 comments on commit b8e4f21

Please sign in to comment.