Skip to content

Commit

Permalink
days 1, 2, 3
Browse files Browse the repository at this point in the history
  • Loading branch information
aetheryx committed Dec 3, 2024
1 parent 9ec4f1e commit a7a8087
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/2024/day1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { DayOpts } from 'src/day';

function day1(input: string): any {
const lines = input.split('\n');
let left: number[] = [];
let right: number[] = [];

for (const line of lines) {
const [ l, r ] = line.split(/ +/).map(Number);
left.push(l);
right.push(r);
}

left.sort((a, b) => a - b);
right.sort((a, b) => a - b);

let sum = 0;
for (let i = 0; i < left.length; i++) {
sum += Math.abs(left[i] - right[i]);
}

return sum;
}

export function day(input: string): any {
const lines = input.split('\n');
let left: number[] = [];
let right = new Map<number, number>();

for (const line of lines) {
const [ l, r ] = line.split(/ +/).map(Number);
left.push(l);
right.set(r, (right.get(r) ?? 0) + 1);
}

let sum = 0;
for (const l of left) {
sum += l * (right.get(l) ?? 0);
}

return sum;
}

export const opts: DayOpts = {
input: readFileSync(join(__dirname, './input.txt')).toString(),
sampleInput: readFileSync(join(__dirname, './sample.txt')).toString(),
sampleAnswer: 11,
};
6 changes: 6 additions & 0 deletions src/2024/day1/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
67 changes: 67 additions & 0 deletions src/2024/day2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { DayOpts } from 'src/day';

const isLevelSafe = (level: number[]) => {
let pos = level[0] > level[1];
for (let i = 0; i < level.length; i++) {
const curr = level[i];
const next = level[i + 1];
const diff = pos ? (curr - next) : (next - curr);
if (diff < 1 || diff > 3) {
return false;
}
}

return true;
};

function day1(input: string): any {
const lines = input.split('\n');
let sum = 0;
const levels: number[][] = [];

for (const line of lines) {
levels.push(line.split(' ').map(Number));
}

for (const level of levels) {
if (isLevelSafe(level)) {
sum++;
}
}

return sum;
}

export function day(input: string): any {
const lines = input.split('\n');
let sum = 0;
const levels: number[][] = [];

for (const line of lines) {
levels.push(line.split(' ').map(Number));
}

for (const level of levels) {
let safe = isLevelSafe(level);

for (let i = 0; i <= level.length; i++) {
const l2 = [ ...level ];
l2.splice(i, 1);
safe ||= isLevelSafe(l2);
}

if (safe) {
sum++;
}
}

return sum;
}

export const opts: DayOpts = {
input: readFileSync(join(__dirname, './input.txt')).toString(),
sampleInput: readFileSync(join(__dirname, './sample.txt')).toString(),
sampleAnswer: 4,
};
6 changes: 6 additions & 0 deletions src/2024/day2/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
34 changes: 34 additions & 0 deletions src/2024/day3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { DayOpts } from 'src/day';

export function day(input: string): any {
const lines = input.split('\n');
let sum = 0;

let doing = true;

for (let line of lines) {
const matches = line.match(/(mul\((\d+),(\d+)\))|(do\(\))|(don't\(\))/g);
for (const match of matches ?? []) {
if (match === 'do()') {
doing = true;
} else if (match === 'don\'t()') {
doing = false;
}

if (match.startsWith('mul') && doing) {
const [ _, a, b ] = match.match(/mul\((\d+),(\d+)\)/);
sum += Number(a) * Number(b);
}
}
}

return sum;
}

export const opts: DayOpts = {
input: readFileSync(join(__dirname, './input.txt')).toString(),
sampleInput: readFileSync(join(__dirname, './sample.txt')).toString(),
sampleAnswer: 48,
};
1 change: 1 addition & 0 deletions src/2024/day3/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

0 comments on commit a7a8087

Please sign in to comment.