-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD06.cs
90 lines (71 loc) · 2.65 KB
/
D06.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace AOC2023;
/// <summary>
/// Day 6: Wait For It.
/// </summary>
public class D06
{
private readonly AOCHttpClient _client = new AOCHttpClient(6);
public void Part1()
{
string input = _client.RetrieveFile();
/*input = @"Time: 7 15 30
Distance: 9 40 200";*/
Match timeMatch = Regex.Match(input, @"Time:\s*(\d+\s*)+");
List<int> times = Regex.Matches(timeMatch.Value, @"\d+")
.Select(m => int.Parse(m.Value))
.ToList();
Match distanceMatch = Regex.Match(input, @"Distance:\s*(\d+\s*)+");
List<int> distances = Regex.Matches(distanceMatch.Value, @"\d+")
.Select(m => int.Parse(m.Value))
.ToList();
List<TimeDistance> timeDistances = times
.Zip(distances, (time, distance) => new TimeDistance(time, distance))
.ToList();
long result = timeDistances
.Select(CountWinningCombinations)
.Aggregate((current, next) => current * next);
Console.WriteLine(result);
}
public void Part2()
{
string input = _client.RetrieveFile();
/*input = @"Time: 7 15 30
Distance: 9 40 200";*/
Match timeMatch = Regex.Match(input, @"Time:\s*(\d+\s*)+");
List<int> times = Regex.Matches(timeMatch.Value, @"\d+")
.Select(m => int.Parse(m.Value))
.ToList();
Match distanceMatch = Regex.Match(input, @"Distance:\s*(\d+\s*)+");
List<int> distances = Regex.Matches(distanceMatch.Value, @"\d+")
.Select(m => int.Parse(m.Value))
.ToList();
TimeDistance timeDistance = new TimeDistance(long.Parse(string.Join("", times)), long.Parse(string.Join("", distances)));
long result = CountWinningCombinations(timeDistance);
Console.WriteLine(result);
}
private long CountWinningCombinations(TimeDistance timeDistance)
{
long winningCombinations = 0;
for (long speed = 1; speed < timeDistance.Time; speed++)
{
long holdTime = timeDistance.Time - speed;
long totalDistance = holdTime * speed;
if (totalDistance > timeDistance.Distance)
{
winningCombinations++;
}
}
return winningCombinations;
}
private record TimeDistance(long Time, long Distance)
{
public override string ToString()
{
return $"Time: {Time} | Distance: {Distance}";
}
}
}