Xander Sigler 4bd2fe8e87
All checks were successful
continuous-integration/drone/push Build is passing
Added Day 6
2023-12-09 20:37:27 -08:00

88 lines
2.7 KiB
C#

using AdventOfCode.Common;
using AdventOfCode.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace AdventOfCode._2023
{
public class Day6 : AOCDay
{
protected override AOCResponse ExecutePartA()
{
var splitInput = this.GetSplitInput().Select(x => x.Split(':')[1].Trim().ReplaceMultipleSpaces()).ToArray();
var races = new List<Race>();
var timeArr = splitInput[0].Split(" ").Select(x => int.Parse(x)).ToArray();
var distArr = splitInput[1].Split(" ").Select(x => int.Parse(x)).ToArray();
for (int i = 0; i < timeArr.Length; i++)
{
races.Add(new Race(i, timeArr[i], distArr[i]));
}
var result = 1;
foreach (var race in races)
{
var raceOptions = race.CalculateOptimalTimes();
result *= raceOptions.Count;
Log($"Race {race.RaceId} has {raceOptions.Count} race options!");
}
this.Answer = result;
return this._response;
}
protected override AOCResponse ExecutePartB()
{
var splitInput = this.GetSplitInput().Select(x => x.Split(':')[1].Trim().Replace(" ", "")).ToArray();
var bigRace = new Race(1, long.Parse(splitInput[0]), long.Parse(splitInput[1]));
var raceOptions = bigRace.CalculateOptimalTimes();
Log($"Race {bigRace.RaceId} has {raceOptions.Count} race options!");
this.Answer = raceOptions.Count;
return this._response;
}
}
class Race
{
public int RaceId { get; set; }
private long _time;
private long _distance;
public Race(int raceId, long time, long distance)
{
RaceId = raceId;
_time = time;
_distance = distance;
}
public List<RaceOption> CalculateOptimalTimes()
{
var raceOption = new List<RaceOption>();
for (long i = 0; i < _time; i++)
{
var chargeTime = i;
var calcDistance = (_time - i) * chargeTime;
if (calcDistance > _distance)
{
raceOption.Add(new RaceOption()
{
SecondsPressed = chargeTime,
DistanceGone = calcDistance
});
}
}
return raceOption;
}
}
class RaceOption
{
public long SecondsPressed { get; set; }
public long DistanceGone { get; set; }
}
}