diff --git a/adventofcode.tests/adventofcode.com/2015/Testing2015day0022.cs b/adventofcode.tests/adventofcode.com/2015/Testing2015day0022.cs new file mode 100644 index 0000000..7821f81 --- /dev/null +++ b/adventofcode.tests/adventofcode.com/2015/Testing2015day0022.cs @@ -0,0 +1,19 @@ + +using adventofcode.adventofcode.com._2015; + +namespace adventofcode.tests.adventofcode.com._2015; + +public class Testing2015day0022 +{ + [TestCase] + public void TestWhoWins() + { + var result = Solution2015day0022.WhoWins( + new Solution2015day0022.Player(10, 250), + new int[] { 3, 0 }, + new Solution2015day0022.Player(13, 0, 8)); + result.Winner.Should().Be(0); + } + + private const string MainInput = @"{input}"; +} diff --git a/adventofcode/adventofcode.com/2015/Solution2015day0022.cs b/adventofcode/adventofcode.com/2015/Solution2015day0022.cs new file mode 100644 index 0000000..2a8ca34 --- /dev/null +++ b/adventofcode/adventofcode.com/2015/Solution2015day0022.cs @@ -0,0 +1,46 @@ + +namespace adventofcode.adventofcode.com._2015; + +public static class Solution2015day0022 +{ + public record Effect(int Turns, int Armor, int Damage, int Mana, int Heal); + + private record Spell(int ManaCost, int Damage, int Heal, Effect? Effect = null); + + public record Player(int HitPoints, int Mana = 0, int Damage = 0) + { + public List Effects { get; init; } = new List(); + } + + private static Spell[] Spells = new Spell[] { + new Spell(53, 4, 0), // 0. Magic Missle + new Spell(73, 2, 2), // 1. Drain + new Spell(113, 0, 0, new Effect(6, 7, 0, 0, 0)), // 2. Shield + new Spell(173, 0, 0, new Effect(6, 0, 3, 0, 0)), // 3. Poison + new Spell(113, 0, 0, new Effect(5, 0, 0, 101, 0))// 4. Recharge + }; + + public static (int Winner, int Mana) WhoWins(Player player, int[] spellsToCast, Player boss) + => Enumerable.Range(0, Math.Max(player.HitPoints, boss.HitPoints)) + .TakeWhile(round => + { + (player, boss) = round % 2 == 0 ? PlayerMove(round, player, spellsToCast, boss) : BossMove(player, boss); + return player.HitPoints > 0 && boss.HitPoints > 0; + }).ToList() + .And(rounds => + ( + Winner: player.HitPoints > 0 ? 0 : 1, + Mana: rounds.Append(rounds.Last() + 1) + .Select(idx => idx < spellsToCast.Length ? Spells[spellsToCast[idx]].ManaCost : 0).Sum() + )); + + private static (Player player, Player boss) BossMove(Player player, Player boss) + => (player with { + HitPoints = player.HitPoints - boss.Damage + }, boss); + + private static (Player player, Player boss) PlayerMove(int round, Player player, int[] spellsToCast, Player boss) + { + throw new NotImplementedException(); + } +} diff --git a/prepare_solution/Program.cs b/prepare_solution/Program.cs index 9131f77..0d23236 100644 --- a/prepare_solution/Program.cs +++ b/prepare_solution/Program.cs @@ -2,7 +2,7 @@ using System.Reflection; -var url = "https://adventofcode.com/2015/day/22"; +var url = "https://adventofcode.com/2015/day/23"; var year = "2015"; var solutionFolder = GetSolutionFolder(); var libFolder = Path.Combine(solutionFolder, "adventofcode");