Skip to content

Commit

Permalink
all of 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Nov 23, 2024
1 parent 03f72ed commit afd191f
Show file tree
Hide file tree
Showing 50 changed files with 2,605 additions and 4 deletions.
18 changes: 16 additions & 2 deletions 2020/01/task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
export async function taskOne(input: string[]): Promise<void> {
console.log("Unimplemented");
const nums = input.map(Number)
for (let i = 0; i < nums.length; i++) {
for (let j = i+1; j < nums.length; j++) {
if (nums[i] + nums[j] == 2020) console.log(nums[i]*nums[j])
}
}
}

export async function taskTwo(input: string[]): Promise<void> {
console.log("Unimplemented");
const nums = input.map(Number)
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 2020) continue
for (let j = i+1; j < nums.length; j++) {
if (nums[i] + nums[j] > 2020) continue
for (let k = j+1; k < nums.length; k++) {
if (nums[i] + nums[j] + nums[k] == 2020) console.log(nums[i]*nums[j]*nums[k])
}
}
}
}
29 changes: 29 additions & 0 deletions 2020/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();
28 changes: 28 additions & 0 deletions 2020/02/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export async function taskOne(input: string[]): Promise<void> {
// 1-3 a: abcde
console.log(input.map(i => {
const r = /([0-9]+)-([0-9]+) (.): (.*)/.exec(i)!
let min = Number(r[1])
let max = Number(r[2])
let letter = r[3]
let pw = r[4]
let c = 0
for (const l of pw) {
if (l == letter) c++
}
return (c >= min && c <= max ? 1 : 0) as number
}).reduce((a,b)=>a+b,0))
}

export async function taskTwo(input: string[]): Promise<void> {
console.log(input.map(i => {
const r = /([0-9]+)-([0-9]+) (.): (.*)/.exec(i)!
let pos1 = Number(r[1])
let pos2 = Number(r[2])
let letter = r[3]
let pw = r[4]
const p2 = pw[pos2-1] == letter
const res = pw[pos1-1] == letter ? !p2 : p2
return (res ? 1:0) as number
}).reduce((a,b)=>a+b,0))
}
29 changes: 29 additions & 0 deletions 2020/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();
24 changes: 24 additions & 0 deletions 2020/03/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export async function taskOne(input: string[]): Promise<void> {
console.log(check(input, 3, 1))
}

export async function taskTwo(input: string[]): Promise<void> {
console.log(check(input,1,1)*check(input,3,1)*check(input,5,1)*check(input,7,1)*check(input,1,2));
}

function check(input: string[], xD: number, yD: number) {
const map = input.map(i => i.split(''))
const W = map[0].length
function get(x: number, y: number) {
return map[y][x % W]
}
let y = 0
let c = 0
let x = 0
while(y < map.length) {
if (get(x,y) == '#') c++
x += xD
y += yD
}
return c
}
29 changes: 29 additions & 0 deletions 2020/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();
66 changes: 66 additions & 0 deletions 2020/04/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export async function taskOne(input: string[]): Promise<void> {
const passportKeys: string[][] = []
let cur: string[] = []
for (const i of input) {
if (i == '') {
passportKeys.push(cur)
cur = []
continue
}
i.split(' ').forEach(j => {
cur.push(j.split(':')[0])
})
}
passportKeys.push(cur)
const allNeededKeys = ['byr','iyr','eyr','hgt','hcl','ecl','pid']
let c = 0
for (const p of passportKeys) {
if (allNeededKeys.every(k => p.includes(k))) c++
}
console.log(c)
}

export async function taskTwo(input: string[]): Promise<void> {
const passportPairs: Record<string, string>[] = []
let cur: Record<string, string> = {}
for (const i of input) {
if (i == '') {
passportPairs.push(cur)
cur = {}
continue
}
i.split(' ').forEach(j => {
const s = j.split(':')
cur[s[0]] = s[1]
})
}
passportPairs.push(cur)
const allNeededKeys = ['byr','iyr','eyr','hgt','hcl','ecl','pid']
let c = 0
for (const p of passportPairs) {
const keys = Object.keys(p)
if (allNeededKeys.some(k => !keys.includes(k))) continue
if (!checkYear(p['byr'], 1920, 2002)) continue
if (!checkYear(p['iyr'], 2010, 2020)) continue
if (!checkYear(p['eyr'], 2020, 2030)) continue
if (!/^#[0-9a-f]{6}$/.test(p['hcl'])) continue
if (!['amb','blu','brn','gry','grn','hzl','oth'].includes(p['ecl'])) continue
if (!/^[0-9]{9}$/.test(p['pid'])) continue

const h = p['hgt']
const n = Number(h.substring(0, h.length-2))
if (h.endsWith('in')) {
if (n > 76 || n < 59) continue
} else if (h.endsWith('cm')) {
if (n > 193 || n < 150) continue
} else continue
c++
}
console.log(c)

function checkYear(y: string, min: number, max: number) {
if (!/^[0-9]{4}$/.test(y)) return false
const v = Number(y)
return v >= min && v <= max
}
}
29 changes: 29 additions & 0 deletions 2020/05/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();
38 changes: 38 additions & 0 deletions 2020/05/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export async function taskOne(input: string[]): Promise<void> {
let max = 0
for (const i of input) {
const v = getRow(i)*8+getSeat(i)
if(v > max) max =v
}
console.log(max)
}

export async function taskTwo(input: string[]): Promise<void> {
const seats = new Set<number>()
let MAX = 128*8
for (const i of input) {
const v = getRow(i)*8+getSeat(i)
seats.add(v)
}
for (let i = 0; i < MAX; i++) {
if (!seats.has(i) && seats.has(i+1) && seats.has(i-1)) console.log(i)
}
}

function getRow(s: string) {
const mults = [64,32,16,8,4,2,1]
let c = 0
for (let i = 0; i < mults.length; i++) {
if (s[i] == 'B') c += mults[i]
}
return c
}

function getSeat(s: string) {
const mults = [4,2,1]
let c = 0;
for (let i = 0; i < mults.length; i++) {
if (s[i+7] == 'R') c += mults[i]
}
return c
}
29 changes: 29 additions & 0 deletions 2020/06/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();
37 changes: 37 additions & 0 deletions 2020/06/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export async function taskOne(input: string[]): Promise<void> {
input.push('')
const s = new Set<string>()
let c = 0
for (const i of input) {
if (i == '') {
c += s.size
s.clear()
}
i.split('').forEach(j => s.add(j))
}
console.log(c)
}

export async function taskTwo(input: string[]): Promise<void> {
input.push('')
let s: Record<string, number> = {}
let c = 0
let pc=0
for (const i of input) {
if (i == '') {
const keys = Object.keys(s)
for (let k of keys) {
if (s[k] == pc) c++
}
s = {}
pc = 0
continue
}
i.split('').forEach(j => {
if(s[j] == undefined) s[j] = 0
s[j]++
})
pc++
}
console.log(c)
}
Loading

0 comments on commit afd191f

Please sign in to comment.