Skip to content

Commit

Permalink
day 9!
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingAP committed Dec 9, 2024
1 parent 184f712 commit 837be2a
Show file tree
Hide file tree
Showing 38 changed files with 5,813 additions and 7,229 deletions.
Binary file modified aoc/aoc_data.db
Binary file not shown.
1,871 changes: 0 additions & 1,871 deletions aoc/test_out.txt

This file was deleted.

408 changes: 204 additions & 204 deletions index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion puzzles/2020/day01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
| | **Part 1** | **Part 2** |
| :--------------: | :--------: | :--------: |
| **Results** | 926464 | 65656536 |
| **Time (in ms)** | 0.29 | 1.44 |
| **Time (in ms)** | 0.06 | 0.29 |
19 changes: 7 additions & 12 deletions puzzles/2020/day01/solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* puzzles/2020/day01/solution.ts
*
Expand All @@ -14,13 +12,11 @@
* the code of part 1 of the puzzle
*/
const part1 = (input: string) => {
let numbers = input.split('\n').map(value => parseInt(value));
const numbers = input.trim().split('\n').map(value => parseInt(value));

for (let i = 0; i < numbers.length; i++) {
for (let j = 0; j < numbers.length; j++) {
if (i == j) continue;
if (numbers[i] + numbers[j] == 2020) return numbers[i] * numbers[j];
}
const other = 2020 - numbers[i];
if (numbers.includes(other)) return numbers[i] * other;
}

return 0;
Expand All @@ -30,14 +26,13 @@ const part1 = (input: string) => {
* the code of part 2 of the puzzle
*/
const part2 = (input: string) => {
let numbers = input.split('\n').map(value => parseInt(value));
const numbers = input.trim().split('\n').map(value => parseInt(value));

for (let i = 0; i < numbers.length; i++) {
const left = 2020 - numbers[i];
for (let j = 0; j < numbers.length; j++) {
for (let k = 0; k < numbers.length; k++) {
if (i == j || i == k || j == k) continue;
if (numbers[i] + numbers[j] + numbers[k] == 2020) return numbers[i] * numbers[j] * numbers[k];
}
const other = left - numbers[j];
if (numbers.includes(other)) return numbers[i] * numbers[j] * other;
}
}

Expand Down
16 changes: 8 additions & 8 deletions puzzles/2021/day01/solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* puzzles/2021/day01/solution.ts
*
Expand All @@ -14,23 +12,25 @@
* the code of part 1 of the puzzle
*/
const part1 = (input: string) => {
const numbers = input.split('\n').map(num => parseInt(num));

// count how many numbers are increasing from the last one
let count = 0;
let numbers = input.split('\n').map(num => parseInt(num));
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > numbers[i - 1]) count++;
}
for (let i = 1; i < numbers.length; i++) if (numbers[i] > numbers[i - 1]) count++;
return count;
};

/**
* the code of part 2 of the puzzle
*/
const part2 = (input: string) => {
const numbers = input.split('\n').map(num => parseInt(num));

// count how many numbers increased from the last 3 number window
let count = 0;
let numbers = input.split('\n').map(num => parseInt(num));
let last = numbers[0] + numbers[1] + numbers[2];
for (let i = 1; i < numbers.length - 1; i++) {
let next = numbers[i - 1] + numbers[i] + numbers[i + 1];
const next = numbers[i - 1] + numbers[i] + numbers[i + 1];
if (next > last) count++;
last = next;
}
Expand Down
22 changes: 12 additions & 10 deletions puzzles/2021/day02/solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* puzzles/2021/day02/solution.ts
*
Expand All @@ -14,16 +12,17 @@
* the code of part 1 of the puzzle
*/
const part1 = (input: string) => {
let commands = {
const COMMANDS: { [key: string]: { x: number, y: number } } = {
forward: { x: 1, y: 0 },
down: { x: 0, y: 1 },
up: { x: 0, y: -1 }
}

let sub = { x: 0, y: 0 };
// control the sub using the commands above
const sub = { x: 0, y: 0 };
input.trim().split('\n').forEach(element => {
let [command, amount] = element.split(' ');
let vel = commands[command];
const [command, amount] = element.split(' ');
const vel = COMMANDS[command];
sub.x += vel.x * parseInt(amount);
sub.y += vel.y * parseInt(amount);
});
Expand All @@ -34,19 +33,22 @@ const part1 = (input: string) => {
* the code of part 2 of the puzzle
*/
const part2 = (input: string) => {
let sub = { x: 0, y: 0, aim: 0 };
const sub = { x: 0, y: 0, aim: 0 };

// control the subs with the command below as it is more involved
input.trim().split('\n').forEach(element => {
let [command, amount] = element.split(' ');
const [command, amount] = element.split(' ');

if (command == 'forward') {
if (command === 'forward') {
sub.x += parseInt(amount);
sub.y += parseInt(amount) * sub.aim;
} else if (command == 'up') {
} else if (command === 'up') {
sub.aim -= parseInt(amount);
} else {
sub.aim += parseInt(amount);
}
});

return sub.x * sub.y;
};

Expand Down
74 changes: 31 additions & 43 deletions puzzles/2021/day03/solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* puzzles/2021/day03/solution.ts
*
Expand All @@ -14,24 +12,21 @@
* the code of part 1 of the puzzle
*/
const part1 = (input: string) => {
let binary = input.split('\n');
let gamma = '';
let ones = 0, zeros = 0;
const binary = input.split('\n');

// calculate gamma/epsilon from the most/least common digits in each column
let gamma = '', epsilon = '';
for (let j = 0; j < binary[0].length; j++) {
let ones = 0, zeros = 0;

for (let i = 0; i < binary.length; i++) {
if (binary[i][j] == '1') ones++;
if (binary[i][j] === '1') ones++;
else zeros++;
}
if (ones > zeros) gamma += '1';
else gamma += '0';
ones = 0;
zeros = 0;
}

let epsilon = gamma.split('').map(element => {
if (element == '1') return '0';
else return '1';
}).join('');
gamma += (ones > zeros) ? '1' : '0';
epsilon += (ones > zeros) ? '0' : '1';
}

return parseInt(gamma, 2) * parseInt(epsilon, 2);
};
Expand All @@ -40,48 +35,41 @@ const part1 = (input: string) => {
* the code of part 2 of the puzzle
*/
const part2 = (input: string) => {
let binary = input.split('\n');
let ones = 0, zeros = 0;
let oxygen = input.split('\n');
for (let j = 0; j < oxygen[0].length; j++) {
let ones = 0, zeros = 0;

let oxygen = '';
for (let j = 0; j < binary[0].length; j++) {
for (let i = 0; i < binary.length; i++) {
if (binary[i][j] == '1') ones++;
// find most common in column
for (let i = 0; i < oxygen.length; i++) {
if (oxygen[i][j] == '1') ones++;
else zeros++;
}

if (ones >= zeros) binary = binary.filter(element => element[j] === '1');
else binary = binary.filter(element => element[j] === '0');
// filter oxygen down until one is left
if (ones >= zeros) oxygen = oxygen.filter(element => element[j] === '1');
else oxygen = oxygen.filter(element => element[j] === '0');

ones = 0;
zeros = 0;
if (binary.length == 1) {
oxygen = binary[0];
break;
}
if (oxygen.length === 1) break;
}

binary = input.split('\n');
let carbon = input.split('\n');;
for (let j = 0; j < carbon[0].length; j++) {
let ones = 0, zeros = 0;

let carbon = '';
for (let j = 0; j < binary[0].length; j++) {
for (let i = 0; i < binary.length; i++) {
if (binary[i][j] == '1') ones++;
// find most common in column
for (let i = 0; i < carbon.length; i++) {
if (carbon[i][j] == '1') ones++;
else zeros++;
}

if (ones < zeros) binary = binary.filter(element => element[j] === '1');
else binary = binary.filter(element => element[j] === '0');
// filter carbon down until one is left
if (ones < zeros) carbon = carbon.filter(element => element[j] === '1');
else carbon = carbon.filter(element => element[j] === '0');

ones = 0;
zeros = 0;
if (binary.length == 1) {
carbon = binary[0];
break;
}
if (carbon.length == 1) break;
}

return parseInt(oxygen, 2) * parseInt(carbon, 2);
return parseInt(oxygen[0], 2) * parseInt(carbon[0], 2);
};

export { part1, part2 };
2 changes: 1 addition & 1 deletion puzzles/2021/day04/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
| | **Part 1** | **Part 2** |
| :--------------: | :--------: | :--------: |
| **Results** | 16716 | 4880 |
| **Time (in ms)** | 3.78 | 10.92 |
| **Time (in ms)** | 1.18 | 2.39 |
Loading

0 comments on commit 837be2a

Please sign in to comment.