Skip to content

Commit

Permalink
some 2019
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Nov 22, 2024
1 parent ab74939 commit 03f72ed
Show file tree
Hide file tree
Showing 12 changed files with 561 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2019/01/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();
17 changes: 17 additions & 0 deletions 2019/01/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export async function taskOne(input: string[]): Promise<void> {
console.log(input.map(Number).map(i => Math.floor(i/3)-2).reduce((a,b)=>a+b,0))
}

export async function taskTwo(input: string[]): Promise<void> {
console.log(input.map(Number).map(calc).reduce((a,b)=>a+b,0))

function calc(c: number) {
let sum = 0
let fuel = Math.floor(c/3)-2
while(fuel > 0) {
sum += fuel
fuel = Math.floor(fuel/3)-2
}
return sum
}
}
29 changes: 29 additions & 0 deletions 2019/02/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();
39 changes: 39 additions & 0 deletions 2019/02/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export async function taskOne(input: string[]): Promise<void> {
const list: Record<number, number> = {}
input[0].split(',').map(Number).forEach((i,idx) => list[idx] = i)
list[1] = 12
list[2] = 2

console.log(run(list)[0])
}

export async function taskTwo(input: string[]): Promise<void> {
const list: Record<number, number> = {}
input[0].split(',').map(Number).forEach((i,idx) => list[idx] = i)
for (let i = 0; i <= 99; i++) {
for (let j = 0; j <= 99; j++) {
const copy = JSON.parse(JSON.stringify(list))
copy[1] = i
copy[2] = j
if (run(copy)[0] == 19690720) {
console.log(i * 100 + j)
return
}
}
}
}

function run(list: Record<number, number>) {
let i = 0
while(list[i] != 99) {
if (list[i] == 1) {
list[list[i+3]] = list[list[i+1]] + list[list[i+2]]
} else if (list[i] == 2) {
list[list[i+3]] = list[list[i+1]] * list[list[i+2]]
} else {
throw i
}
i+=4
}
return list
}
29 changes: 29 additions & 0 deletions 2019/03/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();
156 changes: 156 additions & 0 deletions 2019/03/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
export async function taskOne(input: string[]): Promise<void> {
const parts = input[0].split(',')

let cX = 0
let cY = 0

const lines: Line[] = []
for (const p of parts) {
let dir = [0,0]
if (p[0] == 'U') dir = [0,1]
if (p[0] == 'D') dir = [0,-1]
if (p[0] == 'L') dir = [-1,0]
if (p[0] == 'R') dir = [1,0]
const mov = Number(p.substring(1))
const newLine = {
x1: cX,
y1: cY,
x2: cX + dir[0] * mov,
y2: cY + dir[1] * mov
}
lines.push(newLine)
cX = newLine.x2
cY = newLine.y2
}

const parts2 = input[1].split(',')
let minD = Infinity
cX = 0
cY = 0
for (const p of parts2) {
let dir = [0,0]
if (p[0] == 'U') dir = [0,1]
if (p[0] == 'D') dir = [0,-1]
if (p[0] == 'L') dir = [-1,0]
if (p[0] == 'R') dir = [1,0]
const mov = Number(p.substring(1))
const newLine = {
x1: cX,
y1: cY,
x2: cX + dir[0] * mov,
y2: cY + dir[1] * mov
}

for (const l of lines) {
const i = intersect(l, newLine)
if (i == null) continue
const d = Math.abs(i[0]) + Math.abs(i[1])
if (d == 0) continue
if (d < minD) minD = d
}

cX = newLine.x2
cY = newLine.y2
}
console.log(minD)
}

