aboutsummaryrefslogtreecommitdiff
path: root/2016/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-01-12 17:36:26 +0100
committerThomas Schmucker <ts@its1.de>2025-01-12 17:36:26 +0100
commitd396dc3221fcf893c885bd9a370b9a30f25b31dd (patch)
treef003b49ea2601a54926a2ef3de3b06f0f897dc37 /2016/src
parenta8f11c0a8470c4e6b67e70219986712cd70d0de2 (diff)
downloadadvent-of-code-d396dc3221fcf893c885bd9a370b9a30f25b31dd.tar.gz
advent-of-code-d396dc3221fcf893c885bd9a370b9a30f25b31dd.tar.bz2
advent-of-code-d396dc3221fcf893c885bd9a370b9a30f25b31dd.zip
aoc 2016, days 4, 5, 6
Diffstat (limited to '2016/src')
-rw-r--r--2016/src/day04.cpp102
-rw-r--r--2016/src/day05.cpp74
-rw-r--r--2016/src/day06.cpp52
3 files changed, 228 insertions, 0 deletions
diff --git a/2016/src/day04.cpp b/2016/src/day04.cpp
new file mode 100644
index 0000000..713b16f
--- /dev/null
+++ b/2016/src/day04.cpp
@@ -0,0 +1,102 @@
1#include <fstream>
2#include <iostream>
3#include <map>
4#include <regex>
5#include <set>
6#include <sstream>
7#include <string>
8#include <tuple>
9#include <vector>
10
11using namespace std;
12
13vector<string>
14split(const string& line, const regex& sep)
15{
16 return { sregex_token_iterator(line.begin(), line.end(), sep, -1), {} };
17}
18
19vector<tuple<vector<string>, long, string>>
20read_file(string_view filename)
21{
22 fstream input{ filename };
23
24 vector<tuple<vector<string>, long, string>> result;
25
26 for ( string line; getline(input, line); ) {
27 static const regex sep{ "[\\[\\]-]" };
28
29 auto parts = split(line, sep);
30
31 auto checksum = parts.at(parts.size() - 1);
32 parts.pop_back();
33
34 auto selector = stol(parts.at(parts.size() - 1));
35 parts.pop_back();
36
37 result.emplace_back(parts, selector, checksum);
38 }
39
40 return result;
41}
42
43void
44part1(const vector<tuple<vector<string>, long, string>>& data)
45{
46 long sum = 0;
47 for ( const auto& [ids, selector, checksum]: data ) {
48 map<char, long> counts;
49 for ( const auto& id: ids ) {
50 for ( const auto chr: id ) {
51 ++counts[chr];
52 }
53 }
54
55 set<tuple<long, char>> tops;
56 for ( const auto& [chr, count]: counts ) {
57 tops.emplace(-count, chr);
58 }
59
60 string result;
61 for ( const auto& [count, chr]: tops ) {
62 result += chr;
63 }
64
65 if ( result.starts_with(checksum) ) {
66 sum += selector;
67 }
68 }
69 cout << sum << endl;
70}
71
72void
73part2(const vector<tuple<vector<string>, long, string>>& lines)
74{
75 auto rot = [](string str, int n) {
76 for ( auto& chr: str ) {
77 chr = ((chr - 'a' + n) % 26) + 'a';
78 }
79 return str;
80 };
81
82 auto is_northpole = [&](const string& str, int n) {
83 return rot(str, n) == "northpole";
84 };
85
86 for ( const auto& line: lines ) {
87 const auto& ids = get<0>(line);
88 const auto selector = get<1>(line);
89 if ( ranges::any_of(ids, [&](auto& str) { return is_northpole(str, (int) selector); }) ) {
90 cout << selector << endl;
91 return;
92 }
93 }
94}
95
96int
97main()
98{
99 auto data = read_file("data/day04.txt");
100 part1(data);
101 part2(data);
102}
diff --git a/2016/src/day05.cpp b/2016/src/day05.cpp
new file mode 100644
index 0000000..dfce653
--- /dev/null
+++ b/2016/src/day05.cpp
@@ -0,0 +1,74 @@
1// Standard C++
2#include <array>
3#include <fstream>
4#include <iostream>
5#include <string>
6
7// Standard C
8#include <cstdlib> // memcmp
9
10// System
11#include <md5.h>
12
13using namespace std;
14
15void
16part1(string_view puzzle)
17{
18 array<char, MD5_DIGEST_STRING_LENGTH> digest{};
19
20 string result;
21
22 string input;
23 for ( unsigned long counter = 0;; ++counter ) {
24 input = puzzle;
25 input += to_string(counter);
26 MD5Data(input.data(), input.size(), digest.data());
27
28 if ( memcmp(digest.data(), "00000", 5) == 0 ) {
29 result += digest.at(5);
30 if ( result.length() == 8 ) {
31 cout << result << endl;
32 return;
33 }
34 }
35 }
36}
37
38void
39part2(string_view puzzle)
40{
41 array<char, MD5_DIGEST_STRING_LENGTH> digest{};
42
43 string result = "________";
44
45 string input;
46 for ( unsigned long counter = 0;; ++counter ) {
47 input = puzzle;
48 input += to_string(counter);
49 MD5Data(input.data(), input.size(), digest.data());
50
51 if ( memcmp(digest.data(), "00000", 5) == 0 ) {
52 auto position = digest.at(5) - '0';
53 if ( position < 0 || position > 7 ) {
54 continue;
55 }
56 if ( result.at(size_t(position)) != '_' ) {
57 continue;
58 }
59 result.at(size_t(position)) = digest.at(6);
60 if ( result.find('_') == string::npos ) {
61 cout << result << endl;
62 return;
63 }
64 }
65 }
66}
67
68int
69main()
70{
71 auto puzzle = "reyedfim"s;
72 part1(puzzle);
73 part2(puzzle);
74}
diff --git a/2016/src/day06.cpp b/2016/src/day06.cpp
new file mode 100644
index 0000000..5bd0f93
--- /dev/null
+++ b/2016/src/day06.cpp
@@ -0,0 +1,52 @@
1#include <fstream>
2#include <iostream>
3#include <map>
4#include <regex>
5#include <set>
6#include <sstream>
7#include <string>
8#include <tuple>
9#include <vector>
10
11using namespace std;
12
13vector<string>
14read_file(string_view filename)
15{
16 fstream input{ filename };
17
18 vector<string> lines;
19
20 for ( string line; getline(input, line); ) {
21 lines.emplace_back(line);
22 }
23
24 return lines;
25}
26
27void
28solve(const vector<string>& lines)
29{
30 string part1;
31 string part2;
32
33 for ( size_t col = 0; col != lines[0].length(); ++col ) {
34 map<char, size_t> counts;
35 for ( const auto& line: lines ) {
36 ++counts[line[col]];
37 }
38
39 part1 += max_element(counts.begin(), counts.end(), [](auto lhs, auto rhs) { return lhs.second < rhs.second; })->first;
40 part2 += min_element(counts.begin(), counts.end(), [](auto lhs, auto rhs) { return lhs.second < rhs.second; })->first;
41 }
42
43 cout << part1 << '\n'
44 << part2 << endl;
45}
46
47int
48main()
49{
50 auto lines = read_file("data/day06.txt");
51 solve(lines);
52}