Compare commits

..

23 Commits

Author SHA1 Message Date
64cb7fb370 y2024d10 2024-12-10 10:32:05 +01:00
99430e8e26 y2024d9 2024-12-09 13:20:27 +01:00
0dec0bbf82 y2024d8 2024-12-08 16:35:33 +01:00
2a91587ae5 y2024d7 2024-12-08 11:40:52 +01:00
dbf878baf1 y2024d6 2024-12-08 11:40:24 +01:00
013b85fbbf y2016d5 2024-12-06 10:04:09 +01:00
4dcf7001ee y2016d4 2024-12-05 12:54:39 +01:00
05c88e5e56 y2016d3 2024-12-05 10:47:26 +01:00
4c62525fa7 y2016d2 2024-12-05 10:21:06 +01:00
fa52f5bd13 y2024d5 2024-12-05 09:13:24 +01:00
a141026754 y2016d1 genuinely don't understand why this was so hard 2024-12-04 15:44:56 +01:00
6abd6c7d66 fixed justfile as I had some issues when I wanted to generated 2024 2024-12-04 14:39:44 +01:00
b429585457 y2015d25 2024-12-04 14:33:50 +01:00
5a17373244 y2024d4 2024-12-04 11:19:28 +01:00
eae352413b y2015d24 pretty easy also but had to use itertools instead of my own combinations/permutations impl because of an overflow 2024-12-03 15:28:12 +01:00
0b4e2449a1 y2015d23 refreshingly easy 2024-12-03 13:33:49 +01:00
daead25413 y2024d3p2 simplified, anyway, I lost too much time on it already 2024-12-03 13:03:49 +01:00
5b2e4a38d1 y2024d3p2 2024-12-03 12:53:58 +01:00
4e6399f5e2 y2024d3 p2 still incorrect 2024-12-03 10:52:57 +01:00
1db00c45db y2024d3 use regex for part 1 2024-12-03 10:21:13 +01:00
a6731d4a67 y2024d3 second try part 2 2024-12-03 10:12:20 +01:00
5bd059970e y2024d3 first try part 2 2024-12-03 10:11:59 +01:00
9329c9f77a y2015d22 finally, alot of small things to watch out for 2024-12-02 15:51:01 +01:00

179
day22.js
View File

@ -1,179 +0,0 @@
'use strict';
var bossStats = {
hp: 71,
damageAmt: 10,
}
class Player {
constructor(initial, isWizard) {
this.history = [];
this.initial = initial;
this.isWizard = !!isWizard;
if (this.isWizard) {
this.spells = [
{
cost: 53,
effect: (m, o) => o.damage(4),
},
{
cost: 73,
effect: (m, o) => { o.damage(2); m.hp += 2; },
},
{
cost: 113,
start: (m, o) => m.armor += 7,
effect: (m, o) => { },
end: (m, o) => m.armor -= 7,
duration: 6,
},
{
cost: 173,
effect: (m, o) => o.damage(3),
duration: 6,
},
{
cost: 229,
effect: (m, o) => m.mana += 101,
duration: 5,
},
];
}
this.start();
}
attack(opponent, spellIdx) {
if (!this.isWizard) {
opponent.damage(this.damageAmt);
} else {
this.history.push(spellIdx);
var spell = this.spells[spellIdx];
this.spent += spell.cost;
this.mana -= spell.cost;
if (spell.duration) {
var newSpell = {
idx: spellIdx,
effect: spell.effect,
duration: spell.duration,
};
if (spell.start) {
spell.start(this, opponent);
}
if (spell.end) {
newSpell.end = spell.end;
}
this.activeSpells.push(newSpell);
} else {
spell.effect(this, opponent);
}
}
}
damage(n) {
this.hp -= Math.max(1, n - this.armor);
}
duplicate() {
var newPlayer = new Player(this.initial, this.isWizard);
newPlayer.hp = this.hp;
newPlayer.spent = this.spent;
newPlayer.armor = this.armor;
newPlayer.turn = this.turn;
for (var i = 0; i < this.activeSpells.length; i++) {
newPlayer.activeSpells.push(Object.assign({}, this.activeSpells[i]));
}
for (var i = 0; i < this.history.length; i++) {
newPlayer.history.push(this.history[i]);
}
if (this.isWizard)
newPlayer.mana = this.mana;
else
newPlayer.damageAmt = this.damageAmt;
return newPlayer;
}
takeTurn(opponent) {
this.turn++;
for (var i = 0; i < this.activeSpells.length; i++) {
var spell = this.activeSpells[i];
if (spell.duration > 0) {
spell.effect(this, opponent);
spell.duration--;
if (spell.duration === 0 && spell.end) {
spell.end(this, opponent);
}
}
}
}
start() {
this.hp = this.initial.hp;
this.spent = 0;
this.armor = 0;
this.turn = 0;
this.activeSpells = [];
if (this.isWizard)
this.mana = this.initial.mana;
else
this.damageAmt = this.initial.damageAmt;
}
}
var me = new Player({ hp: 50, mana: 500 }, true);
var boss = new Player(bossStats);
var cheapestSpent = Infinity;
function playAllGames(me, boss, partTwo, depth) {
depth = depth || 0;
for (var i = 0; i < me.spells.length; i++) {
var spellMatch = false;
for (var j = 0; j < me.activeSpells.length; j++) {
if (me.activeSpells[j].duration > 1 && i === me.activeSpells[j].idx) {
spellMatch = true;;
}
}
if (spellMatch)
continue;
if (me.spells[i].cost > me.mana) {
continue;
}
var newMe = me.duplicate();
var newBoss = boss.duplicate();
if (partTwo)
newMe.hp--;
newMe.takeTurn(newBoss);
newBoss.takeTurn(newMe);
newMe.attack(newBoss, i);
newMe.takeTurn(newBoss);
newBoss.takeTurn(newMe);
newBoss.attack(newMe);
if (newBoss.hp <= 0) {
cheapestSpent = Math.min(cheapestSpent, newMe.spent);
}
if (newMe.hp > (partTwo ? 1 : 0) && newBoss.hp > 0 && newMe.spent < cheapestSpent)
playAllGames(newMe, newBoss, partTwo, depth + 1);
}
}
playAllGames(me, boss);
console.log('Part One:', cheapestSpent);
cheapestSpent = Infinity;
playAllGames(me, boss, true);
console.log('Part Two:', cheapestSpent);