Skip to content

Instantly share code, notes, and snippets.

@desrtfx
Created December 21, 2015 09:58
Show Gist options
  • Save desrtfx/df6c589394cc798b9c1e to your computer and use it in GitHub Desktop.
Save desrtfx/df6c589394cc798b9c1e to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class Day21 {
static final int BOSS_HIT_POINTS = 104;
static final int BOSS_DAMAGE = 8;
static final int BOSS_ARMOR = 1;
List<Item> weapons = new ArrayList<>();
List<Item> armors = new ArrayList<>();
List<Item> rings = new ArrayList<>();
class Item {
String name;
int cost;
int damage;
int armor;
public Item(String name, int cost, int damage, int armor) {
this.name = name;
this.cost = cost;
this.damage = damage;
this.armor = armor;
}
public Item(String data) {
this.name = data.substring(0, 12).trim();
this.cost = Integer.parseInt(data.substring(12, 15).trim());
this.damage = Integer.parseInt(data.substring(15, 21).trim());
this.armor = Integer.parseInt(data.substring(21).trim());
}
}
class Player {
String name;
int hitPoints;
int damage;
int armor;
int cost;
public Player(String name, int hitPoints, int damage, int armor) {
this.name = name;
this.hitPoints = hitPoints;
this.damage = damage;
this.armor = armor;
cost = 0;
}
public void equip(Item item) {
this.armor += item.armor;
this.damage += item.damage;
this.cost += item.cost;
}
public void fight(Player other) {
int hit = Math.max(damage - other.armor, 1); // deal at least 1 hit
// Point
other.hitPoints -= hit; // adjust other's hit points
}
public boolean isDead() {
return hitPoints <= 0;
}
@Override
public String toString() {
return String.format("Player: %s\tHit Points: %3d\tDamage: %2d\tArmor: %2d\n", name, hitPoints, damage,
armor);
}
}
class Game {
Player[] players = new Player[2];
public Game() {
players[0] = new Player("Henry", 100, 0, 0);
players[1] = new Player("Boss", Day21.BOSS_HIT_POINTS, Day21.BOSS_DAMAGE, Day21.BOSS_ARMOR);
}
public void equipPlayer(Item item) {
players[0].equip(item);
}
public int tick(int player) { // one fight round either Human -> Boss or
// Boss -> Human
players[player].fight(players[1 - player]);
if (players[1 - player].isDead()) {
return player;
}
return -1; // no one dies - no winner
}
public int runGame() {
while (!players[0].isDead() && !players[1].isDead()) {
for (int i = 0; i < 2; i++) {
tick(i);
if (players[1].isDead()) {
return 0;
}
if (players[0].isDead()) {
return 1;
}
}
}
if (players[0].isDead()) {
return 1;
}
return 0;
}
}
public Day21() {
weapons.add(new Item("Dagger 8 4 0"));
weapons.add(new Item("Shortsword 10 5 0"));
weapons.add(new Item("Warhammer 25 6 0"));
weapons.add(new Item("Longsword 40 7 0"));
weapons.add(new Item("Greataxe 74 8 0"));
armors.add(new Item("None 0 0 0"));
armors.add(new Item("Leather 13 0 1"));
armors.add(new Item("Chainmail 31 0 2"));
armors.add(new Item("Splintmail 53 0 3"));
armors.add(new Item("Bandedmail 75 0 4"));
armors.add(new Item("Platemail 102 0 5"));
rings.add(new Item("None 0 0 0"));
rings.add(new Item("None 0 0 0"));
rings.add(new Item("Damage +1 25 1 0"));
rings.add(new Item("Damage +2 50 2 0"));
rings.add(new Item("Damage +3 100 3 0"));
rings.add(new Item("Defense +1 20 0 1"));
rings.add(new Item("Defense +2 40 0 2"));
rings.add(new Item("Defense +3 80 0 3"));
}
public void solve() {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int cost = 0;
// Brute force attack:
// Run through all weapons
for (Item weapon : weapons) {
// run through all armors
for (Item armor : armors) {
// run through all the rings (0..2)
for (int ring1 = 0; ring1 < rings.size() - 1; ring1++) {
for (int ring2 = ring1 + 1; ring2 < rings.size(); ring2++) {
// Start new game round
Game game = new Game();
// equip human
game.players[0].equip(weapon);
game.players[0].equip(armor);
game.players[0].equip(rings.get(ring1));
game.players[0].equip(rings.get(ring2));
cost = game.players[0].cost;
int winner = game.runGame();
// If human has won
if (winner == 0) {
if (min >= cost) {
min = cost;
}
}
// Part 2 - Max Gold to lose
if (winner == 1) {
if (max <= cost) {
max = cost;
}
}
}
}
}
}
System.out.println("Part1: Minimum cost is: " + min);
System.out.println("Part2: Maximum cost is: " + max);
}
public static void main(String[] args) {
Day21 day21 = new Day21();
day21.solve();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment