Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author /u/Philboyd_Studge on 12/20/2015.
- */
- public class Advent21 {
- static List<Item> weapons = new ArrayList<>();
- static List<Item> armor = new ArrayList<>();
- static List<Item> rings = new ArrayList<>();
- static {
- weapons.add(new Item(8, 4, 0));
- weapons.add(new Item(10, 5, 0));
- weapons.add(new Item(25, 6, 0));
- weapons.add(new Item(40, 7, 0));
- weapons.add(new Item(74, 8, 0));
- armor.add(new Item(0, 0, 0));
- armor.add(new Item(13, 0, 1));
- armor.add(new Item(31, 0, 2));
- armor.add(new Item(53, 0, 3));
- armor.add(new Item(75, 0, 4));
- armor.add(new Item(102, 0, 5));
- rings.add(new Item(0, 0, 0));
- rings.add(new Item(0, 0, 0));
- rings.add(new Item(25, 1, 0));
- rings.add(new Item(50, 2, 0));
- rings.add(new Item(100,3, 0));
- rings.add(new Item(20, 0, 1));
- rings.add(new Item(40, 0, 2));
- rings.add(new Item(80, 0 ,3));
- }
- static class Player {
- String name;
- int hp;
- int currentHp;
- int damage;
- int armor;
- public Player(String name, int hp, int damage, int armor) {
- this.name = name;
- this.hp = hp;
- this.currentHp = hp;
- this.damage = damage;
- this.armor = armor;
- }
- public void reset() {
- currentHp = hp;
- damage = 0;
- armor = 0;
- }
- public void reset(int damage, int armor) {
- currentHp = hp;
- this.damage = damage;
- this.armor = armor;
- }
- public int equip(Item item) {
- damage += item.damage;
- armor += item.armor;
- return item.cost;
- }
- public void attack(Player p) {
- int attackDamage = this.damage - p.armor;
- if (attackDamage <= 1) attackDamage = 1;
- p.currentHp -= attackDamage;
- //System.out.println("The " + name + " deals " + attackDamage +
- // " damage. The " + p.name + " goes down to " + p.currentHp + " hit points.");
- }
- public boolean isDead() {
- return currentHp <= 0;
- }
- }
- static class Item {
- int cost;
- int damage;
- int armor;
- public Item(int cost, int damage, int armor) {
- this.cost = cost;
- this.damage = damage;
- this.armor = armor;
- }
- }
- public static void turn(Player a, Player b) {
- a.attack(b);
- }
- public static Player battle(Player a, Player b) {
- while (!a.isDead() && !b.isDead()) {
- turn(a, b);
- if (getWinner(a, b)==null) {
- turn(b, a);
- }
- }
- return getWinner(a, b);
- }
- public static Player getWinner(Player a, Player b) {
- if (a.isDead()) return b;
- if (b.isDead()) return a;
- return null;
- }
- public static void main(String[] args) {
- Player boss = new Player("Boss", 109, 8, 2);
- Player hero = new Player("Hero", 100,0,0);
- int cost = 0;
- int min = Integer.MAX_VALUE;
- int max = Integer.MIN_VALUE;
- for (Item each : weapons) {
- for (Item every : armor) {
- for (int i = 0; i < rings.size()-1; i++) {
- for (int j = i + 1; j < rings.size(); j++) {
- hero.reset();
- boss.reset(8, 2);
- cost = 0;
- cost += hero.equip(each);
- cost += hero.equip(every);
- cost += hero.equip(rings.get(i));
- cost += hero.equip(rings.get(j));
- Player winner = battle(hero, boss);
- //System.out.println(winner.name + " is the winner!");
- //System.out.println(cost);
- if (winner.name.equals("Hero") && cost < min) {
- min = cost;
- }
- if (winner.name.equals("Boss") && cost > max) {
- max = cost;
- }
- }
- }
- }
- }
- System.out.printf("Min (Part 1): %d%n", min);
- System.out.printf("Max (Part 2): %d%n", max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment