-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.cpp
62 lines (47 loc) · 1.69 KB
/
Day2.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
#include <iostream>
#include <string>
#include <vector>
#include "aoclib.h"
#include "Game.h"
// https://adventofcode.com/2023/day/2
std::vector<Game> parseGames(const std::vector<std::string>& input)
{
static const std::string gameIdDelimiter = ": ";
static const std::string handfulDelimiter = "; ";
static const std::string cubeDelimiter = ", ";
std::vector<Game> games;
for (std::string line : input) // Intentional element copy
{
aoclib::eat(line, ' '); // Drop "Game "
unsigned int gameId = std::stoi(aoclib::eat(line, gameIdDelimiter));
Game& game = games.emplace_back(gameId);
for (const std::string& handfulString : aoclib::split(line, handfulDelimiter))
{
Handful handful;
for (std::string& cubeString : aoclib::split(handfulString, cubeDelimiter))
{
unsigned int amount = std::stoi(aoclib::eat(cubeString, ' '));
Cube::Color cube = Cube::getByName(cubeString);
handful.setCubes(cube, amount);
}
game.addHandful(handful);
}
}
return games;
}
int main()
{
const std::vector<std::string> input = aoclib::readInput();
const std::vector<Game> games = parseGames(input);
unsigned int possibleGameIdSum = 0, totalPower = 0;
for (const Game& game : games)
{
if (game.isPossible())
{
possibleGameIdSum += game.getId();
}
totalPower += game.getPowerOfMinimumCubes();
}
std::cout << "The sum of ids of all possible games is " << possibleGameIdSum << "." << std::endl;
std::cout << "The sum of all game power is " << totalPower << "." << std::endl;
}