-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc4.cpp
128 lines (103 loc) · 3.43 KB
/
aoc4.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "aoc.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <cstring>
namespace aoc4 {
std::vector<std::vector<std::string>>
loadInput(std::istream & is) {
std::vector<std::vector<std::string>> result{};
std::string line;
while (std::getline(is, line)) {
std::stringstream ss(line);
std::string token{};
std::vector<std::string> set{};
while (std::getline(ss, token, ' ')) {
set.push_back(token);
}
result.push_back(set);
}
return result;
}
void
solve(int argc, char** argv) {
solvePart1(argc, argv);
solvePart2(argc, argv);
}
/*
--- Day 4: High-Entropy Passphrases ---
A new system policy has been put in place that requires all accounts to use
a passphrase instead of simply a password. A passphrase consists of a
series of words (lowercase letters) separated by spaces.
To ensure security, a valid passphrase must contain no duplicate words.
For example:
aa bb cc dd ee is valid.
aa bb cc dd aa is not valid - the word aa appears more than once.
aa bb cc dd aaa is valid - aa and aaa count as different words.
The system's full passphrase list is available as your puzzle input.
How many passphrases are valid?
*/
void
solvePart1(int argc, char** argv) {
std::ifstream ifs("input/aoc4.in");
std::vector<std::vector<std::string>> data = loadInput(ifs);
int valid = 0;
std::vector<std::vector<std::string>>::iterator passwords = data.begin();
while (passwords != data.end()) {
std::set<std::string> tokens{};
for (std::string t : *passwords) {
tokens.insert(t);
}
if (tokens.size() == (*passwords).size()) {
valid++;
}
passwords++;
}
std::printf("04 P 1: %d\n", valid);
}
/*
--- Part Two ---
For added security, yet another system policy has been put in place.
Now, a valid passphrase must contain no two words that are anagrams
of each other - that is, a passphrase is invalid if any word's
letters can be rearranged to form any other word in the passphrase.
For example:
abcde fghij is a valid passphrase.
abcde xyz ecdab is not valid - the letters from the third word can be rearranged to form the first word.
a ab abc abd abf abj is a valid passphrase, because all letters need to be used when forming another word.
iiii oiii ooii oooi oooo is valid.
oiii ioii iioi iiio is not valid - any of these words can be rearranged to form any other word.
Under this new system policy, how many passphrases are valid?
*/
int
cmp(const void* m1, const void* m2) {
return *((const char*) m1) - *((const char*) m2);
}
void
solvePart2(int argc, char** argv) {
std::ifstream ifs("input/aoc4.in");
std::vector<std::vector<std::string>> data = loadInput(ifs);
int valid = 0;
std::vector<std::vector<std::string>>::iterator passwords = data.begin();
while (passwords != data.end()) {
std::set<std::string> tokens{};
for (std::string t : *passwords) {
char tmp[t.size()];
std::strcpy(tmp, t.c_str());
std::qsort(tmp, t.size(), sizeof(char), cmp);
tokens.insert(std::string{tmp});
}
if (tokens.size() == (*passwords).size()) {
valid++;
}
passwords++;
}
std::printf("04 P 2: %d\n", valid);
}
}