Combine AoC Repos
This commit is contained in:
221
2015/day21/main.go
Normal file
221
2015/day21/main.go
Normal file
@@ -0,0 +1,221 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var enemyHp int
|
||||
var enemyDmg int
|
||||
var enemyDef int
|
||||
|
||||
var weaponsAvailable []weapon
|
||||
var armorAvailable []armor
|
||||
var ringsAvailable []ring
|
||||
|
||||
var currWeapon, cheapWeapon, highWeapon weapon
|
||||
var currArmor, cheapArmor, highArmor armor
|
||||
var currRing1, cheapRing1, highRing1 ring
|
||||
var currRing2, cheapRing2, highRing2 ring
|
||||
|
||||
// My input: ./day21 100 8 2
|
||||
func main() {
|
||||
if len(os.Args) < 4 || os.Args[1] == "-help" {
|
||||
fmt.Println("Usage: day21 <enemy hp> <enemy dmg> <enemy def")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
enemyHp = mustAtoi(os.Args[1])
|
||||
enemyDmg = mustAtoi(os.Args[2])
|
||||
enemyDef = mustAtoi(os.Args[3])
|
||||
|
||||
cheapest := -1
|
||||
highest := -1
|
||||
initShop()
|
||||
|
||||
for wi := range weaponsAvailable {
|
||||
currWeapon = weaponsAvailable[wi]
|
||||
for ai := range armorAvailable {
|
||||
currArmor = armorAvailable[ai]
|
||||
for r1i := range ringsAvailable {
|
||||
currRing1 = ringsAvailable[r1i]
|
||||
for r2i := range ringsAvailable {
|
||||
if r2i == r1i {
|
||||
continue
|
||||
}
|
||||
currRing2 = ringsAvailable[r2i]
|
||||
if toTheDeath(false) {
|
||||
if calcCost() < cheapest || cheapest == -1 {
|
||||
cheapest = calcCost()
|
||||
saveCheapest()
|
||||
}
|
||||
} else {
|
||||
if calcCost() > highest || highest == -1 {
|
||||
highest = calcCost()
|
||||
saveHighest()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cheapest >= 0 {
|
||||
fmt.Println("Cheapest Victory: " + itoa(cheapest))
|
||||
fmt.Println("Weapon: " + cheapWeapon.name)
|
||||
fmt.Println("Armor: " + cheapArmor.name)
|
||||
fmt.Println("Ring1: " + cheapRing1.name)
|
||||
fmt.Println("Ring2: " + cheapRing2.name)
|
||||
loadCheapest()
|
||||
toTheDeath(true)
|
||||
} else {
|
||||
fmt.Println("No victory to be had.")
|
||||
}
|
||||
fmt.Println()
|
||||
if highest >= 0 {
|
||||
fmt.Println("Most Expensive Loss: " + itoa(highest))
|
||||
fmt.Println("Weapon: " + highWeapon.name)
|
||||
fmt.Println("Armor: " + highArmor.name)
|
||||
fmt.Println("Ring1: " + highRing1.name)
|
||||
fmt.Println("Ring2: " + highRing2.name)
|
||||
loadHighest()
|
||||
toTheDeath(true)
|
||||
} else {
|
||||
fmt.Println("No victory to be had.")
|
||||
}
|
||||
}
|
||||
|
||||
func loadCheapest() {
|
||||
currWeapon = cheapWeapon
|
||||
currArmor = cheapArmor
|
||||
currRing1 = cheapRing1
|
||||
currRing2 = cheapRing2
|
||||
}
|
||||
|
||||
func saveCheapest() {
|
||||
cheapWeapon = currWeapon
|
||||
cheapArmor = currArmor
|
||||
cheapRing1 = currRing1
|
||||
cheapRing2 = currRing2
|
||||
}
|
||||
|
||||
func loadHighest() {
|
||||
currWeapon = highWeapon
|
||||
currArmor = highArmor
|
||||
currRing1 = highRing1
|
||||
currRing2 = highRing2
|
||||
}
|
||||
|
||||
func saveHighest() {
|
||||
highWeapon = currWeapon
|
||||
highArmor = currArmor
|
||||
highRing1 = currRing1
|
||||
highRing2 = currRing2
|
||||
}
|
||||
|
||||
func toTheDeath(output bool) bool {
|
||||
var pHp, pDmg, pDef, eHp, eDmg, eDef int
|
||||
pHp = 100
|
||||
|
||||
pDmg += currWeapon.damage
|
||||
pDmg += currRing1.damage
|
||||
pDmg += currRing2.damage
|
||||
pDef += currArmor.defense
|
||||
pDef += currRing1.defense
|
||||
pDef += currRing2.defense
|
||||
|
||||
eHp = enemyHp
|
||||
eDmg = enemyDmg
|
||||
eDef = enemyDef
|
||||
|
||||
for pHp > 0 && eHp > 0 {
|
||||
pAtk := pDmg - eDef
|
||||
if pAtk <= 1 {
|
||||
pAtk = 1
|
||||
}
|
||||
eHp -= pAtk
|
||||
eAtk := 1
|
||||
if eHp > 0 {
|
||||
eAtk = eDmg - pDef
|
||||
if eAtk <= 1 {
|
||||
eAtk = 1
|
||||
}
|
||||
pHp -= eAtk
|
||||
}
|
||||
}
|
||||
if output {
|
||||
fmt.Print(itoa(calcCost()) + ": ")
|
||||
fmt.Print(itoa(pHp) + " to " + itoa(eHp) + " ")
|
||||
fmt.Println("DMG(" + itoa(pDmg) + ") DEF(" + itoa(pDef) + ") ")
|
||||
}
|
||||
return pHp > 0
|
||||
}
|
||||
|
||||
func calcCost() int {
|
||||
return currWeapon.cost + currArmor.cost + currRing1.cost + currRing2.cost
|
||||
}
|
||||
|
||||
func initShop() {
|
||||
addWeapon("Dagger", 8, 4)
|
||||
addWeapon("Shortsword", 10, 5)
|
||||
addWeapon("Warhammer", 25, 6)
|
||||
addWeapon("Longsword", 40, 7)
|
||||
addWeapon("Greataxe", 74, 8)
|
||||
|
||||
addArmor("Leather", 13, 1)
|
||||
addArmor("Chainmail", 31, 2)
|
||||
addArmor("Splintmail", 53, 3)
|
||||
addArmor("Bandedmail", 75, 4)
|
||||
addArmor("Platemail", 102, 5)
|
||||
addArmor("No Armor", 0, 0)
|
||||
|
||||
addRing("Damage +1", 25, 1, 0)
|
||||
addRing("Damage +2", 50, 2, 0)
|
||||
addRing("Damage +3", 100, 3, 0)
|
||||
addRing("Defense +1", 20, 0, 1)
|
||||
addRing("Defense +2", 40, 0, 2)
|
||||
addRing("Defense +3", 80, 0, 3)
|
||||
addRing("No Ring 1", 0, 0, 0)
|
||||
addRing("No Ring 2", 0, 0, 0)
|
||||
}
|
||||
func addWeapon(n string, c, dmg int) {
|
||||
weaponsAvailable = append(weaponsAvailable,
|
||||
weapon{name: n, cost: c, damage: dmg})
|
||||
}
|
||||
func addArmor(n string, c, def int) {
|
||||
armorAvailable = append(armorAvailable,
|
||||
armor{name: n, cost: c, defense: def})
|
||||
}
|
||||
func addRing(n string, c, dmg, def int) {
|
||||
ringsAvailable = append(ringsAvailable,
|
||||
ring{name: n, cost: c, damage: dmg, defense: def})
|
||||
}
|
||||
|
||||
type weapon struct {
|
||||
name string
|
||||
cost, damage int
|
||||
}
|
||||
|
||||
type armor struct {
|
||||
name string
|
||||
cost, defense int
|
||||
}
|
||||
|
||||
type ring struct {
|
||||
name string
|
||||
cost, damage, defense int
|
||||
}
|
||||
|
||||
func itoa(i int) string {
|
||||
return strconv.Itoa(i)
|
||||
}
|
||||
func mustAtoi(s string) int {
|
||||
var i int
|
||||
var err error
|
||||
if i, err = strconv.Atoi(s); err != nil {
|
||||
fmt.Println("Tried to atoi " + s)
|
||||
os.Exit(1)
|
||||
}
|
||||
return i
|
||||
}
|
108
2015/day21/problem
Normal file
108
2015/day21/problem
Normal file
@@ -0,0 +1,108 @@
|
||||
Advent of Code
|
||||
|
||||
br0xen 42*
|
||||
|
||||
• [About]
|
||||
• [Stats]
|
||||
• [Leaderboard]
|
||||
• [Settings]
|
||||
• [Log out]
|
||||
|
||||
--- Day 21: RPG Simulator 20XX ---
|
||||
|
||||
Little Henry Case got a new video game for Christmas. It's an RPG, and he's stuck on a boss.
|
||||
He needs to know what equipment to buy at the shop. He hands you the controller.
|
||||
|
||||
In this game, the player (you) and the enemy (the boss) take turns attacking. The player
|
||||
always goes first. Each attack reduces the opponent's hit points by at least 1. The first
|
||||
character at or below 0 hit points loses.
|
||||
|
||||
Damage dealt by an attacker each turn is equal to the attacker's damage score minus the
|
||||
defender's armor score. An attacker always does at least 1 damage. So, if the attacker has a
|
||||
damage score of 8, and the defender has an armor score of 3, the defender loses 5 hit points.
|
||||
If the defender had an armor score of 300, the defender would still lose 1 hit point.
|
||||
|
||||
Your damage score and armor score both start at zero. They can be increased by buying items in
|
||||
exchange for gold. You start with no items and have as much gold as you need. Your total
|
||||
damage or armor is equal to the sum of those stats from all of your items. You have 100 hit
|
||||
points.
|
||||
|
||||
Here is what the item shop is selling:
|
||||
|
||||
Weapons: Cost Damage Armor
|
||||
Dagger 8 4 0
|
||||
Shortsword 10 5 0
|
||||
Warhammer 25 6 0
|
||||
Longsword 40 7 0
|
||||
Greataxe 74 8 0
|
||||
|
||||
Armor: Cost Damage Armor
|
||||
Leather 13 0 1
|
||||
Chainmail 31 0 2
|
||||
Splintmail 53 0 3
|
||||
Bandedmail 75 0 4
|
||||
Platemail 102 0 5
|
||||
|
||||
Rings: Cost Damage Armor
|
||||
Damage +1 25 1 0
|
||||
Damage +2 50 2 0
|
||||
Damage +3 100 3 0
|
||||
Defense +1 20 0 1
|
||||
Defense +2 40 0 2
|
||||
Defense +3 80 0 3
|
||||
|
||||
You must buy exactly one weapon; no dual-wielding. Armor is optional, but you can't use more
|
||||
than one. You can buy 0-2 rings (at most one for each hand). You must use any items you buy.
|
||||
The shop only has one of each item, so you can't buy, for example, two rings of Damage +3.
|
||||
|
||||
For example, suppose you have 8 hit points, 5 damage, and 5 armor, and that the boss has 12
|
||||
hit points, 7 damage, and 2 armor:
|
||||
|
||||
• The player deals 5-2 = 3 damage; the boss goes down to 9 hit points.
|
||||
• The boss deals 7-5 = 2 damage; the player goes down to 6 hit points.
|
||||
• The player deals 5-2 = 3 damage; the boss goes down to 6 hit points.
|
||||
• The boss deals 7-5 = 2 damage; the player goes down to 4 hit points.
|
||||
• The player deals 5-2 = 3 damage; the boss goes down to 3 hit points.
|
||||
• The boss deals 7-5 = 2 damage; the player goes down to 2 hit points.
|
||||
• The player deals 5-2 = 3 damage; the boss goes down to 0 hit points.
|
||||
|
||||
In this scenario, the player wins! (Barely.)
|
||||
|
||||
You have 100 hit points. The boss's actual stats are in your puzzle input. What is the least
|
||||
amount of gold you can spend and still win the fight?
|
||||
|
||||
Your puzzle answer was 91.
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
Turns out the shopkeeper is working with the boss, and can persuade you to buy whatever items
|
||||
he wants. The other rules still apply, and he still only has one of each item.
|
||||
|
||||
What is the most amount of gold you can spend and still lose the fight?
|
||||
|
||||
Your puzzle answer was 158.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your advent calendar and try another puzzle.
|
||||
|
||||
If you still want to see it, you can get your puzzle input.
|
||||
|
||||
You can also [Shareon Twitter Google+ Reddit] this puzzle.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
. http://adventofcode.com/
|
||||
. http://adventofcode.com/about
|
||||
. http://adventofcode.com/stats
|
||||
. http://adventofcode.com/leaderboard
|
||||
. http://adventofcode.com/settings
|
||||
. http://adventofcode.com/auth/logout
|
||||
. https://en.wikipedia.org/wiki/Role-playing_video_game
|
||||
. https://en.wikipedia.org/wiki/Game_controller
|
||||
. http://adventofcode.com/
|
||||
. http://adventofcode.com/day/21/input
|
||||
. https://twitter.com/intent/tweet?text=I%27ve+completed+%22RPG+Simulator+20XX%22+%2D+Day+21+%2D+Advent+of+Code&url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F21&related=ericwastl&hashtags=AdventOfCode
|
||||
. https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F21
|
||||
. http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F21&title=I%27ve+completed+%22RPG+Simulator+20XX%22+%2D+Day+21+%2D+Advent+of+Code
|
Reference in New Issue
Block a user