Skip to content

Commit

Permalink
2016 8,9,10
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed May 9, 2024
1 parent 4f58731 commit ab42164
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 1 deletion.
1 change: 0 additions & 1 deletion 2016/08/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export async function taskTwo(input: string[]): Promise<void> {
}
console.log(r)
}
// ZJHRKCPLYJ
}

function perform(input: string[]) {
Expand Down
29 changes: 29 additions & 0 deletions 2016/09/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFile } from "fs/promises";
import { taskOne, taskTwo } from "./task";

async function main() {
let lastArgument = process.argv.pop() as string;
let taskNumber = 1;
let isTest = false;

if (lastArgument === "test") {
isTest = true;
taskNumber = parseInt(process.argv.pop() as string);
} else {
taskNumber = parseInt(lastArgument)
}

const fileToLoad = isTest ? "test.in" : "solve.in";
const fileContents = await readFile(fileToLoad, "utf-8")

const lines = fileContents.split("\n");

if (taskNumber === 1) {
await taskOne(lines);
}
if (taskNumber === 2) {
await taskTwo(lines);
}
}

void main();
62 changes: 62 additions & 0 deletions 2016/09/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export async function taskOne(_input: string[]): Promise<void> {
let len = 0
let i = 0
const input = _input[0].replace(' ', '')
//console.log(input)
while(i < input.length) {
if (input[i] == '(') {
let first = ''
let second = ''
i++
while(input[i] != 'x') {
first += input[i]
i++
}
i++
while(input[i] != ')') {
second += input[i]
i++
}
//console.log(input.substring(i+1), first, second, len)
len += parseInt(first) * parseInt(second)
i += parseInt(first)
//console.log(input.substring(i+1), len)
//console.log('')
} else {
len++
}
i++
}
console.log(len)
}

export async function taskTwo(input: string[]): Promise<void> {
const inp = input[0].replace(' ', '')
console.log(nested(inp))
function nested(input: string): number {
let len = 0
let i = 0
while(i < input.length) {
if (input[i] == '(') {
let first = ''
let second = ''
i++
while(input[i] != 'x') {
first += input[i]
i++
}
i++
while(input[i] != ')') {
second += input[i]
i++
}
len += nested(input.substring(i+1, i+1+parseInt(first))) * parseInt(second)
i += parseInt(first)
} else {
len++
}
i++
}
return len
}
}
29 changes: 29 additions & 0 deletions 2016/10/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFile } from "fs/promises";
import { taskOne, taskTwo } from "./task";

async function main() {
let lastArgument = process.argv.pop() as string;
let taskNumber = 1;
let isTest = false;

if (lastArgument === "test") {
isTest = true;
taskNumber = parseInt(process.argv.pop() as string);
} else {
taskNumber = parseInt(lastArgument)
}

const fileToLoad = isTest ? "test.in" : "solve.in";
const fileContents = await readFile(fileToLoad, "utf-8")

const lines = fileContents.split("\n");

if (taskNumber === 1) {
await taskOne(lines);
}
if (taskNumber === 2) {
await taskTwo(lines);
}
}

void main();
164 changes: 164 additions & 0 deletions 2016/10/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
export async function taskOne(input: string[]): Promise<void> {
let maxID = 0
input.forEach(i => {
let r = /value ([0-9]+) goes to bot ([0-9]+)/.exec(i)
if (r) {
maxID = Math.max(maxID, parseInt(r[2]))
return
}
r = /bot ([0-9]+) gives low to (bot|output) ([0-9]+) and high to (bot|output) ([0-9]+)/.exec(i)
if (r) {
maxID = Math.max(maxID, parseInt(r[1]))
if (r[2] == 'bot') maxID = Math.max(maxID, parseInt(r[3]))
if (r[4] == 'bot') maxID = Math.max(maxID, parseInt(r[5]))
return
}
throw i
})
const numbers: [number, number][] = Array.from({length: maxID+1}, () => [-1, -1])
const ins: Instrcution[] = Array.from({length: maxID+1}, () => [(n)=>{}, (n)=>{}])
input.forEach(i => {
let r = /value ([0-9]+) goes to bot ([0-9]+)/.exec(i)
if (r) {
const rob = parseInt(r[2])
addNum(rob, parseInt(r[1]))
return
}
r = /bot ([0-9]+) gives low to (bot|output) ([0-9]+) and high to (bot|output) ([0-9]+)/.exec(i)
if (r) {
const rob = parseInt(r[1])
const a = r[2]
const b = parseInt(r[3])
const c = r[4]
const d = parseInt(r[5])
if (a == 'bot') {
ins[rob][0] = (n: number) => addNum(b, n)
}
if (c == 'bot') {
ins[rob][1] = (n: number) => addNum(d, n)
}
return
}
throw i
})

function base(lower: number, higher: number) {
if (lower == 17 && higher == 61) return true
return false
}
function addNum(rob: number, v: number) {
if(numbers[rob][0] < 0) {
numbers[rob][0] = v
} else {
numbers[rob][1] = v
}
}

let i = 0

while(true) {
if (i >= numbers.length) i = 0
try {
if (numbers[i][0] >= 0 && numbers[i][1] >= 0) {
const min = Math.min(numbers[i][0], numbers[i][1])
const max = Math.max(numbers[i][0], numbers[i][1])
if (base(min, max)) {
console.log(i)
break;
}
ins[i][0](min)
ins[i][1](max)
numbers[i] = [-1,-1]
}
} catch(e) {
console.log(i)
throw e
}

i++
}
}

