Compare commits
	
		
			23 Commits
		
	
	
		
			64cb7fb370
			...
			9b2497f31e
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9b2497f31e | |||
| ada97e5ad6 | |||
| b457e10a35 | |||
| 961bc666d6 | |||
| f61f1567cf | |||
| 49633b8504 | |||
| 8fd9bcd63a | |||
| a09f89896d | |||
| d12175371e | |||
| 3da784f97d | |||
| b39f312e9f | |||
| f663a57db8 | |||
| 33ba8d72f4 | |||
| 29a122c3ff | |||
| dafe576fc9 | |||
| 900f01913f | |||
| f952db1e0f | |||
| 1ec3c9c939 | |||
| 27e9e60089 | |||
| d06a694543 | |||
| d655185336 | |||
| f468af10f4 | |||
| 4d1477d7c1 | 
							
								
								
									
										179
									
								
								day22.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										179
									
								
								day22.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,179 @@ | |||||||
|  | '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); | ||||||
		Reference in New Issue
	
	Block a user