diff --git a/day_6/input.txt b/day_6/input.txt new file mode 100644 index 0000000..db24621 --- /dev/null +++ b/day_6/input.txt @@ -0,0 +1,2 @@ +Time: 41 96 88 94 +Distance: 214 1789 1127 1055 diff --git a/day_6/sample.txt b/day_6/sample.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/day_6/sample.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/day_6/solution.d b/day_6/solution.d new file mode 100644 index 0000000..205b87e --- /dev/null +++ b/day_6/solution.d @@ -0,0 +1,51 @@ +import std.stdio; +import std.array; +import std.algorithm; +import std.string; +import std.conv; +import std.math; + +struct Race { + long time, dist; +} + +Race[] parseRaces(string filename) { + string[] lines = File(filename).byLineCopy.array; + long[] times = lines[0].split(":")[1].strip.split.map!(to!long).array; + long[] distances = lines[1].split(":")[1].strip.split.map!(to!long).array; + Race[] result = new Race[](times.length); + foreach (size_t i, long time; times) { + result[i] = Race(time, distances[i]); + } + return result; +} + +Race parseKernedRace(string filename) { + string[] lines = File(filename).byLineCopy.array; + long time = lines[0].split(":")[1].strip.split.join.to!long; + long dist = lines[1].split(":")[1].strip.split.join.to!long; + return Race(time, dist); +} + +long computeSolutions(in Race r) { + double D = r.time * r.time - 4 * r.dist; + double t1 = (-r.time + sqrt(D)) / -2; + double t2 = (-r.time - sqrt(D)) / -2; + long minWinningTime = cast(long) (ceil(t1) == t1 ? t1 + 1 : ceil(t1)); + long maxWinningTime = cast(long) (floor(t2) == t2 ? t2 - 1 : floor(t2)); + return maxWinningTime - minWinningTime + 1; +} + +void part1() { + parseRaces("input.txt").map!computeSolutions.fold!((a, b) => a * b).writeln; + +} + +void part2() { + parseKernedRace("input.txt").computeSolutions.writeln; +} + +void main() { + part1(); + part2(); +} \ No newline at end of file