Skip to content

Commit

Permalink
finished 2015 again!
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingAP committed Nov 29, 2023
1 parent ea9d754 commit ce6619c
Show file tree
Hide file tree
Showing 6 changed files with 5,705 additions and 5,397 deletions.
8 changes: 8 additions & 0 deletions aoc/puzzles/2015/day22/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Advent of Code 2015 - Day 22: [Wizard Simulator 20XX](https://adventofcode.com/2015/day/22)

## [Write Up](https://codingap.github.io/advent-of-code/writeups/2015/day22)
## Results
|| **Part 1** | **Part 2** |
|:--:|:---:|:---:|
| **Results** | 1824 | 1937 |
| **Time (in ms)** | 16225.73 | 1061.72 |
26 changes: 20 additions & 6 deletions aoc/puzzles/2015/day22/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const spells = {
}
}

const doRound = (state, spell) => {
const doRound = (state, spell, constantDamage) => {
// do current spell effects at player's turn
state.currentSpells.forEach(spellInfo => {
spells[spellInfo.name].activate(state.player, state.boss);
Expand All @@ -59,6 +59,9 @@ const doRound = (state, spell) => {
state.manaCost += spells[spell].cost;
state.currentSpells.push({ name: spell, timer: spells[spell].timer });

// remove constant damage (for part 2)
state.player.health -= constantDamage;

// remove expired spells
let ongoing = state.currentSpells.map(spellInfo => {
if (spellInfo.timer <= 0) {
Expand Down Expand Up @@ -92,7 +95,7 @@ const doRound = (state, spell) => {
state.player.health -= Math.max(state.boss.damage - state.player.armor, 1);
}

const simulateAllBattles = startingState => {
const simulateAllBattles = (startingState, constantDamage) => {
let minimum = Infinity;

const simulateBattle = (state, depth) => {
Expand All @@ -106,16 +109,18 @@ const simulateAllBattles = startingState => {
}

// find all spells that can be casted and try them
// WOW: the bit I was missing was that spells can be casted on the same turn they end :(
let availableSpells = Object.keys(spells).filter(spell => {
for (let i = 0; i < state.currentSpells.length; i++) {
if (spell == state.currentSpells[i].name || state.player.mana < spells[spell].cost) return false;
if (state.player.mana < spells[spell].cost) return false;
if (spell == state.currentSpells[i].name && state.currentSpells[i].timer != 1) return false;
}
return true;
});

availableSpells.forEach(spellName => {
let newState = structuredClone(state);
doRound(newState, spellName);
doRound(newState, spellName, constantDamage);
simulateBattle(newState, depth + 1);
});
}
Expand All @@ -140,7 +145,7 @@ const part1 = async input => {
manaCost: 0
}

return simulateAllBattles(startingState);
return simulateAllBattles(startingState, 0);
}

/**
Expand All @@ -150,7 +155,16 @@ const part1 = async input => {
* @returns {Promise<string | number>} the result of part 2
*/
const part2 = async input => {
return 0;
let [bossHitPoints, bossDamage] = input.split(/\n/g).map(line => parseInt(line.split(': ')[1]));

const startingState = {
player: { health: 50, armor: 0, mana: 500 },
boss: { health: bossHitPoints, damage: bossDamage },
currentSpells: [],
manaCost: 0
}

return simulateAllBattles(startingState, 1);
}

export { part1, part2 };
2 changes: 1 addition & 1 deletion aoc/puzzles/2015/day25/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
|| **Part 1** | **Part 2** |
|:--:|:---:|:---:|
| **Results** | 8997277 | 2015 DONE! |
| **Time (in ms)** | 380.81 | 0.01 |
| **Time (in ms)** | 543.96 | 0.02 |
4 changes: 2 additions & 2 deletions aoc/src/puzzles.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
},
{
"title": "Wizard Simulator 20XX",
"stars": 0
"stars": 2
},
{
"title": "Opening the Turing Lock",
Expand All @@ -98,7 +98,7 @@
},
{
"title": "Let It Snow",
"stars": 1
"stars": 2
}
],
"2016": [
Expand Down
Loading

0 comments on commit ce6619c

Please sign in to comment.