aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-20 09:50:44 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-20 09:50:44 +0800
commit8a59303f3bc876bfa2d0db5aca548233ea15272d (patch)
treec6b5a424524b51ffac4c80e1106571f5b5f9287d /src
parent33fc0bfa0bd56c0632e5395aec30192ddb5feb7f (diff)
downloadadvent-of-code-8a59303f3bc876bfa2d0db5aca548233ea15272d.tar.gz
advent-of-code-8a59303f3bc876bfa2d0db5aca548233ea15272d.zip
day14 part1
Diffstat (limited to 'src')
-rw-r--r--src/2015/day14/README.md11
-rw-r--r--src/2015/day14/aoc.cpp10
-rw-r--r--src/2015/day14/aoc.h66
3 files changed, 84 insertions, 3 deletions
diff --git a/src/2015/day14/README.md b/src/2015/day14/README.md
index 88dad48..f023938 100644
--- a/src/2015/day14/README.md
+++ b/src/2015/day14/README.md
@@ -15,4 +15,15 @@ In this example, after the 1000th second, both reindeer are resting, and Comet i
Given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, what distance has the winning reindeer traveled?
+--- Part Two ---
+
+Seeing how reindeer move in bursts, Santa decides he's not pleased with the old scoring system.
+
+Instead, at the end of each second, he awards one point to the reindeer currently in the lead. (If there are multiple reindeer tied for the lead, they each get one point.) He keeps the traditional 2503 second time limit, of course, as doing otherwise would be entirely ridiculous.
+
+Given the example reindeer from above, after the first second, Dancer is in the lead and gets one point. He stays in the lead until several seconds into Comet's second burst: after the 140th second, Comet pulls into the lead and gets his first point. Of course, since Dancer had been in the lead for the 139 seconds before that, he has accumulated 139 points by the 140th second.
+
+After the 1000th second, Dancer has accumulated 689 points, while poor Comet, our old champion, only has 312. So, with the new scoring system, Dancer would win (if the race ended at 1000 seconds).
+
+Again given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, how many points does the winning reindeer have?
diff --git a/src/2015/day14/aoc.cpp b/src/2015/day14/aoc.cpp
index 016d160..0f3859c 100644
--- a/src/2015/day14/aoc.cpp
+++ b/src/2015/day14/aoc.cpp
@@ -1,5 +1,13 @@
#include "aoc.h"
namespace aoc2015 {
-int day14(int) { return 0; }
+int day14(line_view file, int seconds) {
+ olympics games;
+ per_line(file, [&games](line_view lv) {
+ games.parse(lv);
+ return true;
+ });
+ auto p = games.best(seconds);
+ return p.second;
+}
} // namespace aoc2015
diff --git a/src/2015/day14/aoc.h b/src/2015/day14/aoc.h
index 89699fc..1d00d73 100644
--- a/src/2015/day14/aoc.h
+++ b/src/2015/day14/aoc.h
@@ -1,7 +1,69 @@
#pragma once
#include "common.h"
+#include <vector>
namespace aoc2015 {
-int day14(int);
-}
+struct reindeer {
+ line_view name;
+ int speed;
+ int fly_seconds;
+ int rest_seconds;
+
+ friend std::ostream& operator<<(std::ostream& o, const reindeer& r) {
+ o << r.name << " " << r.speed << " " << r.fly_seconds << " " << r.rest_seconds;
+ return o;
+ }
+};
+
+struct olympics {
+ std::vector<reindeer> reindeers;
+
+ int get_number(const char* p) {
+ int d{0};
+ while (*p != ' ') {
+ d = d * 10 + *p - '0';
+ p++;
+ }
+ return d;
+ }
+
+ int kilometers(const reindeer& r, int s) {
+ int period = r.fly_seconds + r.rest_seconds;
+ int x = s / period;
+ int y = s % period;
+ if (y > r.fly_seconds) {
+ y = r.fly_seconds;
+ }
+ return x * (r.speed * r.fly_seconds) + r.speed * y;
+ }
+
+ std::pair<line_view, int> best(int s) {
+ std::pair<line_view, int> d{{}, INT32_MIN};
+ for (auto& r : reindeers) {
+ auto x = kilometers(r, s);
+ std::cout << r.name << ": " << x << std::endl;
+ if (x > d.second) {
+ d = {r.name, x};
+ }
+ }
+ return d;
+ }
+
+ void parse(line_view lv) {
+ const char* p1 = lv.contains("can");
+ const char* p2 = lv.contains("for");
+ line_view tail = {p2 + 3, lv.line + lv.length};
+ const char* p3 = tail.contains("for");
+ reindeer r;
+ r.name = line_view{lv.line, p1 - 1};
+ r.speed = get_number(p1 + 8);
+ r.fly_seconds = get_number(p2 + 4);
+ r.rest_seconds = get_number(p3 + 4);
+ // std::cout << r << std::endl;
+ reindeers.push_back(r);
+ }
+};
+
+int day14(line_view, int);
+} // namespace aoc2015