diff options
-rw-r--r-- | 2023/07/Makefile | 18 | ||||
-rw-r--r-- | 2023/07/resources/input_small.txt | 5 | ||||
-rw-r--r-- | 2023/07/src/part1.cpp | 109 | ||||
-rw-r--r-- | 2023/07/src/part2.cpp | 113 |
4 files changed, 245 insertions, 0 deletions
diff --git a/2023/07/Makefile b/2023/07/Makefile new file mode 100644 index 0000000..a3c2cea --- /dev/null +++ b/2023/07/Makefile | |||
@@ -0,0 +1,18 @@ | |||
1 | CXXFLAGS := -std=c++17 | ||
2 | EXE := part1 part2 | ||
3 | |||
4 | .PHONY: all clean configure | ||
5 | |||
6 | all: $(EXE) | ||
7 | |||
8 | configure: | ||
9 | bear -- $(MAKE) all | ||
10 | |||
11 | %.o: %.cpp | ||
12 | $(CXX) -c $(CXXFLAGS) $< -o $@ | ||
13 | |||
14 | clean: | ||
15 | rm -rf $(EXE) src/*.o compile_commands.json | ||
16 | |||
17 | %: src/%.o | ||
18 | $(CXX) $^ -o $@ | ||
diff --git a/2023/07/resources/input_small.txt b/2023/07/resources/input_small.txt new file mode 100644 index 0000000..e3500c3 --- /dev/null +++ b/2023/07/resources/input_small.txt | |||
@@ -0,0 +1,5 @@ | |||
1 | 32T3K 765 | ||
2 | T55J5 684 | ||
3 | KK677 28 | ||
4 | KTJJT 220 | ||
5 | QQQJA 483 | ||
diff --git a/2023/07/src/part1.cpp b/2023/07/src/part1.cpp new file mode 100644 index 0000000..089be93 --- /dev/null +++ b/2023/07/src/part1.cpp | |||
@@ -0,0 +1,109 @@ | |||
1 | #include <algorithm> | ||
2 | #include <array> | ||
3 | #include <fstream> | ||
4 | #include <iostream> | ||
5 | #include <set> | ||
6 | #include <sstream> | ||
7 | #include <string> | ||
8 | |||
9 | class Hand | ||
10 | { | ||
11 | static constexpr int HAND{ 5 }; | ||
12 | |||
13 | int bet_; | ||
14 | int score_{}; | ||
15 | std::array<char,HAND> cards_; | ||
16 | |||
17 | public: | ||
18 | Hand(const std::string& cards, int bet) : bet_{ bet } | ||
19 | { | ||
20 | for (int c = 0; c < HAND; ++c) | ||
21 | { | ||
22 | switch (cards[c]) | ||
23 | { | ||
24 | case 'A': | ||
25 | cards_[c] = 12; | ||
26 | break; | ||
27 | case 'K': | ||
28 | cards_[c] = 11; | ||
29 | break; | ||
30 | case 'Q': | ||
31 | cards_[c] = 10; | ||
32 | break; | ||
33 | case 'J': | ||
34 | cards_[c] = 9; | ||
35 | break; | ||
36 | case 'T': | ||
37 | cards_[c] = 8; | ||
38 | break; | ||
39 | default: | ||
40 | cards_[c] = cards[c] - '2'; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | std::array<int,13> mult{}; | ||
45 | std::for_each(cards_.begin(), cards_.end(), [&mult](int c) { ++mult[c]; }); | ||
46 | std::for_each(mult.begin(), mult.end(), [this](int c) { | ||
47 | switch (c) | ||
48 | { | ||
49 | case 2: | ||
50 | this->score_ += 1; | ||
51 | break; | ||
52 | case 3: | ||
53 | this->score_ += 3; | ||
54 | break; | ||
55 | case 4: | ||
56 | this->score_ += 5; | ||
57 | break; | ||
58 | case 5: | ||
59 | this->score_ += 6; | ||
60 | break; | ||
61 | } | ||
62 | }); | ||
63 | } | ||
64 | |||
65 | inline int bet() const | ||
66 | { | ||
67 | return bet_; | ||
68 | } | ||
69 | |||
70 | bool operator<(const Hand& that) const | ||
71 | { | ||
72 | return this->score_ < that.score_ or | ||
73 | (this->score_ == that.score_ and this->cards_ < that.cards_); | ||
74 | } | ||
75 | |||
76 | static Hand from_string(const std::string& str) | ||
77 | { | ||
78 | int bet; | ||
79 | std::string hand; | ||
80 | std::istringstream{ str } >> hand >> bet; | ||
81 | return { hand, bet }; | ||
82 | } | ||
83 | }; | ||
84 | |||
85 | int main(void) | ||
86 | { | ||
87 | int answer{}; | ||
88 | std::multiset<Hand> hands; | ||
89 | |||
90 | std::ifstream input{ "resources/input.txt" }; | ||
91 | if (input.is_open()) | ||
92 | { | ||
93 | std::string line; | ||
94 | while (not std::getline(input,line).eof()) | ||
95 | { | ||
96 | hands.insert(Hand::from_string(line)); | ||
97 | } | ||
98 | } | ||
99 | input.close(); | ||
100 | |||
101 | int rank{}; | ||
102 | for (const auto& h : hands) | ||
103 | { | ||
104 | answer += ++rank * h.bet(); | ||
105 | } | ||
106 | |||
107 | std::cout << answer << std::endl; | ||
108 | return 0; | ||
109 | } | ||
diff --git a/2023/07/src/part2.cpp b/2023/07/src/part2.cpp new file mode 100644 index 0000000..239df11 --- /dev/null +++ b/2023/07/src/part2.cpp | |||
@@ -0,0 +1,113 @@ | |||
1 | #include <algorithm> | ||
2 | #include <array> | ||
3 | #include <fstream> | ||
4 | #include <iostream> | ||
5 | #include <set> | ||
6 | #include <sstream> | ||
7 | #include <string> | ||
8 | |||
9 | class Hand | ||
10 | { | ||
11 | static constexpr int HAND{ 5 }; | ||
12 | |||
13 | int bet_; | ||
14 | int score_{}; | ||
15 | std::array<char,HAND> cards_; | ||
16 | |||
17 | public: | ||
18 | Hand(const std::string& cards, int bet) : bet_{ bet } | ||
19 | { | ||
20 | for (int c = 0; c < HAND; ++c) | ||
21 | { | ||
22 | switch (cards[c]) | ||
23 | { | ||
24 | case 'A': | ||
25 | cards_[c] = 12; | ||
26 | break; | ||
27 | case 'K': | ||
28 | cards_[c] = 11; | ||
29 | break; | ||
30 | case 'Q': | ||
31 | cards_[c] = 10; | ||
32 | break; | ||
33 | case 'J': | ||
34 | cards_[c] = 0; | ||
35 | break; | ||
36 | case 'T': | ||
37 | cards_[c] = 9; | ||
38 | break; | ||
39 | default: | ||
40 | cards_[c] = cards[c] - '1'; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | std::array<int,13> mult{}; | ||
45 | std::for_each(cards_.begin(), cards_.end(), [&mult](int c) { ++mult[c]; }); | ||
46 | std::for_each(mult.begin() + 1, mult.end(), [this](int c) { | ||
47 | switch (c) | ||
48 | { | ||
49 | case 2: | ||
50 | this->score_ += 1; | ||
51 | break; | ||
52 | case 3: | ||
53 | this->score_ += 3; | ||
54 | break; | ||
55 | case 4: | ||
56 | this->score_ += 5; | ||
57 | break; | ||
58 | case 5: | ||
59 | this->score_ += 6; | ||
60 | break; | ||
61 | } | ||
62 | }); | ||
63 | for (int j = 1; j <= mult[0] and j < HAND; ++j) | ||
64 | { | ||
65 | score_ += (1 <= score_ and score_ < 4) ? 2 : 1; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | inline int bet() const | ||
70 | { | ||
71 | return bet_; | ||
72 | } | ||
73 | |||
74 | bool operator<(const Hand& that) const | ||
75 | { | ||
76 | return this->score_ < that.score_ or | ||
77 | (this->score_ == that.score_ and this->cards_ < that.cards_); | ||
78 | } | ||
79 | |||
80 | static Hand from_string(const std::string& str) | ||
81 | { | ||
82 | int bet; | ||
83 | std::string hand; | ||
84 | std::istringstream{ str } >> hand >> bet; | ||
85 | return { hand, bet }; | ||
86 | } | ||
87 | }; | ||
88 | |||
89 | int main(void) | ||
90 | { | ||
91 | int answer{}; | ||
92 | std::multiset<Hand> hands; | ||
93 | |||
94 | std::ifstream input{ "resources/input.txt" }; | ||
95 | if (input.is_open()) | ||
96 | { | ||
97 | std::string line; | ||
98 | while (not std::getline(input,line).eof()) | ||
99 | { | ||
100 | hands.insert(Hand::from_string(line)); | ||
101 | } | ||
102 | } | ||
103 | input.close(); | ||
104 | |||
105 | int rank{}; | ||
106 | for (const auto& h : hands) | ||
107 | { | ||
108 | answer += ++rank * h.bet(); | ||
109 | } | ||
110 | |||
111 | std::cout << answer << std::endl; | ||
112 | return 0; | ||
113 | } | ||