-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
366 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!! | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.