Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Advent of Code
- // Day 21: RPG Simulator 20XX
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace AdventOfCode
- {
- public class AdventOfCode21
- {
- public static void Main(string[] args)
- {
- Item[] weapons = new Item[]
- {
- new Item("Dagger", 8, 4, 0),
- new Item("Shortsword", 10, 5, 0),
- new Item("Warhammer", 25, 6, 0),
- new Item("Longsword", 40, 7, 0),
- new Item("Greataxe", 74, 8, 0)
- };
- Item[] armor = new Item[]
- {
- new Item("Leather", 13, 0, 1),
- new Item("Chainmail", 31, 0, 2),
- new Item("Splintmail", 53, 0, 3),
- new Item("Bandedmail", 75, 0, 4),
- new Item("Platemail", 102, 0, 5)
- };
- Item[] rings = new Item[]
- {
- new Item("Damage +1", 25, 1, 0),
- new Item("Damage +2", 50, 2, 0),
- new Item("Damage +3", 100, 3, 0),
- new Item("Defense +1", 20, 0, 1),
- new Item("Defense +2", 40, 0, 2),
- new Item("Defense +3", 80, 0, 3)
- };
- List<Player> wins = new List<Player>();
- List<Player> losses = new List<Player>();
- for (int i=0; i < weapons.Length; i++)
- {
- for (int j = -1; j < armor.Length; j++)
- {
- for (int k = -1; k < rings.Length; k++)
- {
- for (int l = -1; l <= k; l++)
- {
- if (k >= 0 && k == l) { continue; }
- Player boss = new Player(104) { Damage = 8, Armor = 1 };
- Player player = new Player(100);
- player.Equip(weapons[i]);
- if (j >= 0) { player.Equip(armor[j]); }
- if (k >= 0) { player.Equip(rings[k]); }
- if (l >= 0) { player.Equip(rings[l]); }
- if (Shots(player, boss) >= Shots(boss, player))
- {
- wins.Add(player);
- }
- else
- {
- losses.Add(player);
- }
- }
- }
- }
- }
- Player minWinner = wins.OrderBy(p => p.Spent).First();
- Console.WriteLine("Min winner: " + minWinner.Spent + " (" + String.Join(", ", minWinner.Arsenal.Select(i => i.Name)) + ")");
- Player maxLoser = losses.OrderBy(p => p.Spent).Last();
- Console.WriteLine("Max loser: " + maxLoser.Spent + " (" + String.Join(", ", maxLoser.Arsenal.Select(i => i.Name)) + ")");
- Console.ReadKey();
- }
- private static int Shots(Player a, Player b)
- {
- int shotDamage = Math.Max(b.Damage - a.Armor, 1);
- int shots = a.HitPoints / shotDamage;
- int rem = a.HitPoints % shotDamage;
- return shots + (rem > 0 ? 1 : 0);
- }
- private class Player
- {
- public int HitPoints { get; set; }
- public int Damage { get; set; }
- public int Armor { get; set; }
- public int Spent { get; private set; }
- public List<Item> Arsenal { get; private set; }
- public Player(int hitPoints)
- {
- this.HitPoints = hitPoints;
- Arsenal = new List<Item>();
- }
- public void Equip(Item item)
- {
- this.Damage += item.Damage;
- this.Armor += item.Armor;
- this.Spent += item.Cost;
- this.Arsenal.Add(item);
- }
- }
- private class Item
- {
- public string Name { get; }
- public int Cost { get; }
- public int Damage { get; }
- public int Armor { get; }
- public Item(string name, int cost, int damage, int armor)
- {
- this.Name = name;
- this.Cost = cost;
- this.Damage = damage;
- this.Armor = armor;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement