Skip to content

Commit

Permalink
Day 15 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jongeorge1 committed Feb 21, 2019
1 parent 62505c2 commit 43ad1f6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AoC2018.Runner/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"AoC2018.Runner": {
"commandName": "Project",
"commandLineArgs": "15 1"
"commandLineArgs": "15 2"
}
}
}
2 changes: 2 additions & 0 deletions AoC2018.Solutions/Day15/MapSpaceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace AoC2018.Solutions.Day15
{
using System.Linq;

public static class MapSpaceExtensions
{
public static bool IsEmpty(this MapSpace space)
Expand Down
60 changes: 60 additions & 0 deletions AoC2018.Solutions/Day15/Part02.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;

namespace AoC2018.Solutions.Day15
{
public class Part02 : ISolution
{
public string Solve(string input)
{
////Console.WriteLine(currentState.ToString());

int attackStrength = 4;

while (true)
{
////Console.WriteLine($"Running with hit points = {attackStrength}");

var currentState = State.Parse(input);
Elf[] elves = currentState.GetUnits<Elf>();
int elfCount = elves.Length;

// Set their hit count
foreach (Elf current in elves)
{
current.AttackStrength = attackStrength;
}

int rounds = 0;
bool deadElves = false;

while (!deadElves)
{
bool roundCompleted = currentState.Round();

if (roundCompleted)
{
rounds++;
}

deadElves = currentState.CountUnits<Elf>() < elfCount;

if (currentState.IsCombatEnded())
{
break;
}
}

if (deadElves)
{
////Console.WriteLine($"Elves died during round {rounds}");
attackStrength++;
}
else
{
return (rounds * currentState.TotalRemainingHitPoints()).ToString();
}
}

}
}
}
12 changes: 12 additions & 0 deletions AoC2018.Solutions/Day15/StateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,17 @@ public static int TotalRemainingHitPoints(this State state)
{
return state.Map.Sum(x => x?.Unit?.HitPoints ?? 0);
}

public static int CountUnits<T>(this State state)
where T : Unit
{
return state.Map.Count(x => x?.Unit?.GetType() == typeof(T));
}

public static T[] GetUnits<T>(this State state)
where T : Unit
{
return state.Map.Where(x => x?.Unit?.GetType() == typeof(T)).Select(x => (T)x.Unit).ToArray();
}
}
}

0 comments on commit 43ad1f6

Please sign in to comment.