aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-04 10:55:15 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-04 10:55:15 +0800
commitdb6c4680263981594cacd464e4abfea38586b9d5 (patch)
tree1a83e4ce89b58ebb15afe7896020125c238c9d12 /src
parente3d42a551d838e3fef28a132912f33740095c20b (diff)
downloadadvent-of-code-db6c4680263981594cacd464e4abfea38586b9d5.tar.gz
advent-of-code-db6c4680263981594cacd464e4abfea38586b9d5.zip
2018 day1
Diffstat (limited to 'src')
-rw-r--r--src/2018/day1/README.md46
-rw-r--r--src/2018/day1/aoc.cpp42
-rw-r--r--src/2018/day1/aoc.h2
-rw-r--r--src/2018/day1/input1030
4 files changed, 1118 insertions, 2 deletions
diff --git a/src/2018/day1/README.md b/src/2018/day1/README.md
index 8b13789..e562d37 100644
--- a/src/2018/day1/README.md
+++ b/src/2018/day1/README.md
@@ -1 +1,47 @@
+--- Day 1: Chronal Calibration ---
+"We've detected some temporal anomalies," one of Santa's Elves at the Temporal Anomaly Research and Detection Instrument Station tells you. She sounded pretty worried when she called you down here. "At 500-year intervals into the past, someone has been changing Santa's history!"
+"The good news is that the changes won't propagate to our time stream for another 25 days, and we have a device" - she attaches something to your wrist - "that will let you fix the changes with no such propagation delay. It's configured to send you 500 years further into the past every few days; that was the best we could do on such short notice."
+
+"The bad news is that we are detecting roughly fifty anomalies throughout time; the device will indicate fixed anomalies with stars. The other bad news is that we only have one device and you're the best person for the job! Good lu--" She taps a button on the device and you suddenly feel like you're falling. To save Christmas, you need to get all fifty stars by December 25th.
+
+Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
+
+After feeling like you've been falling for a few minutes, you look at the device's tiny screen. "Error: Device must be calibrated before first use. Frequency drift detected. Cannot maintain destination lock." Below the message, the device shows a sequence of changes in frequency (your puzzle input). A value like +6 means the current frequency increases by 6; a value like -3 means the current frequency decreases by 3.
+
+For example, if the device displays frequency changes of +1, -2, +3, +1, then starting from a frequency of zero, the following changes would occur:
+
+Current frequency 0, change of +1; resulting frequency 1.
+Current frequency 1, change of -2; resulting frequency -1.
+Current frequency -1, change of +3; resulting frequency 2.
+Current frequency 2, change of +1; resulting frequency 3.
+In this example, the resulting frequency is 3.
+
+Here are other example situations:
+
++1, +1, +1 results in 3
++1, +1, -2 results in 0
+-1, -2, -3 results in -6
+Starting with a frequency of zero, what is the resulting frequency after all of the changes in frequency have been applied?
+
+--- Part Two ---
+You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice.
+
+For example, using the same list of changes above, the device would loop as follows:
+
+Current frequency 0, change of +1; resulting frequency 1.
+Current frequency 1, change of -2; resulting frequency -1.
+Current frequency -1, change of +3; resulting frequency 2.
+Current frequency 2, change of +1; resulting frequency 3.
+(At this point, the device continues from the start of the list.)
+Current frequency 3, change of +1; resulting frequency 4.
+Current frequency 4, change of -2; resulting frequency 2, which has already been seen.
+In this example, the first frequency reached twice is 2. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.
+
+Here are other examples:
+
++1, -1 first reaches 0 twice.
++3, +3, +4, -2, -4 first reaches 10 twice.
+-6, +3, +8, +5, -6 first reaches 5 twice.
++7, +7, -2, -7, -4 first reaches 14 twice.
+What is the first frequency your device reaches twice?
diff --git a/src/2018/day1/aoc.cpp b/src/2018/day1/aoc.cpp
index 1d092ff..aea1e97 100644
--- a/src/2018/day1/aoc.cpp
+++ b/src/2018/day1/aoc.cpp
@@ -1,3 +1,43 @@
#include "aoc.h"
+#include <set>
-namespace aoc2018 {}
+namespace aoc2018 {
+int parse_day1(line_view lv) {
+ const char* p = lv.line;
+ int sign = *p++ == '+' ? 1 : -1;
+ int s{0};
+ while (*p >= '0' && *p <= '9') {
+ s = s * 10 + *p - '0';
+ p++;
+ }
+ return sign * s;
+}
+
+int day1(line_view file) {
+ int sum{0};
+
+ per_line(file, [&sum](line_view lv) {
+ sum += parse_day1(lv);
+ return true;
+ });
+ return sum;
+}
+
+int day1part2(line_view file) {
+ std::set<int> frequencies;
+
+ int f{0};
+ bool found{false};
+ while (!found) {
+ per_line(file, [&f, &frequencies, &found](line_view lv) {
+ f += parse_day1(lv);
+ // printf("%d\n", f);
+ auto p = frequencies.insert(f);
+ found = !p.second;
+ return p.second;
+ });
+ }
+ return f;
+}
+
+} // namespace aoc2018
diff --git a/src/2018/day1/aoc.h b/src/2018/day1/aoc.h
index 4bfdbf3..efd635c 100644
--- a/src/2018/day1/aoc.h
+++ b/src/2018/day1/aoc.h
@@ -3,4 +3,6 @@
namespace aoc2018 {
+int day1(line_view);
+int day1part2(line_view);
}
diff --git a/src/2018/day1/input b/src/2018/day1/input
index 8b13789..1bfa4c1 100644
--- a/src/2018/day1/input
+++ b/src/2018/day1/input
@@ -1 +1,1029 @@
-
+-17
++14
++10
+-2
+-1
++6
++6
++7
++1
++9
++8
+-13
+-7
++17
+-4
+-16
+-6
+-11
+-7
+-20
++3
++2
+-10
+-5
++3
++5
++13
+-3
+-2
+-4
++19
+-6
++14
+-4
++3
++6
++17
++4
+-18
++16
++19
+-3
+-4
++18
+-2
++7
+-10
+-8
++10
+-6
++11
++5
+-6
++14
+-16
+-5
++15
++2
++14
+-19
++13
++1
+-6
+-18
++20
++16
+-10
++8
+-9
++3
++9
++16
+-9
+-3
+-6
++5
++15
+-1
++12
+-7
+-2
+-14
++20
+-11
++24
+-12
++1
+-5
++7
++14
++8
+-16
+-17
+-24
++17
++1
++8
+-12
++1
+-8
+-12
+-10
++16
+-3
+-16
+-7
++14
+-13
+-19
++8
+-1
+-4
+-13
+-2
++10
+-4
+-17
+-23
++21
++18
+-20
++16
+-22
+-16
+-6
+-16
+-6
++5
++5
+-19
+-18
++14
+-10
+-15
++4
++19
+-11
+-15
++16
+-17
+-8
+-15
++12
+-17
++3
+-13
++3
++3
+-8
+-15
+-14
++11
++13
+-3
+-4
++9
++21
+-9
++12
+-2
++6
++6
++8
+-2
++16
+-6
++4
+-1
++15
++1
++4
++6
+-14
++2
+-19
+-18
++6
+-3
++5
++8
+-19
+-4
+-17
+-16
+-13
+-16
++5
+-6
+-2
++7
+-13
+-8
+-13
+-19
++5
++12
+-13
++19
+-2
++16
++4
++18
++9
+-1
+-4
+-16
++19
++19
++4
+-5
+-14
++12
+-7
+-1
+-24
+-8
+-9
+-18
+-16
++2
+-13
+-7
++16
+-7
++4
+-12
+-9
+-10
++14
++18
+-16
++6
++17
+-6
++10
++5
++5
++10
++5
+-13
+-9
+-1
++2
++12
+-15
++6
++7
++14
++11
+-7
++13
++10
++19
++17
++7
++10
+-9
+-10
++12
+-9
++21
++26
++18
+-11
+-1
+-11
++22
++12
+-3
+-5
+-17
++16
++3
++24
++14
++8
++20
+-6
+-7
+-22
++1
+-2
++24
++23
+-15
++25
++37
+-15
++6
++40
++13
++3
++8
+-18
++6
+-4
++13
++18
+-4
++5
+-11
++4
++14
+-16
++5
+-11
+-15
++7
++1
++4
++21
++18
++15
+-18
+-9
+-8
++18
+-4
++9
++18
+-12
++1
++14
+-8
++1
+-11
+-15
++8
++6
+-20
+-4
++12
+-1
+-3
+-13
+-17
++8
++20
+-10
++30
++12
+-7
+-1
+-15
++12
+-4
++18
+-8
++16
++4
++13
+-8
+-11
+-5
++18
++8
++17
++14
+-16
++6
++12
++5
++19
++2
++13
+-19
++8
++3
+-16
+-20
++16
++5
+-13
+-14
+-11
++14
++14
+-5
++16
++5
++8
++13
+-16
+-18
+-1
++15
+-11
+-10
+-19
+-10
+-7
+-4
++7
+-11
+-2
++21
+-7
++5
+-15
+-17
+-7
++18
+-5
++12
++3
++8
++15
+-7
+-17
++18
+-10
++5
++17
+-8
++1
+-18
+-18
+-6
+-13
+-18
++6
+-11
+-19
+-2
+-15
+-8
+-18
+-3
+-6
++3
++26
++14
++11
++10
++46
++4
++4
+-2
++21
++22
++14
++8
+-4
++20
+-2
++4
++23
++11
++22
++16
+-8
++11
++22
+-17
++4
+-8
+-13
+-15
++11
++19
+-23
+-5
+-5
++47
++9
+-12
++18
++15
++15
++8
++8
++37
+-1
+-15
+-17
+-10
++32
++22
++15
+-10
++8
++128
++27
++19
++18
++18
++7
++34
++18
++10
+-3
+-5
++22
++4
++15
++2
++3
++9
+-6
+-35
+-22
+-1
++4
++55
+-2
++80
++21
+-2
++53
++33
++13
+-60
+-28
+-44
+-30
++2
+-17
++12
++249
++26
+-186
++72428
+-2
++13
++17
++15
+-18
++1
++7
++3
+-14
++1
+-4
+-2
++3
+-18
++2
++14
+-13
+-8
+-21
+-4
+-2
+-8
+-5
++18
++12
+-9
++2
++13
++6
+-9
++17
++9
++8
++14
+-3
++7
+-3
++16
+-15
++10
++18
++12
+-9
++12
++7
++18
+-17
++1
++14
++9
+-1
++8
++13
+-4
+-12
+-8
++10
++6
++2
++8
++2
++6
++16
+-20
++10
++2
++19
+-6
+-14
++17
++6
++7
++3
++4
++19
++16
++17
++1
++1
++4
++19
+-12
++2
++12
+-6
++11
++16
+-1
++10
+-3
++15
++17
++2
++7
++9
+-14
++16
++15
++13
++10
+-12
+-8
++13
++1
+-11
+-10
+-19
++16
++2
+-8
++4
+-20
+-18
++19
++7
++15
++11
++17
++7
+-14
++6
+-12
+-19
+-8
++6
+-4
++8
++6
++3
++17
++8
++16
+-1
++4
+-18
++3
+-10
++5
++15
+-4
++8
+-1
+-11
+-8
++14
++17
++6
+-12
++17
+-13
++9
+-5
++19
++10
+-19
++17
++13
++12
+-4
+-2
+-11
++14
++14
++17
++12
+-15
+-10
+-13
++2
+-18
+-17
++13
+-1
+-2
++6
+-9
++13
++10
++8
++11
++14
+-15
+-16
+-11
+-18
+-9
++10
+-12
++4
+-3
++12
++13
+-28
+-4
++16
++20
++12
++19
++14
+-17
++11
++15
++12
+-18
+-2
++6
++17
++21
+-22
+-17
+-18
+-19
++4
++3
++2
++2
+-6
+-15
+-25
+-4
+-12
+-12
+-12
++21
+-12
++6
+-4
+-3
+-10
+-9
+-16
+-13
+-19
+-17
+-2
+-15
++9
++15
++3
+-17
++10
+-9
+-16
++9
++9
++1
++4
+-12
++6
++18
+-29
+-8
+-16
+-16
++10
+-14
+-4
++6
++15
++5
++7
++12
+-4
+-18
+-10
+-19
++17
+-21
+-2
+-16
+-18
+-15
++13
+-12
+-18
+-6
+-3
+-19
+-13
+-14
++10
+-14
++12
+-3
+-17
+-14
+-18
+-15
++4
+-15
++19
+-10
++3
+-18
++2
++2
+-9
++13
++6
+-13
++6
+-22
+-23
+-19
++18
++19
++16
+-21
++18
++18
++13
+-11
++7
++21
+-11
++15
+-1
++9
+-4
+-12
++15
+-19
+-10
++3
++1
++24
++14
++6
++17
++19
++9
++11
+-12
+-10
++8
++16
++13
+-18
++2
++10
++16
++17
++6
++4
+-1
++4
++5
++9
++4
+-15
+-1
++23
++21
++6
+-15
+-11
++10
+-17
++11
++14
+-2
++1
++19
+-15
++18
+-6
+-18
++17
++13
++6
++2
++17
+-23
+-16
+-8
++21
++62
+-15
+-8
++5
++24
++36
++10
++2
++19
+-8
++18
++2
++13
+-1
+-8
++14
++35
++21
++7
+-14
++19
+-3
++39
++11
++12
++25
+-5
+-6
+-7
++14
++15
++22
+-19
++12
+-18
++19
+-17
+-9
+-13
++11
++45
++21
+-18
++12
+-14
++26
++10
++4
++9
+-1
++3
++14
++5
+-2
++7
+-14
+-2
+-12
++25
++19
++3
++7
+-28
+-6
++15
++3
++11
++10
+-22
+-6
+-2
++3
++21
++14
+-12
++17
+-6
+-32
+-28
++2
++19
+-16
++8
+-4
+-20
++1
++3
++17
+-36
+-21
+-3
++2
++14
+-29
++20
+-6
++25
+-11
++34
+-76
++10
++30
+-42
+-32
+-25
+-12
++8
++7
+-35
++17
+-5
+-10
++3
++10
+-23
+-37
+-15
++14
++18
+-88
+-38
++17
++13
++50
+-56
+-25
+-26
+-1
+-29
++19
+-73214