export async function taskTwo(input: string[]): Promise<void> {
let maxID = 0
let maxOut = 0
input.forEach(i => {
let r = /value ([0-9]+) goes to bot ([0-9]+)/.exec(i)
if (r) {
maxID = Math.max(maxID, parseInt(r[2]))
return
}
r = /bot ([0-9]+) gives low to (bot|output) ([0-9]+) and high to (bot|output) ([0-9]+)/.exec(i)
if (r) {
maxID = Math.max(maxID, parseInt(r[1]))
if (r[2] == 'bot') maxID = Math.max(maxID, parseInt(r[3]))
else maxOut = Math.max(maxOut, parseInt(r[3]))
if (r[4] == 'bot') maxID = Math.max(maxID, parseInt(r[5]))
else maxOut = Math.max(maxOut, parseInt(r[5]))
return
}
throw i
})
const numbers: [number, number][] = Array.from({length: maxID+1}, () => [-1, -1])
const ins: Instrcution[] = Array.from({length: maxID+1}, () => [(n)=>{}, (n)=>{}])
const out: number[][] = Array.from({length: maxOut+1}, ()=>[])
input.forEach(i => {
let r = /value ([0-9]+) goes to bot ([0-9]+)/.exec(i)
if (r) {
const rob = parseInt(r[2])
addNum(rob, parseInt(r[1]))
return
}
r = /bot ([0-9]+) gives low to (bot|output) ([0-9]+) and high to (bot|output) ([0-9]+)/.exec(i)
if (r) {
const rob = parseInt(r[1])
const a = r[2]
const b = parseInt(r[3])
const c = r[4]
const d = parseInt(r[5])
if (a == 'bot') {
ins[rob][0] = (n: number) => addNum(b, n)
} else {
ins[rob][0] = (n: number) => out[b].push(n)
}
if (c == 'bot') {
ins[rob][1] = (n: number) => addNum(d, n)
} else {
ins[rob][1] = (n: number) => out[d].push(n)
}
return
}
throw i
})

function addNum(rob: number, v: number) {
if(numbers[rob][0] < 0) {
numbers[rob][0] = v
} else {
numbers[rob][1] = v
}
}

let i = 0

while(true) {
if (i >= numbers.length) i = 0
if (numbers[i][0] >= 0 && numbers[i][1] >= 0) {
const min = Math.min(numbers[i][0], numbers[i][1])
const max = Math.max(numbers[i][0], numbers[i][1])
ins[i][0](min)
ins[i][1](max)
numbers[i] = [-1,-1]
}

if (out[0].length > 0 && out[1].length > 0 && out[2].length > 0) {
console.log(out[0][0]*out[1][0]*out[2][0])
break
}

i++
}
}

type F = (num: number) => void
type Instrcution = [F, F]
29 changes: 29 additions & 0 deletions 2016/11/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFile } from "fs/promises";
import { taskOne, taskTwo } from "./task";

async function main() {
let lastArgument = process.argv.pop() as string;
let taskNumber = 1;
let isTest = false;

if (lastArgument === "test") {
isTest = true;
taskNumber = parseInt(process.argv.pop() as string);
} else {
taskNumber = parseInt(lastArgument)
}

const fileToLoad = isTest ? "test.in" : "solve.in";
const fileContents = await readFile(fileToLoad, "utf-8")

const lines = fileContents.split("\n");

if (taskNumber === 1) {
await taskOne(lines);
}
if (taskNumber === 2) {
await taskTwo(lines);
}
}

void main();
Loading

0 comments on commit ab42164

Please sign in to comment.