summaryrefslogtreecommitdiff
path: root/2023/02/src
diff options
context:
space:
mode:
Diffstat (limited to '2023/02/src')
-rw-r--r--2023/02/src/game.cpp98
-rw-r--r--2023/02/src/game.h45
-rw-r--r--2023/02/src/part1.cpp33
-rw-r--r--2023/02/src/part2.cpp31
4 files changed, 207 insertions, 0 deletions
diff --git a/2023/02/src/game.cpp b/2023/02/src/game.cpp
new file mode 100644
index 0000000..a99b8a2
--- /dev/null
+++ b/2023/02/src/game.cpp
@@ -0,0 +1,98 @@
1#include "game.h"
2
3#include <iostream>
4#include <sstream>
5
6Game::Game(int id) : id_{ id }
7{
8}
9
10void Game::push_back(const std::vector<int>& draw)
11{
12 draws.push_back(draw);
13}
14
15bool Game::is_valid(int blue, int red, int green) const
16{
17 for (const auto& draw : draws)
18 {
19 if (draw[0] > blue or draw[1] > red or draw[2] > green)
20 {
21 return false;
22 }
23 }
24 return true;
25}
26
27int Game::power() const
28{
29 int blue{}, red{}, green{};
30 for (const auto& draw : draws)
31 {
32 blue = std::max(draw[0], blue);
33 red = std::max(draw[1], red);
34 green = std::max(draw[2], green);
35 }
36
37 return blue * red * green;
38}
39
40std::string Game::to_string() const
41{
42 std::ostringstream out;
43 out << "Game " << id_ << ": ";
44 for (const auto& draw : draws)
45 {
46 out << draw[0] << " " << draw[1] << " " << draw[2] << ";";
47 }
48
49 return out.str();
50}
51
52Game Game::from_string(const std::string& line)
53{
54 std::istringstream in(line);
55
56 std::string id;
57 in >> skip<game_t> >> id;
58 id.pop_back();
59 Game game{ std::stoi(id) };
60
61 int n;
62 std::string kind;
63 std::vector<int> draw{ 0, 0, 0};
64
65 while (in >> n >> kind)
66 {
67 char sep{ kind.back() };
68 if (sep == ';' or sep == ',')
69 {
70 kind.pop_back();
71 }
72 else
73 {
74 sep = ';';
75 }
76
77 if (kind == "blue")
78 {
79 draw[0] += n;
80 }
81 else if (kind == "red")
82 {
83 draw[1] += n;
84 }
85 else if (kind == "green")
86 {
87 draw[2] += n;
88 }
89
90 if (sep == ';')
91 {
92 game.push_back(draw);
93 draw = { 0, 0, 0 };
94 }
95 }
96
97 return game;
98}
diff --git a/2023/02/src/game.h b/2023/02/src/game.h
new file mode 100644
index 0000000..1c8002a
--- /dev/null
+++ b/2023/02/src/game.h
@@ -0,0 +1,45 @@
1#ifndef GAME_H
2#define GAME_H
3
4#include <stdexcept>
5#include <string>
6#include <tuple>
7#include <vector>
8
9template<const char* keyword, typename CharT, typename Traits>
10std::basic_istream<CharT, Traits>& skip(std::basic_istream<CharT, Traits>& stream)
11{
12 std::string word;
13 stream >> word;
14 if (word != keyword)
15 {
16 throw std::invalid_argument("Malformed input");
17 }
18
19 return stream;
20}
21
22class Game
23{
24 int id_;
25 std::vector<std::vector<int>> draws{};
26
27 static constexpr char game_t[] = "Game";
28
29 public:
30 Game(int id);
31
32 inline int id() const
33 {
34 return id_;
35 }
36
37 bool is_valid(int blue, int red, int green) const;
38 int power() const;
39 void push_back(const std::vector<int>& draw);
40 std::string to_string() const;
41
42 static Game from_string(const std::string& line);
43};
44
45#endif //GAME_H
diff --git a/2023/02/src/part1.cpp b/2023/02/src/part1.cpp
new file mode 100644
index 0000000..c9fe48c
--- /dev/null
+++ b/2023/02/src/part1.cpp
@@ -0,0 +1,33 @@
1#include <iostream>
2#include <fstream>
3
4#include "game.h"
5
6int main(void)
7{
8 int answer{};
9 std::vector<Game> games{};
10
11 std::ifstream input{ "resources/input.txt" };
12 if (input.is_open())
13 {
14 std::string line;
15
16 while (not std::getline(input,line).eof())
17 {
18 games.push_back(Game::from_string(line));
19 }
20 }
21 input.close();
22
23 for (const auto& game : games)
24 {
25 if (game.is_valid(14, 12, 13))
26 {
27 answer += game.id();
28 }
29 }
30
31 std::cout << answer << std::endl;
32 return 0;
33}
diff --git a/2023/02/src/part2.cpp b/2023/02/src/part2.cpp
new file mode 100644
index 0000000..52b8fc8
--- /dev/null
+++ b/2023/02/src/part2.cpp
@@ -0,0 +1,31 @@
1#include <algorithm>
2#include <iostream>
3#include <fstream>
4
5#include "game.h"
6
7int main(void)
8{
9 int answer{};
10 std::vector<Game> games{};
11
12 std::ifstream input{ "resources/input.txt" };
13 if (input.is_open())
14 {
15 std::string line;
16
17 while (not std::getline(input,line).eof())
18 {
19 games.push_back(Game::from_string(line));
20 }
21 }
22 input.close();
23
24 for (const auto& game : games)
25 {
26 answer += game.power();
27 }
28
29 std::cout << answer << std::endl;
30 return 0;
31}