export async function taskTwo(input: string[]): Promise<void> {
interface Line2 extends Line {
goneDistance: number
}

const parts = input[0].split(',')

let cX = 0
let cY = 0
let cD = 0

const lines: Line2[] = []
for (const p of parts) {
let dir = [0,0]
if (p[0] == 'U') dir = [0,1]
if (p[0] == 'D') dir = [0,-1]
if (p[0] == 'L') dir = [-1,0]
if (p[0] == 'R') dir = [1,0]
const mov = Number(p.substring(1))
const newLine = {
x1: cX,
y1: cY,
x2: cX + dir[0] * mov,
y2: cY + dir[1] * mov,
goneDistance: cD
}
lines.push(newLine)
cX = newLine.x2
cY = newLine.y2
cD += mov
}

const parts2 = input[1].split(',')
let minD = Infinity
cX = 0
cY = 0
cD = 0
for (const p of parts2) {
let dir = [0,0]
if (p[0] == 'U') dir = [0,1]
if (p[0] == 'D') dir = [0,-1]
if (p[0] == 'L') dir = [-1,0]
if (p[0] == 'R') dir = [1,0]
const mov = Number(p.substring(1))
const newLine = {
x1: cX,
y1: cY,
x2: cX + dir[0] * mov,
y2: cY + dir[1] * mov
}

for (const l of lines) {
const i = intersect(l, newLine)
if (i == null) continue
if(i[0] == 0 && i[1] == 0) continue
const d = cD + l.goneDistance + Math.abs(l.x1 - i[0]) + Math.abs(l.y1 - i[1]) + Math.abs(newLine.x1 - i[0]) + Math.abs(newLine.y1 - i[1])
if (d < minD) minD = d
}

cX = newLine.x2
cY = newLine.y2
cD += mov
}
console.log(minD)
}

interface Line {
x1: number
x2: number
y1: number
y2: number
}

function intersect(l1: Line, l2: Line): [number, number]|null {
const x1 = l1.x1
const x2 = l1.x2
const x3 = l2.x1
const x4 = l2.x2
const y1 = l1.y1
const y2 = l1.y2
const y3 = l2.y1
const y4 = l2.y2

const pX = ((x1*y2 - y1*x2)*(x3 - x4) - (x1 - x2)*(x3*y4 - y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))
const pY = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))

if (!inRange(pX, x1,x2) || !inRange(pX, x3,x4) || !inRange(pY, y1,y2) || !inRange(pY,y3,y4)) {
return null
}

return [pX, pY]
}

function inRange(test: number, n1: number, n2: number) {
let max = n1 > n2 ? n1 : n2
let min = n1 > n2 ? n2 : n1

return test >= min && test <= max
}
29 changes: 29 additions & 0 deletions 2019/04/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();
54 changes: 54 additions & 0 deletions 2019/04/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export async function taskOne(input: string[]): Promise<void> {
const range = input[0].split('-').map(Number)
let count = 0
for (let i = range[0]; i <= range[1]; i++) {
if (checkValid(i)) count++
}
console.log(count)

function checkValid(n: number) {
const nums = getNums(n)
for (let i = 1; i < nums.length; i++) {
if (nums[i] < nums[i-1]) return false
}
for (let i = 1; i < nums.length; i++) {
if (nums[i] == nums[i-1]) return true
}
return false
}
}

export async function taskTwo(input: string[]): Promise<void> {
const range = input[0].split('-').map(Number)
let count = 0
for (let i = range[0]; i <= range[1]; i++) {
if (checkValid(i)) count++
}
console.log(count)

function checkValid(n: number) {
const nums = getNums(n)
for (let i = 1; i < nums.length; i++) {
if (nums[i] < nums[i-1]) return false
}
const c: number[] = []
let last = nums[0]
let co = 0
for (let i = 0; i < nums.length; i++) {
if (nums[i] == last) co++
else {
c.push(co)
co = 1
last = nums[i]
}
}
c.push(co)
return c.includes(2)
}
}



function getNums(n: number): number[] {
return [100000, 10000, 1000, 100, 10, 1].map(i => Math.floor((n % (i*10)) / i))
}
Loading

0 comments on commit 03f72ed

Please sign in to comment.