Skip to content

Commit

Permalink
ported all 2015, 2016, 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingAP committed Nov 25, 2024
1 parent 602d37e commit ef80cfb
Show file tree
Hide file tree
Showing 184 changed files with 17,215 additions and 11,487 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ These are all my solutions for the years of [Advent of Code](https://adventofcod
### How to Use Locally

Clone this repository on your computer and run...

```
> npm i
```
Expand All @@ -18,10 +19,12 @@ SESSION=YOUR_TOKEN_HERE
```

### How to Run

To see how it works, run `node aoc` to see all commands and arguments.

Also, to lint:
`npx prettier . --write`
Also, to lint: `npx prettier . --write`

### Note for AOC moderators
The automated requests are served from `aoc.js`, where they have the a User-Agent header that points to this page. Nothing is done automatically, all done by a function call by the built in command line tool or a call from another script. Also, there are no inputs stored on the GitHub repository

The automated requests are served from `aoc.js`, where they have the a User-Agent header that points to this page. Nothing is done automatically, all done by a
function call by the built in command line tool or a call from another script. Also, there are no inputs stored on the GitHub repository
Binary file modified aoc/aoc_data.db
Binary file not shown.
1 change: 1 addition & 0 deletions aoc/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@codingap/steve": "jsr:@codingap/steve@^2.0.10",
"@db/sqlite": "jsr:@db/sqlite@^0.12.0",
"@libs/markdown": "jsr:@libs/markdown@^2.0.0",
"@std/crypto": "jsr:@std/crypto@^1.0.3",
"@std/html": "jsr:@std/html@1",
"@std/path": "jsr:@std/path@1",
"@std/fs": "jsr:@std/fs@1"
Expand Down
5 changes: 5 additions & 0 deletions aoc/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions aoc/fix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { join, resolve } from '@std/path';
import { getTitle, profilePuzzle, runPuzzle, submitAnswer, updatePuzzle } from './aoc.ts';

const year = '2016';

const baseDirectory = resolve('../puzzles');

const getComment = async (day: string, year: string) => {
const title = await getTitle(day, year);

return `// @ts-nocheck previous years was written in javascript, so disable it here
/**
* puzzles/${year}/day${day}/solution.ts
*
* ~~ ${title} ~~
* this is my solution for this advent of code puzzle
*
* by alex prosser
* 11/24/2024
*/
`;
};

const part1Comment = `/**
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {string | number} the result of part 1
*/
const part1 = input => {`;

const part2Comment = `/**
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {string | number} the result of part 2
*/
const part2 = input => {`;

for (let i = 1; i <= 25; i++) {
const solutionDirectory = join(baseDirectory, year, `day${i.toString().padStart(2, '0')}`);

await profilePuzzle(i.toString(), year, '3');
await submitAnswer(i.toString(), year, 'both', 'true');
console.log('day', i, year);

// const response = await submitAnswer(i.toString(), year, 'both', 'true');
// console.log(response.part1);
// console.log(response.part2);

// let script = await Deno.readTextFile(join(solutionDirectory, 'solution.js'));
// script = script.replace(/async /g, '');
// script = script.replace(/const part1 = input => {/g, part1Comment);
// script = script.replace(/const part2 = input => {/g, part2Comment);
// const lines = script.replace(/\r/g, '').split('\n');
// const commentLines = await getComment(i.toString(), year);

// commentLines.split('\n').reverse().forEach(line => lines.unshift(line));

// await Deno.writeTextFile(join(baseDirectory, year, `day${i.toString().padStart(2, '0')}`, 'solution.ts'), lines.join('\n'));
// await Deno.remove(join(baseDirectory, year, `day${i.toString().padStart(2, '0')}`, 'solution.js'))
}
16,800 changes: 8,405 additions & 8,395 deletions index.html

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion puzzles/2015/day01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,24 @@
|| **Part 1** | **Part 2** |
|:--:|:---:|:---:|
| **Results** | 280 | 1797 |
| **Time (in ms)** | 0.25 | 0.27 |
| **Time (in ms)** | 0.25 | 0.27 |

%%%

The first puzzle of Advent of Code! This is also the start of the Advent of Code Hub that I created! This was a lot of work, but I'm happy to show everything I built off because I believe it will be a very valuable learning experience, not only to the people reading but to myself as well. Anyways, on to the puzzle!

In this puzzle, we are on an elevator that moves up and down, and it is controlled by a series of parenthesis: `(` makes us go up and `)` makes us go down.

For the first part, we need to find what floor we end up on. Single enough, I used a `.reduce()` to go through all characters and add 1 if it was a `(` and -1 if it was a `)`. For my input, my answer is `280`.

For the second part, though, we need to find when we first enter the 'basement', or when we reach a negative floor. Instead of using `.reduce()`, I just looped normally and checked when we hit a negative floor. I did it with a flag, which is slightly weird, but it works...

```javascript
let floor = 0, first = -1;
input.split('').forEach((char, index) => {
floor += (char == '(') ? 1 : -1;
if (floor < 0 && first == -1) first = index + 1;
});
```

Afterwards, for my input, my answer is `1797`. I didn't compete during 2015 (my first was 2019), so this was done after the fact. I think for a first-ever concept, it worked well. The first part was easy while the second part used the same concept, but changed it to target a different solution.
12 changes: 10 additions & 2 deletions puzzles/2015/day01/solution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* puzzles/2015/day1/solution.ts
*
Expand All @@ -10,15 +12,21 @@

/**
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {string | number} the result of part 1
*/
const part1 = (input: string) => {
const part1 = input => {
return input.split('').reduce((sum, char) => sum + ((char == '(') ? 1 : -1), 0);
}

/**
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {string | number} the result of part 2
*/
const part2 = (input: string) => {
const part2 = input => {
let floor = 0, first = -1;
input.split('').forEach((char, index) => {
floor += (char == '(') ? 1 : -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* aoc/puzzles/2015/day02/solution.js
* puzzles/2015/day02/solution.ts
*
* ~~ I Was Told There Would Be No Math ~~
* this is my solution for this advent of code puzzle
Expand All @@ -12,9 +14,9 @@
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 1
* @returns {string | number} the result of part 1
*/
const part1 = async input => {
const part1 = input => {
return input.split(/\n/g).reduce((sum, dimensions) => {
let [l, w, h] = dimensions.split(/x/g).map(num => parseInt(num)).sort((a, b) => a - b);
let surfaceArea = 2 * l * w + 2 * w * h + 2 * h * l;
Expand All @@ -27,9 +29,9 @@ const part1 = async input => {
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 2
* @returns {string | number} the result of part 2
*/
const part2 = async input => {
const part2 = input => {
return input.split(/\n/g).reduce((sum, dimensions) => {
let [l, w, h] = dimensions.split(/x/g).map(num => parseInt(num)).sort((a, b) => a - b);
let perimeter = 2 * l + 2 * w;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* aoc/puzzles/2015/day03/solution.js
* puzzles/2015/day03/solution.ts
*
* ~~ Perfectly Spherical Houses in a Vacuum ~~
* this is my solution for this advent of code puzzle
Expand All @@ -12,9 +14,9 @@
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 1
* @returns {string | number} the result of part 1
*/
const part1 = async input => {
const part1 = input => {
let santa = { x: 0, y: 0 };
let houses = new Set();

Expand All @@ -34,9 +36,9 @@ const part1 = async input => {
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 2
* @returns {string | number} the result of part 2
*/
const part2 = async input => {
const part2 = input => {
let santa = { x: 0, y: 0 }, robot = { x: 0, y: 0 };
let houses = new Set();

Expand Down
49 changes: 0 additions & 49 deletions puzzles/2015/day04/solution.js

This file was deleted.

58 changes: 58 additions & 0 deletions puzzles/2015/day04/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* puzzles/2015/day04/solution.ts
*
* ~~ The Ideal Stocking Stuffer ~~
* this is my solution for this advent of code puzzle
*
* by alex prosser
* 11/27/2023
*/

import { crypto } from '@std/crypto';

/**
* returns an md5 hash for the given string.
*
* @param {string} content - the string to hash.
* @returns {string} the md5 hash of the input string in hexadecimal format.
*/
const md5 = (content) => {
const encoder = new TextEncoder();
const data = encoder.encode(content);
const hashBuffer = crypto.subtle.digestSync('md5', data);
return Array.from(new Uint8Array(hashBuffer))
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
};

/**
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {string | number} the result of part 1
*/
const part1 = input => {
let i = 0;
while (true) {
if (md5(input + i).startsWith('00000')) return i;
i++;
}
}

/**
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {string | number} the result of part 2
*/
const part2 = input => {
let i = 0;
while (true) {
if (md5(input + i).startsWith('000000')) return i;
i++;
}
}

export { part1, part2 };
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-nocheck previous years was written in javascript, so disable it here

/**
* aoc/puzzles/2015/day05/solution.js
* puzzles/2015/day05/solution.ts
*
* ~~ Doesn&apos;t He Have InternElves For This? ~~
* this is my solution for this advent of code puzzle
Expand All @@ -12,9 +14,9 @@
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 1
* @returns {string | number} the result of part 1
*/
const part1 = async input => {
const part1 = input => {
return input.split(/\n/g).reduce((sum, line) => {
const characters = line.split('');

Expand All @@ -30,9 +32,9 @@ const part1 = async input => {
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 2
* @returns {string | number} the result of part 2
*/
const part2 = async input => {
const part2 = input => {
return input.split(/\n/g).reduce((sum, line) => {
const characters = line.split('');

Expand Down
Loading

0 comments on commit ef80cfb

Please sign in to comment.