Skip to content

Commit

Permalink
2017 10-18
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Jul 21, 2024
1 parent d94459b commit c83bc89
Show file tree
Hide file tree
Showing 18 changed files with 997 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2017/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();
66 changes: 66 additions & 0 deletions 2017/10/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const HEX = [
'0', '1', '2', '3','4','5','6','7','8','9','a','b','c','d','e','f'
]

export async function taskOne(input: string[]): Promise<void> {
const LENGTH = 256
let list = Array.from({length: LENGTH}, (_, i: number) => i)
let currentIndex = 0;
let skipSize = 0;

const instrcutions = input[0].split(',').map(Number)
for (const i of instrcutions) {
reverse(i)
currentIndex += i + skipSize
skipSize++
}
console.log(list[0] * list[1])

function reverse(len: number) {
for (let i = 0; i < len / 2; i++) {
const temp = list[(currentIndex + i) % LENGTH]
list[(currentIndex + i) % LENGTH] = list[(currentIndex + len - i - 1) % LENGTH]
list[(currentIndex + len - i - 1) % LENGTH] = temp
}
}
}

export async function taskTwo(input: string[]): Promise<void> {
const LENGTH = 256
let list = Array.from({length: LENGTH}, (_, i: number) => i)
let currentIndex = 0;
let skipSize = 0;

const instrcutions = input[0].split('').map((s: string) => s.charCodeAt(0))
instrcutions.push(...[17, 31, 73, 47, 23])
for (let c = 0; c < 64; c++) {
for (const i of instrcutions) {
reverse(i)
currentIndex += i + skipSize
currentIndex %= LENGTH
skipSize++
}
}

let dense = Array.from({length: 16}, () => 0)
for (let i = 0; i < 16; i++) {
for (let j = 0; j < 16; j++) {
dense[i] = dense[i] ^ list[i * 16 + j]
}
}

let result = ''
for (const d of dense) {
result += HEX[Math.floor(d/16)]
result += HEX[d%16]
}
console.log(result)

function reverse(len: number) {
for (let i = 0; i < len / 2; i++) {
const temp = list[(currentIndex + i) % LENGTH]
list[(currentIndex + i) % LENGTH] = list[(currentIndex + len - i - 1) % LENGTH]
list[(currentIndex + len - i - 1) % LENGTH] = temp
}
}
}
29 changes: 29 additions & 0 deletions 2017/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();
104 changes: 104 additions & 0 deletions 2017/11/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {Queue} from '../../base/simpleStructure'

export async function taskOne(input: string[]): Promise<void> {

console.log(distance(input[0].split(',')))
}

export async function taskTwo(input: string[]): Promise<void> {
const all = input[0].split(',')
const temp = [] as string[]
let max = 0;
for (const a of all) {
temp.push(a)
const d = distance(temp)
if (d > max) max = d
}
console.log(max)
}

function distance(moves: string[]) {
let n = 0;
let s = 0;
let sw = 0;
let se = 0;
let nw = 0;
let ne = 0;
moves.forEach(i => {
switch(i) {
case 'n':
n++
break;
case 's':
s++;
break;
case 'ne':
ne++
break;
case 'se':
se++;
break;
case 'nw':
nw++
break;
case 'sw':
sw++;
break;
}
})
let didSomething = false;
do {
didSomething = false
if (n != 0 && s != 0) {
didSomething = true
const m = Math.min(n,s)
n -= m
s -= m
}

if (ne != 0 && nw != 0) {
didSomething = true
const m = Math.min(ne,nw)
ne -= m
nw -= m
n += m
}
if (se != 0 && sw != 0) {
didSomething = true
const m = Math.min(se,sw)
se -= m
sw -= m
s += m
}

if (se != 0 && n != 0) {
didSomething = true
const m = Math.min(se,n)
se -= m
n -= m
ne += m
}
if (sw != 0 && n != 0) {
didSomething = true
const m = Math.min(sw,n)
sw -= m
n -= m
nw += m
}
if (ne != 0 && s != 0) {
didSomething = true
const m = Math.min(ne,s)
ne -= m
s -= m
se += m
}
if (nw != 0 && s != 0) {
didSomething = true
const m = Math.min(nw,s)
nw -= m
s -= m
sw += m
}
} while(didSomething)
return n+s+ne+nw+se+sw
}
29 changes: 29 additions & 0 deletions 2017/12/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();
43 changes: 43 additions & 0 deletions 2017/12/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export async function taskOne(input: string[]): Promise<void> {
const graph: Record<string, string[]> = {}
input.forEach(i => {
const r = /([0-9]+) <-> (.*)/.exec(i)!
graph[r[1]] = r[2].split(', ')
})

const V = new Set<string>()
const Q = ['0']
while(Q.length > 0) {
const q = Q.pop() as string
if (V.has(q)) continue;
V.add(q)
Q.push(...graph[q])
}
console.log(V.size)
}

export async function taskTwo(input: string[]): Promise<void> {
const graph: Record<string, string[]> = {}
input.forEach(i => {
const r = /([0-9]+) <-> (.*)/.exec(i)!
graph[r[1]] = r[2].split(', ')
})

const V = new Set<string>()
const Q = [] as string[]
const keys = Object.keys(graph)
let groupCount = 0;
for (const k of keys) {
if (V.has(k)) continue
groupCount++
Q.push(k)
while(Q.length > 0) {
const q = Q.pop() as string
if (V.has(q)) continue;
V.add(q)
Q.push(...graph[q])
}
}

console.log(groupCount)
}
29 changes: 29 additions & 0 deletions 2017/13/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();
29 changes: 29 additions & 0 deletions 2017/13/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export async function taskOne(input: string[]): Promise<void> {
const nums = input.map(i => i.split(': ').map(Number))
let sum = 0;
for (const n of nums) {
if ((n[0] % (n[1]*2-2)) == 0) {
sum += n[1] * n[0]
}
}
console.log(sum)
}

export async function taskTwo(input: string[]): Promise<void> {
const nums = input.map(i => i.split(': ').map(Number))
let i = 0
while (true) {
let passed = true
for (const n of nums) {
if (((n[0] + i) % (n[1]*2-2)) == 0) {
passed = false
break;
}
}
if (passed) break
i++
continue
}
console.log(i)

}
Loading

0 comments on commit c83bc89

Please sign in to comment.