Skip to content

Instantly share code, notes, and snippets.

@johnboker
Last active December 22, 2015 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnboker/797359b9d60c5629430c to your computer and use it in GitHub Desktop.
Save johnboker/797359b9d60c5629430c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCodeDay21
{
class Program
{
static void Main(string[] args)
{
var shop = new[]
{
new Equipment("Dagger", 8, 4, 0, Equipment.EquipmentType.Weapon),
new Equipment("Shortsword", 10, 5, 0, Equipment.EquipmentType.Weapon),
new Equipment("Warhammer", 25, 6, 0, Equipment.EquipmentType.Weapon),
new Equipment("Longsword", 40, 7, 0, Equipment.EquipmentType.Weapon),
new Equipment("Greataxe", 74, 8, 0, Equipment.EquipmentType.Weapon),
new Equipment("No Armor 1", 0, 0, 0, Equipment.EquipmentType.Armor),
new Equipment("Leather", 13, 0, 1, Equipment.EquipmentType.Armor),
new Equipment("Chainmail", 31, 0, 2, Equipment.EquipmentType.Armor),
new Equipment("Splintmail", 53, 0, 3, Equipment.EquipmentType.Armor),
new Equipment("Bandedmail", 75, 0, 4, Equipment.EquipmentType.Armor),
new Equipment("Platemail", 102, 0, 5, Equipment.EquipmentType.Armor),
new Equipment("No Ring 1", 0, 0, 0, Equipment.EquipmentType.Ring),
new Equipment("No Ring 2", 0, 0, 0, Equipment.EquipmentType.Ring),
new Equipment("Damage +1", 25, 1, 0, Equipment.EquipmentType.Ring),
new Equipment("Damage +2", 50, 2, 0, Equipment.EquipmentType.Ring),
new Equipment("Damage +3", 100, 3, 0, Equipment.EquipmentType.Ring),
new Equipment("Defense +1", 20, 0, 1, Equipment.EquipmentType.Ring),
new Equipment("Defense +2", 40, 0, 2, Equipment.EquipmentType.Ring),
new Equipment("Defense +3", 80, 0, 3, Equipment.EquipmentType.Ring)
};
var bag = new List<Equipment>();
var recordWin = 0;
var recordLoss = 0;
var minCost = 99999999;
var maxCostLoss = 0;
foreach (var weapon in shop.Where(a => a.Type == Equipment.EquipmentType.Weapon))
{
foreach (var armor in shop.Where(a => a.Type == Equipment.EquipmentType.Armor))
{
foreach (var ring1 in shop.Where(a => a.Type == Equipment.EquipmentType.Ring))
{
foreach (var ring2 in shop.Where(a => a.Type == Equipment.EquipmentType.Ring && a != ring1))
{
var equip = new[] { weapon, armor, ring1, ring2 };
var player = new Player { HitPoints = 100, Armor = equip.Sum(a => a.Armor), Damage = equip.Sum(a => a.Damage) };
var boss = new Player { HitPoints = 109, Damage = 8, Armor = 2 };
var isWin = PlayGame(player, boss);
recordWin += isWin ? 1 : 0;
recordLoss += isWin ? 0 : 1;
var cost = equip.Sum(a => a.Cost);
if (isWin)
{
minCost = Math.Min(minCost, cost);
}
else
{
if (cost > maxCostLoss)
{
Console.WriteLine();
Console.WriteLine(string.Join("\n", equip.Select(a => a.Name + "\n\t\t" + a.Cost + "\t" + a.Damage + "\t" + a.Armor)));
Console.WriteLine($"Cost: {cost}");
Console.WriteLine();
}
maxCostLoss = Math.Max(maxCostLoss, cost);
}
}
}
}
}
Console.WriteLine($"Games Played: {recordLoss + recordWin} Won: {recordWin} Lost: {recordLoss} Cheapest Win: {minCost} Expensive Loss: {maxCostLoss}");
Console.ReadLine();
}
public static bool PlayGame(Player player, Player boss)
{
var myTurn = true;
while (true)
{
if (myTurn)
{
player.Hit(boss);
}
else
{
boss.Hit(player);
}
if (player.IsDead || boss.IsDead)
{
break;
}
myTurn = !myTurn;
}
return boss.IsDead;
}
}
public class Player
{
public int HitPoints { get; set; }
public int Damage { get; set; }
public int Armor { get; set; }
public void Hit(Player p)
{
p.HitPoints -= Math.Max(Damage - p.Armor, 1);
}
public bool IsDead => HitPoints <= 0;
}
public class Equipment
{
public Equipment(string name, int cost, int damage, int armor, EquipmentType equipmentType)
{
Name = name;
Cost = cost;
Damage = damage;
Armor = armor;
Type = equipmentType;
}
public string Name { get; set; }
public int Cost { get; set; }
public int Damage { get; set; }
public int Armor { get; set; }
public EquipmentType Type { get; set; }
public enum EquipmentType
{
Weapon,
Armor,
Ring
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment