Skip to content

Commit 7be13d6

Browse files
committed
cpp - fix duplication
1 parent b3aef8a commit 7be13d6

File tree

12 files changed

+72
-34
lines changed

12 files changed

+72
-34
lines changed

src/main/cpp/2017/04/AoC2017_04.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ const vector<string> TEST6 = { "a ab abc abd abf abj" };
1313
const vector<string> TEST7 = { "iiii oiii ooii oooi oooo" };
1414
const vector<string> TEST8 = { "oiii ioii iioi iiio" };
1515

16-
vector<string> split(const string& s) {
17-
istringstream iss(s);
18-
return {istream_iterator<string>{iss}, istream_iterator<string>{}};
19-
}
20-
2116
bool hasNoDuplicateWords(const string &s) {
2217
map<string, uint> m;
23-
for (const string &token : split(s)) {
18+
for (const string &token : aoc::split(s)) {
2419
m[token]++;
2520
}
2621
return count_if(m.begin(), m.end(),
@@ -31,7 +26,7 @@ bool hasNoDuplicateWords(const string &s) {
3126
bool hasNoAnagrams(const string &s) {
3227
set<map<char, uint>> letterCounts;
3328
uint wordCount = 0;
34-
for (const string &token : split(s)) {
29+
for (const string &token : aoc::split(s)) {
3530
wordCount++;
3631
map<char, uint> m;
3732
for (auto i = token.begin(); i != token.end(); ++i) {

src/main/cpp/2017/04/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cc_binary(
2+
name = "AoC2017_04",
3+
srcs = ["AoC2017_04.cpp"],
4+
deps = [
5+
"//src/main/cpp/aoc:aoc",
6+
"//src/main/cpp/aocd:aocd",
7+
],
8+
)
9+

src/main/cpp/2017/04/Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
CC := g++
2-
CFLAGS := -Wall -std=c++17
1+
CC := clang++
2+
CFLAGS := -Wall -std=c++17 -Ofast
33
OBJ_DIR := ../../../../../build/cpp/obj
4+
AOC_OBJ_DIR := $(OBJ_DIR)/aoc
45
AOCD_OBJ_DIR := $(OBJ_DIR)/aocd
56
TARGET_DIR := ../../../../../build/cpp
67

8+
AOC_SRCS = ../../aoc/aoc.cpp
79
AOCD_SRCS = ../../aocd/aocd.cpp
810
SRCS = $(MAIN).cpp
11+
AOC_OBJS = $(addprefix $(AOC_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(AOC_SRCS))))
912
AOCD_OBJS = $(addprefix $(AOCD_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(AOCD_SRCS))))
1013
OBJS = $(addprefix $(OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(SRCS))))
1114
TARGET = $(TARGET_DIR)/$(MAIN)
1215

1316
all: $(TARGET)
1417

15-
$(TARGET): $(AOCD_OBJS) $(OBJS)
18+
$(TARGET): $(AOC_OBJS) $(AOCD_OBJS) $(OBJS)
1619
$(CC) $(CFLAGS) -o $(TARGET) $(AOC_OBJS) $(AOCD_OBJS) $(OBJS)
1720

21+
$(AOC_OBJS): $(AOC_SRCS)
22+
@mkdir -p $(AOC_OBJ_DIR)
23+
$(CC) $(CFLAGS) -c $< -o $@
24+
1825
$(AOCD_OBJS): $(AOCD_SRCS)
1926
@mkdir -p $(AOCD_OBJ_DIR)
2027
$(CC) $(CFLAGS) -c $< -o $@
@@ -24,6 +31,6 @@ $(OBJS): $(SRCS)
2431
$(CC) $(CFLAGS) -c $< -o $@
2532

2633
clean:
27-
$(RM) $(AOCD_OBJS) $(OBJS) $(TARGET)
34+
$(RM) $(AOC_OBJS) $(AOCD_OBJS) $(OBJS) $(TARGET)
2835

2936
.PHONY: all clean

src/main/cpp/2017/18/AoC2017_18.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class VirtualMachine {
4040
void step(Program &program);
4141

4242
protected:
43-
vector<string> split(const string& s);
4443
long getValue(Program &program, const string& s);
4544
virtual void snd(Program &program, const string &operand) = 0;
4645
virtual void rcv(Program &program, const string &operand) = 0;
@@ -74,11 +73,6 @@ void Program::put(const string &name, long value) {
7473
registers[name] = value;
7574
}
7675

77-
vector<string> VirtualMachine::split(const string& s) {
78-
istringstream iss(s);
79-
return {istream_iterator<string>{iss}, istream_iterator<string>{}};
80-
}
81-
8276
long VirtualMachine::getValue(Program &program, const string& s) {
8377
try {
8478
return stoi(s);
@@ -89,7 +83,7 @@ long VirtualMachine::getValue(Program &program, const string& s) {
8983

9084
void VirtualMachine::step(Program &program) {
9185
const string &line = program.instructions[program.ip];
92-
const vector<string> &splits = split(line);
86+
const vector<string> &splits = aoc::split(line);
9387
const string &operation = splits[0];
9488
const string &op1 = splits[1];
9589
if (operation == "set") {

src/main/cpp/2017/18/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cc_binary(
2+
name = "AoC2017_18",
3+
srcs = ["AoC2017_18.cpp"],
4+
deps = [
5+
"//src/main/cpp/aoc:aoc",
6+
"//src/main/cpp/aocd:aocd",
7+
],
8+
)
9+

src/main/cpp/2017/18/Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
CC := g++
2-
CFLAGS := -Wall -std=c++17
1+
CC := clang++
2+
CFLAGS := -Wall -std=c++17 -Ofast
33
OBJ_DIR := ../../../../../build/cpp/obj
4+
AOC_OBJ_DIR := $(OBJ_DIR)/aoc
45
AOCD_OBJ_DIR := $(OBJ_DIR)/aocd
56
TARGET_DIR := ../../../../../build/cpp
67

8+
AOC_SRCS = ../../aoc/aoc.cpp
79
AOCD_SRCS = ../../aocd/aocd.cpp
810
SRCS = $(MAIN).cpp
11+
AOC_OBJS = $(addprefix $(AOC_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(AOC_SRCS))))
912
AOCD_OBJS = $(addprefix $(AOCD_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(AOCD_SRCS))))
1013
OBJS = $(addprefix $(OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(SRCS))))
1114
TARGET = $(TARGET_DIR)/$(MAIN)
1215

1316
all: $(TARGET)
1417

15-
$(TARGET): $(AOCD_OBJS) $(OBJS)
18+
$(TARGET): $(AOC_OBJS) $(AOCD_OBJS) $(OBJS)
1619
$(CC) $(CFLAGS) -o $(TARGET) $(AOC_OBJS) $(AOCD_OBJS) $(OBJS)
1720

21+
$(AOC_OBJS): $(AOC_SRCS)
22+
@mkdir -p $(AOC_OBJ_DIR)
23+
$(CC) $(CFLAGS) -c $< -o $@
24+
1825
$(AOCD_OBJS): $(AOCD_SRCS)
1926
@mkdir -p $(AOCD_OBJ_DIR)
2027
$(CC) $(CFLAGS) -c $< -o $@
@@ -24,6 +31,6 @@ $(OBJS): $(SRCS)
2431
$(CC) $(CFLAGS) -c $< -o $@
2532

2633
clean:
27-
$(RM) $(AOCD_OBJS) $(OBJS) $(TARGET)
34+
$(RM) $(AOC_OBJS) $(AOCD_OBJS) $(OBJS) $(TARGET)
2835

2936
.PHONY: all clean

src/main/cpp/2017/25/AoC2017_25.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@ ostream& operator <<(ostream& strm, const Step& step) {
2525
return strm;
2626
}
2727

28-
vector<string> split(const string& s) {
29-
istringstream iss(s);
30-
return {istream_iterator<string>{iss}, istream_iterator<string>{}};
31-
}
32-
3328
string getLastWord(const string& line) {
34-
const vector<string>& splits = split(line);
29+
const vector<string>& splits = aoc::split(line);
3530
const string& last = splits[splits.size() - 1];
3631
return last.substr(0, last.size() - 1);
3732
}
@@ -46,7 +41,7 @@ Step parseStep(const vector<string>& lines, const uint idx) {
4641
tuple<string, uint, unordered_map<string, State>> parse(const vector<string>& input) {
4742
const vector<vector<string>> blocks = aoc::toBlocks(input);
4843
const string start = getLastWord(blocks[0][0]);
49-
const uint steps = stoi(split(blocks[0][1])[5]);
44+
const uint steps = stoi(aoc::split(blocks[0][1])[5]);
5045
unordered_map<string, State> states;
5146
for (uint i = 1; i < blocks.size(); i++) {
5247
const vector<string>& block = blocks[i];

src/main/cpp/2017/25/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cc_binary(
2+
name = "AoC2017_25",
3+
srcs = ["AoC2017_25.cpp"],
4+
deps = [
5+
"//src/main/cpp/aoc:aoc",
6+
"//src/main/cpp/aocd:aocd",
7+
],
8+
)
9+

src/main/cpp/2017/25/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CC := g++
1+
CC := clang++
22
CFLAGS := -Wall -std=c++17 -Ofast
33
OBJ_DIR := ../../../../../build/cpp/obj
44
AOC_OBJ_DIR := $(OBJ_DIR)/aoc
@@ -31,6 +31,6 @@ $(OBJS): $(SRCS)
3131
$(CC) $(CFLAGS) -c $< -o $@
3232

3333
clean:
34-
$(RM) $(AOCD_OBJS) $(OBJS) $(TARGET)
34+
$(RM) $(AOC_OBJS) $(AOCD_OBJS) $(OBJS) $(TARGET)
3535

3636
.PHONY: all clean

src/main/cpp/aoc/aoc.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ vector<vector<string>> aoc::toBlocks(vector<string> lines) {
1010
vector<string> current_split;
1111
for (string& str : lines) {
1212
if (!str.empty()) {
13-
current_split.emplace_back(move(str));
13+
current_split.emplace_back(str);
1414
continue;
1515
}
1616

1717
if (current_split.empty()) {
1818
continue;
1919
}
2020

21-
splits.emplace_back(move(current_split));
21+
splits.emplace_back(current_split);
2222
current_split.clear();
2323
}
2424
if (!current_split.empty()) {
25-
splits.emplace_back(move(current_split));
25+
splits.emplace_back(current_split);
2626
}
2727
return splits;
2828
}
@@ -40,3 +40,7 @@ vector<int> aoc::getNumbers(const string s) {
4040
return numbers;
4141
}
4242

43+
vector<string> aoc::split(const string& s) {
44+
istringstream iss(s);
45+
return {istream_iterator<string>{iss}, istream_iterator<string>{}};
46+
}

src/main/cpp/aoc/aoc.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using namespace std;
1010
namespace aoc {
1111
vector<vector<string>> toBlocks(vector<string> lines);
1212
vector<int> getNumbers(const string s);
13+
vector<string> split(const string& s);
1314
}
1415

1516
#define DEBUG(Message) \

src/test/cpp/aoc/test_aoc.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ TEST(test_aoc, DEBUGDoesNothingWhenNDEBUG) {
4848
const string& captured = ::testing::internal::GetCapturedStderr().c_str();
4949
EXPECT_THAT(captured, IsEmpty());
5050
}
51+
52+
TEST(test_aoc, splitOnSpace) {
53+
EXPECT_THAT(aoc::split(""), Eq(vector<string>({})));
54+
EXPECT_THAT(aoc::split("a b"), Eq(vector<string>({"a", "b"})));
55+
EXPECT_THAT(aoc::split(" a b "), Eq(vector<string>({"a", "b"})));
56+
EXPECT_THAT(aoc::split("a b"), Eq(vector<string>({"a", "b"})));
57+
EXPECT_THAT(aoc::split("a1 b 2"), Eq(vector<string>({"a1", "b", "2"})));
58+
}

0 commit comments

Comments
 (0)