Added 2022 (cpp)
This commit is contained in:
33
2022/cpp/06/Makefile
Normal file
33
2022/cpp/06/Makefile
Normal file
@ -0,0 +1,33 @@
|
||||
PART := 1
|
||||
CXXFLAGS := -Wall -O3 -DPART=$(PART)
|
||||
OBJ_DIR = build
|
||||
OBJ = answer.o
|
||||
|
||||
.PHONY = clean asdf sample1 sample2
|
||||
|
||||
asdf: result
|
||||
@echo "------- RESULT -------"
|
||||
@./result "input" | xclip -selection c
|
||||
@xclip -selection c -o
|
||||
|
||||
check: test
|
||||
./test
|
||||
|
||||
test: $(OBJ_DIR)/$(OBJ)
|
||||
|
||||
verbose: clean
|
||||
$(eval CXXFLAGS += -DVERBOSE)
|
||||
|
||||
$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp
|
||||
|
||||
$(OBJ_DIR)/%.o: %.cpp
|
||||
@echo "Compiling $<..."
|
||||
@mkdir -p $(@D)
|
||||
gcc $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
result: $(OBJ_DIR)/$(OBJ)
|
||||
|
||||
clean:
|
||||
-rm -f *.o
|
||||
-rm -rf $(OBJ_DIR)/
|
||||
|
58
2022/cpp/06/answer.cpp
Normal file
58
2022/cpp/06/answer.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
// AOC - 2022 - 06
|
||||
#include "answer.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <unordered_set>
|
||||
|
||||
Input get_input(const char* filename) {
|
||||
std::ifstream stream(filename);
|
||||
std::string str;
|
||||
getline(stream, str);
|
||||
return {str};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool contains_duplicates(std::deque<T> q) {
|
||||
std::unordered_set<T> set;
|
||||
for (auto& elem : q) {
|
||||
if (set.find(elem) != set.end())
|
||||
return true;
|
||||
set.insert(elem);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string get_result(Input input) {
|
||||
std::string ret;
|
||||
std::stringstream stream(input.str);
|
||||
std::deque<char> q;
|
||||
char c;
|
||||
#if PART == 1
|
||||
#define Q_SIZE 4
|
||||
#else
|
||||
#define Q_SIZE 14
|
||||
#endif
|
||||
for (int i = 0; i < Q_SIZE - 1; i++) {
|
||||
stream >> c;
|
||||
q.push_back(c);
|
||||
}
|
||||
|
||||
int i = Q_SIZE - 1;
|
||||
while (stream.rdbuf()->in_avail()) {
|
||||
i++;
|
||||
stream >> c;
|
||||
q.push_back(c);
|
||||
if (!contains_duplicates(q)) {
|
||||
std::cout << q << "is duplicate free" << std::endl;
|
||||
break;
|
||||
}
|
||||
std::cout << q << " DOES contain duplicates" << std::endl;
|
||||
q.pop_front();
|
||||
}
|
||||
ret = std::to_string(i);
|
||||
return ret;
|
||||
}
|
29
2022/cpp/06/answer.hpp
Normal file
29
2022/cpp/06/answer.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include "lib.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#ifndef VERBOSE
|
||||
#define VERBOSE 0
|
||||
#endif
|
||||
|
||||
#ifndef PART
|
||||
#define PART 2
|
||||
#endif
|
||||
|
||||
struct Input {
|
||||
std::string str;
|
||||
|
||||
bool operator==(const Input& rhs) const {
|
||||
return this->str == rhs.str;
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, Input& inp) {
|
||||
os << inp.str;
|
||||
return os;
|
||||
}
|
||||
|
||||
Input get_input(const char*);
|
||||
|
||||
std::string get_result(Input);
|
BIN
2022/cpp/06/build/answer.o
Normal file
BIN
2022/cpp/06/build/answer.o
Normal file
Binary file not shown.
1
2022/cpp/06/input
Normal file
1
2022/cpp/06/input
Normal file
@ -0,0 +1 @@
|
||||
hlfhfzffqnnrlnnvnmmgbgwgttbppcrcnnmdmfdmmgwwrrqnrrscrctcbttvcvtvvhchjhccjgjttmddplplqplqlbqlblrrbrvvprpffpmmzpmpcczjzzbwwfssvrrvggncgncgcwczzswwqqjjflffpwfpwpbwpwpdpbpvvqffcfcjffjllncczfzzmhzzmddgdrgrwwjzzdjjsnjsjfsjsjhhcchlccchqchhzzpnngdgndnpnppsdsggbvgvgpprqrqmmlzmzllvrrcvclcwczcqqcdcfcqqmmzbzdzdjzdjdmjdjzdjjcvjvcjvcvssltstttfbtftrfrlrdllrqqfssslccjdcjdjfdjfjqjnqjnjnrnddtnndtnnztzqztqztqzzpmzmggzrgrwwdqwdwcdwdnnmlmgmtmtstwssbffcnclclnclcjjcjpcpqcpqcpqpmqqfccpcjppnspsnnzggnpntndtdqtthwhnhwnwllzhlzhlzzghzghhlhvhwhjhfjjcnjnvjnjvvqccdmmgddllnmnrrdtdnncggfhgfglfgfmfnnpvvggznnwvnwwfgghrrfwrwzwszzzldzdldhlhblhblhbbbgjgsjggmqqmrrzggrhhwpwdpwdpwplpgpbggtssqffbqfbqbnnsqnqfnngcnnmwnmnbmmmslsjllbtbbpllltzzhgzztllsdllrvvhvjvbbhcbhchmchcctbcttvccgwcwpcchrcrdrdggcrrntrrfllcffbdfflrrrgbgrbbbdqbqjbbgbgrrqwqtwqwhwghwhzwwcswsnwnqqjhjhwhfwhffdfgddgjgsjgjhgglhlwhlhssfqfhhdmdnmnppdcddfzzhmhqqntqnnjvnjvjddcvcgcbgbbpjbjtbjjfgftgffplljfjrrhqqpddlssrvsrvrpppsllsdsqqqzzfttqsqzssjbbrnbnnrbbsrshsrrshrhwhbwbrrsrfrttfqtfqqfddvrvjrjvjsjhjsjdjqdjjlqjjjgcjcmcncfcrcwrwsrsslffzszmsszrsssrnrjjvbvpvcppptbbhhrddbcbggbqbmmsqqwggfpfbblmldmmpmwpwfwjfjsjnjmmpllccjzcjcwwpswshhpthhzchctcbcrrrrmvrvrdvvjmmvgmgwglghllvmllzlzzsvzzrmmhnnsjnnpvpwvpwwmvwwdqqdffhhhmccfgfvggchcctrrmdrrhrhnhnzzgpzzgttnhthvhzzqvvvwpwqpqdppsnnrgnnhphphmhcmcrmrvvqlvqqsccqhchzhwwmvmzmczzgsgdsggthgglrlnrlllbdllhwlwltwwcswsgssbhbsbvsbsbwbhbnncrcllttbrbppjccfpfhhgshschsccmrcmmcrrrzvvrcrggmwgwjwnwjjbffjddjnjgngqgdgnndznndvvfqfgfvvrvqrvvpllnsszbsbdbbdzbdbzbqbzznrznzjzpptcptccvwccfscffrftrrsnsvvswvvhbhzzfbzffncchhcnngzzcpcmmfttsntnjjsccqbcqqmzzgppdhppdtppmffgtgvvlzlpptdtttdppqjqtqctcrrzsswwtnwtnwnqqvbbdgjhvmmzpnhfvsbddzhgdwcnfdstvhhbzlzcfjwhlptbhmbmblprtsdmrdhbbbwpplnzgdnrzjmgzgpqbggnqvwwtntzgfwqrztqtdrsnhpfzswptggnvbszdcrmrhhtlrrfnpqrnpwrbmhlfwmdqqdbqrwbzqjbzwrgmbgrtzrhdclqfgsrtsgfwqrnnqgwsncmpgffggssrqvwjlhpsghbqdtzwmvzzvcmzsjqvprvcqwqjbcqcqrhpwwcsrscgmfdppbgvmnrdfrppblznbstnjzwwgstjvtprjbhtpdfgrhdjnjmnlbfwggzhcngvcwvcfpcwdtdppwjrdzsnjlnrzbfqqshlnzvwsmscgpfwjzhtwgfwgzdhbdwwzbsmfwwbmvrlrpswnjlmfbfzhwvcmgwfzssmmtjlwtrpwpwgnspbgchdncbfcpjsvtzjqtwqwjwgbhrbwvhqbcstsgsnwsjmhrlrvzgqhqfrmnrjdrhdjwcwctpdrzctlvnfzmzwhsnfprlzgzjpqvzchlmvbhffhpfjtvsdbvbdmwgvmqpflhwwndbqthmmwshdtspsrvqdflmmzwbqbqmpfdwjmvpbzdnqzfmhzdgldqjjvgpfcqftvjzwnzmfqdggrwlfzdhjnhmtrjbnllgqpntwmhnwtglnqdwbqdblpwnnrdwzpsqzfwqcmhqhnpsdcwvdldphgnrtqzdbnnzdzfttldrqcztlvlrgpdqzrcthslmtqhfvbzrfgnlrprcpbsctqhspbhnjtzrzhqjzszbzdthttqmbznzssftztwlggmdqqdtfllqjzjtvpgjfhtbwtbmtjplqnbdmsvlnqcwtdbdvfjnzgsmpnhbvvwwfbrgffjqfsccdjdwvbsdhqwfzvcpjzjbdjgrdctjplhwbdhhnbnwstvndnnwtsgbhzbvwdshvmnbwsthlrggtmddvjbfzfrnrdrqfjpslrccctzpjbwpdbhlbzfmwbltcqfngdprvfhgcszdtpnrcpdmllfnlspgrdrpwqmqbmrglvlrsmrfqrtzzgjcvqtqzpmghjrvmdmvvqztrjzbzjwdqsmrwpqnswbzhjbzzhdvmnfdsztzdzrjssgnnfqvbtsqrrmcppjgrmnstrnrlwjvvcczqlcbmwqzdfpssfwdfrvwtstwchdgwtrhhcmppcqlmrqlnwqccfphsdhbsbmtjvpcwwjrmrllbpnrmpbvwgwbftpdpphccwqcblcnvvbbppscmnjqgddllbnbvmmqzdffrrjtqwllzgpqrmnlfqrptzqmdmnrfnjpvqvjbsqrhljslgqqcqqtmtbwjrpphtjgjbqpzmzrzjrfjwcdcnbsjfljclffjplnrrcfbmhphtcjrzlrvvjcznpgpnrdwwqvgnbnzqnlcghhgwvhqbvjzfbdvhrzlqfbtlqhpltfjlfpbnjbphmmpntzqgjmwjtchwmlwvfjmfflqzpqnvvrgnbddzlfpdpdjghfgbsfddjspnfdwvqppncmdgfrnvrpcrflhgjgbwdsbwblfcwbtlrrnjjdhbvmrzgsvjwgfnnhqfbvhprlmwwqgclzlbqbrdspcbhftmdscsmpwrggrmnsvjphjmzmmrlrhnmdhwjlbmjchtvsrcplfspsssjznmzcrqnsjjtwjzvlhshbptqwwvjhjvzrhphphsbphpnzpfbwcdnqrhrrvlrwrztlpqnrcfzrncsvpzqzgslrlrwhvtgjmfncldqmvshlmnlpqbgvnwqfcthgrgllmqrjqmfgznspgltpptglpdcvhtzsprprbldbzhbmjsqzwvjggwhsczltcvgwqhspzpzvljwqjgrgtwswjdswlzjzslrsslvqzncjwhbbjpbdthqpgmhfglggmlrgwdsplgscrwstntvrhjzjjlshtgmnnhvsjwfmcjbpzjcstmnpvtbgrfcfdwjljsrfhdphrdcslwhgvlnwltwchplvfzntfgcnlsvzrvnnczhhqdlwjvqprhmtjdtwmppffmszzzqtfrgnhnzqgqzhrjzgntcszstrfhhtptgvswvzvjcgcntmhzzmdgsmtgzhpfvqfnwmsjdhtfgmmbrrfsdlptchqqzqdqjncmtpznfssrcnmcdnthglmfzsfgltrndqsfmdftmfgchbwmzgrtjvgqtshlltthnnpqnzfrchzhdzrrnpzvfzblrmhwdwjnqdptlbvndmmlhzhvsfdlmlhqrgqqzsdqtpczwcrwcbsftvvphfbwjrvrnrcqbbcsqgnhltwzvllljcvpwjgslbmngcdmpdvjlgcnrzwqjdgrblncpqmrgjmpqjzvdmcmwfnwqlszdgwqdfznhsnpsjrfwrqpqmpvhstmzgqblfmcfvwljbhdfhdmqcvrwnqcstwtzgmng
|
39
2022/cpp/06/lib.hpp
Normal file
39
2022/cpp/06/lib.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream& os, const std::vector<T>& list) {
|
||||
unsigned long i = 0;
|
||||
os << "[";
|
||||
for (auto el : list) {
|
||||
os << el;
|
||||
if (i < list.size() - 1)
|
||||
os << ", ";
|
||||
i++;
|
||||
}
|
||||
os << "]";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream& os, const std::deque<T>& list) {
|
||||
unsigned long i = 0;
|
||||
os << "[";
|
||||
for (auto el : list) {
|
||||
os << el;
|
||||
if (i < list.size() - 1)
|
||||
os << ", ";
|
||||
i++;
|
||||
}
|
||||
os << "]";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
inline void error(const char* msg) {
|
||||
std::cerr << msg << std::endl;
|
||||
exit(1);
|
||||
}
|
BIN
2022/cpp/06/result
Executable file
BIN
2022/cpp/06/result
Executable file
Binary file not shown.
23
2022/cpp/06/result.cpp
Normal file
23
2022/cpp/06/result.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "answer.hpp"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2)
|
||||
error("Please specify the input file as first argument.");
|
||||
|
||||
#if !VERBOSE
|
||||
std::cout.setstate(std::ios_base::failbit);
|
||||
#endif
|
||||
|
||||
Input input = get_input(argv[1]);
|
||||
std::string result = get_result(input);
|
||||
|
||||
#if !VERBOSE
|
||||
std::cout.clear();
|
||||
#endif
|
||||
|
||||
std::cout << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
1
2022/cpp/06/sample1
Normal file
1
2022/cpp/06/sample1
Normal file
@ -0,0 +1 @@
|
||||
mjqjpqmgbljsphdztnvjfqwrcgsmlb
|
1
2022/cpp/06/sample2
Normal file
1
2022/cpp/06/sample2
Normal file
@ -0,0 +1 @@
|
||||
bvwbjplbgvbhsrlpgdmjqwftvncz
|
1
2022/cpp/06/sample3
Normal file
1
2022/cpp/06/sample3
Normal file
@ -0,0 +1 @@
|
||||
nppdvjthqldpwncqszvftbrmjlhg
|
1
2022/cpp/06/sample4
Normal file
1
2022/cpp/06/sample4
Normal file
@ -0,0 +1 @@
|
||||
nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg
|
1
2022/cpp/06/sample5
Normal file
1
2022/cpp/06/sample5
Normal file
@ -0,0 +1 @@
|
||||
zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw
|
BIN
2022/cpp/06/test
Executable file
BIN
2022/cpp/06/test
Executable file
Binary file not shown.
67
2022/cpp/06/test.cpp
Normal file
67
2022/cpp/06/test.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include <ostream>
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "../doctest/doctest/doctest.h"
|
||||
#include "answer.hpp"
|
||||
|
||||
#if PART == 1
|
||||
|
||||
TEST_CASE("Part 1") {
|
||||
std::vector<Input> expected_inputs;
|
||||
expected_inputs.insert(expected_inputs.end(),
|
||||
{
|
||||
{"mjqjpqmgbljsphdztnvjfqwrcgsmlb"},
|
||||
{"bvwbjplbgvbhsrlpgdmjqwftvncz"},
|
||||
{"nppdvjthqldpwncqszvftbrmjlhg"},
|
||||
{"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"},
|
||||
{"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"},
|
||||
});
|
||||
|
||||
std::vector<std::string> expected_outputs;
|
||||
expected_outputs.insert(expected_outputs.end(),
|
||||
{
|
||||
"7",
|
||||
"5",
|
||||
"6",
|
||||
"10",
|
||||
"11",
|
||||
});
|
||||
|
||||
for (int i : {0, 1, 2, 3, 4}) {
|
||||
Input expected_input = expected_inputs[i];
|
||||
|
||||
std::string sample_name = "sample" + std::to_string(i + 1);
|
||||
Input actual_input = get_input(sample_name.c_str());
|
||||
|
||||
SUBCASE("Testing input is parsed correctly") {
|
||||
CHECK(actual_input == expected_input);
|
||||
}
|
||||
|
||||
SUBCASE("Testing output is the one expected from AOC") {
|
||||
CHECK(get_result(actual_input) == expected_outputs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PART == 2
|
||||
TEST_CASE("Part 2") {
|
||||
std::vector<std::string> expected_outputs;
|
||||
expected_outputs.insert(expected_outputs.end(),
|
||||
{
|
||||
"19",
|
||||
"23",
|
||||
"23",
|
||||
"29",
|
||||
"26",
|
||||
});
|
||||
|
||||
for (int i : {0, 1, 2, 3, 4}) {
|
||||
std::string sample_name = "sample" + std::to_string(i + 1);
|
||||
Input actual_input = get_input(sample_name.c_str());
|
||||
|
||||
SUBCASE("Testing output is the one expected from AOC") {
|
||||
CHECK(get_result(actual_input) == expected_outputs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user