Guest User

Untitled

a guest
Dec 21st, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. /**
  5.  * @author /u/Philboyd_Studge on 12/20/2015.
  6.  */
  7. public class Advent21 {
  8.     static List<Item> weapons = new ArrayList<>();
  9.     static List<Item> armor = new ArrayList<>();
  10.     static List<Item> rings = new ArrayList<>();
  11.  
  12.     static {
  13.         weapons.add(new Item(8, 4, 0));
  14.         weapons.add(new Item(10, 5, 0));
  15.         weapons.add(new Item(25, 6, 0));
  16.         weapons.add(new Item(40, 7, 0));
  17.         weapons.add(new Item(74, 8, 0));
  18.  
  19.         armor.add(new Item(0, 0, 0));
  20.         armor.add(new Item(13, 0, 1));
  21.         armor.add(new Item(31, 0, 2));
  22.         armor.add(new Item(53, 0, 3));
  23.         armor.add(new Item(75, 0, 4));
  24.         armor.add(new Item(102, 0, 5));
  25.  
  26.         rings.add(new Item(0, 0, 0));
  27.         rings.add(new Item(0, 0, 0));
  28.         rings.add(new Item(25, 1, 0));
  29.         rings.add(new Item(50, 2, 0));
  30.         rings.add(new Item(100,3, 0));
  31.         rings.add(new Item(20, 0, 1));
  32.         rings.add(new Item(40, 0, 2));
  33.         rings.add(new Item(80, 0 ,3));
  34.  
  35.     }
  36.  
  37.     static class Player {
  38.         String name;
  39.         int hp;
  40.         int currentHp;
  41.         int damage;
  42.         int armor;
  43.  
  44.         public Player(String name, int hp, int damage, int armor) {
  45.             this.name = name;
  46.             this.hp = hp;
  47.             this.currentHp = hp;
  48.             this.damage = damage;
  49.             this.armor = armor;
  50.         }
  51.  
  52.         public void reset() {
  53.             currentHp = hp;
  54.             damage = 0;
  55.             armor = 0;
  56.         }
  57.  
  58.         public void reset(int damage, int armor) {
  59.             currentHp = hp;
  60.             this.damage = damage;
  61.             this.armor = armor;
  62.         }
  63.  
  64.         public int equip(Item item) {
  65.             damage += item.damage;
  66.             armor += item.armor;
  67.             return item.cost;
  68.         }
  69.  
  70.         public void attack(Player p) {
  71.             int attackDamage = this.damage - p.armor;
  72.             if (attackDamage <= 1) attackDamage = 1;
  73.             p.currentHp -= attackDamage;
  74.             //System.out.println("The " + name + " deals " + attackDamage +
  75.             //        " damage. The " + p.name + " goes down to " + p.currentHp + " hit points.");
  76.  
  77.         }
  78.  
  79.         public boolean isDead() {
  80.             return currentHp <= 0;
  81.         }
  82.     }
  83.  
  84.     static class Item {
  85.         int cost;
  86.         int damage;
  87.         int armor;
  88.  
  89.         public Item(int cost, int damage, int armor) {
  90.             this.cost = cost;
  91.             this.damage = damage;
  92.             this.armor = armor;
  93.         }
  94.     }
  95.  
  96.     public static void turn(Player a, Player b) {
  97.         a.attack(b);
  98.     }
  99.  
  100.     public static Player battle(Player a, Player b) {
  101.         while (!a.isDead() && !b.isDead()) {
  102.             turn(a, b);
  103.             if (getWinner(a, b)==null) {
  104.                 turn(b, a);
  105.             }
  106.         }
  107.         return getWinner(a, b);
  108.     }
  109.  
  110.     public static Player getWinner(Player a, Player b) {
  111.         if (a.isDead()) return b;
  112.         if (b.isDead()) return a;
  113.         return null;
  114.     }
  115.  
  116.  
  117.     public static void main(String[] args) {
  118.         Player boss = new Player("Boss", 109, 8, 2);
  119.         Player hero = new Player("Hero", 100,0,0);
  120.         int cost = 0;
  121.         int min = Integer.MAX_VALUE;
  122.         int max = Integer.MIN_VALUE;
  123.         for (Item each : weapons) {
  124.             for (Item every : armor) {
  125.                 for (int i = 0; i < rings.size()-1; i++) {
  126.                     for (int j = i + 1; j < rings.size(); j++) {
  127.                         hero.reset();
  128.                         boss.reset(8, 2);
  129.                         cost = 0;
  130.                         cost += hero.equip(each);
  131.                         cost += hero.equip(every);
  132.                         cost += hero.equip(rings.get(i));
  133.                         cost += hero.equip(rings.get(j));
  134.                         Player winner = battle(hero, boss);
  135.                         //System.out.println(winner.name + " is the winner!");
  136.                         //System.out.println(cost);
  137.                         if (winner.name.equals("Hero") && cost < min) {
  138.                             min = cost;
  139.                         }
  140.                         if (winner.name.equals("Boss") && cost > max) {
  141.                             max = cost;
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         System.out.printf("Min (Part 1): %d%n", min);
  148.         System.out.printf("Max (Part 2): %d%n", max);
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment