Skip to content

Commit

Permalink
past two days
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingAP committed Dec 6, 2023
1 parent 4b6db9d commit 37665e9
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 20 deletions.
8 changes: 8 additions & 0 deletions aoc/puzzles/2023/day05/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Advent of Code 2023 - Day 5: [If You Give A Seed A Fertilizer](https://adventofcode.com/2023/day/5)

## [Write Up](https://codingap.github.io/advent-of-code/writeups/2023/day05)
## Results
|| **Part 1** | **Part 2** |
|:--:|:---:|:---:|
| **Results** | 484023871 | 46294175 |
| **Time (in ms)** | 0.43 | TOO LONG!!! |
92 changes: 92 additions & 0 deletions aoc/puzzles/2023/day05/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* aoc/puzzles/2023/day05/solution.js
*
* ~~ If You Give A Seed A Fertilizer ~~
* this is my solution for this advent of code puzzle
*
* by alex prosser
* 12/4/2023
*/

/**
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 1
*/
const part1 = async input => {
let [seeds, ...maps] = input.split(/\n\n/g);

seeds = seeds.split(/: /)[1].split(/ /g).map(num => parseInt(num));
maps = maps.reduce((obj, map) => {
let [name, ...locations] = map.split(/\n/g);
obj[name.split(/ /)[0]] = locations.map(numbers => numbers.split(/ /).map(num => parseInt(num)));
return obj;
}, {});

let mapNames = Object.keys(maps);

let source = seeds;
for (let i = 0; i < mapNames.length; i++) {
let destination = maps[mapNames[i]];

for (let i = 0; i < source.length; i++) {
let newNumber = source[i];
for (let j = 0; j < destination.length; j++) {
if (newNumber >= destination[j][1] && newNumber <= destination[j][1] + destination[j][2]) {
newNumber = (newNumber - destination[j][1]) + destination[j][0];
break;
}
}
source[i] = newNumber;
}
}

return source.reduce((min, num) => Math.min(min, num), Infinity);
}

/**
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 2
*/
const part2 = async input => {
let [seeds, ...maps] = input.split(/\n\n/g);

seeds = seeds.split(/: /)[1].split(/ /g).map(num => parseInt(num));

maps = maps.reduce((obj, map) => {
let [name, ...locations] = map.split(/\n/g);
obj[name.split(/ /)[0]] = locations.map(numbers => numbers.split(/ /).map(num => parseInt(num)));
return obj;
}, {});

const mapNames = Object.keys(maps);

const findMinimum = range => {
let min = Infinity;
for (let num = range.start; num < range.end; num++) {
let newNumber = num;
for (let i = 0; i < mapNames.length; i++) {
let destination = maps[mapNames[i]];
for (let j = 0; j < destination.length; j++) {
if (newNumber >= destination[j][1] && newNumber <= destination[j][1] + destination[j][2]) {
newNumber = (newNumber - destination[j][1]) + destination[j][0];
break;
}
}
}
min = Math.min(min, newNumber);
}
return min;
}

let min = Infinity;
for (let i = 0; i < seeds.length; i += 2) {
min = Math.min(min, findMinimum({ start: seeds[i], end: seeds[i] + seeds[i + 1] }));
}
return min;
}

export { part1, part2 };
53 changes: 53 additions & 0 deletions aoc/puzzles/2023/day06/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* aoc/puzzles/2023/day06/solution.js
*
* ~~ Wait For It ~~
* this is my solution for this advent of code puzzle
*
* by alex prosser
* 12/5/2023
*/

/**
* code for part 1 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 1
*/
const part1 = async input => {
// parse input
let [times, distances] = input.split(/\n/g).map(line => {
return line.split(' ').filter(string => string != '').slice(1).map(num => parseInt(num));
});

// multiply all possible ways for each time/distance
return times.reduce((mul, time, index) => {
let ways = 0;
for (let i = 0; i < time; i++) {
if (i * (time - i) > distances[index]) ways++;
}
return mul * ways;
}, 1);
}

/**
* code for part 2 of the advent of code puzzle
*
* @param {string} input
* @returns {Promise<string | number>} the result of part 2
*/
const part2 = async input => {
// parse input
let [time, distance] = input.split(/\n/g).map(line => {
return parseInt(line.split(' ').filter(string => string != '').slice(1).join(''));
});

// find all ways that i * (time - i) is bigger than distance
let ways = 0;
for (let i = 0; i < time; i++) {
if (i * (time - i) > distance) ways++;
}
return ways;
}

export { part1, part2 };
8 changes: 8 additions & 0 deletions aoc/src/puzzles.json
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,14 @@
{
"title": "Scratchcards",
"stars": 2
},
{
"title": "If You Give A Seed A Fertilizer",
"stars": 2
},
{
"title": "Wait For It",
"stars": 0
}
]
}
32 changes: 27 additions & 5 deletions aoc/src/steve/visualization.steve
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@
position: relative;
}

.container canvas, .overlay {
.container canvas {
position: absolute;
}

.overlay {
position: absolute;
padding: 20px;
}
</style>
</head>

<body>
<div class="container">
<canvas></canvas>
<div class="overlay">This div is over the canvas</div>
<div class="overlay">
<label for="input">Load Input:</label>
<input type="file" name="input">
</div>
</div>

<script>
const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');
const inputLoaderInput = document.querySelector('input');

const resize = () => {
canvas.width = window.innerWidth;
Expand All @@ -40,12 +49,25 @@

window.addEventListener('resize', resize);
window.addEventListener('load', () => {
inputLoaderInput.addEventListener('change', () => {
// load and parse input
if (inputLoaderInput.files.length != 0) {
const reader = new FileReader();
reader.addEventListener('load', () => {
startSimulation(reader.result);
});
reader.readAsText(inputLoaderInput.files[0]);
}
});

resize();
loop();
});

const loop = () => {
window.requestAnimationFrame(loop);
/**
* @param {string} input
*/
const startSimulation = input => {

}
</script>
</body>
Expand Down
74 changes: 74 additions & 0 deletions aoc/visualizations/2023/day02.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">

<head>
<steve> return Steve.include('head', { title: 'Day 2, 2023 Visualization', debug: Steve.data.debug }) </steve>

<style>
body {
margin: auto;
overflow: hidden;
}

.container {
position: relative;
}

.container canvas {
position: absolute;
}

.overlay {
position: absolute;
padding: 20px;
}
</style>
</head>

<body>
<div class="container">
<canvas></canvas>
<div class="overlay">
<label for="input">Load Input:</label>
<input type="file" name="input">
<select id="all-games"></select>
</div>
</div>

<script>
const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');
const inputLoaderInput = document.querySelector('input');
const allGamesSelect = document.querySelector('#all-games');

const resize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}

window.addEventListener('resize', resize);
window.addEventListener('load', () => {
inputLoaderInput.addEventListener('change', () => {
// load and parse input
if (inputLoaderInput.files.length != 0) {
const reader = new FileReader();
reader.addEventListener('load', () => {
startSimulation(reader.result);
});
reader.readAsText(inputLoaderInput.files[0]);
}
});

resize();
});

/**
* @param {string} input
*/
const startSimulation = input => {

}
</script>
</body>

</html>
20 changes: 10 additions & 10 deletions index.html

Large diffs are not rendered by default.

Loading

0 comments on commit 37665e9

Please sign in to comment.