Add 2022 Day 1 solutions
This commit is contained in:
29
2022/puzzle-01-01.cc
Normal file
29
2022/puzzle-01-01.cc
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by Matthew Gretton-Dann on 01/12/2022.
|
||||
//
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using Int = std::int64_t;
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
Int current_value{0};
|
||||
Int max_value{0};
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
if (line.empty()) {
|
||||
max_value = std::max(max_value, current_value);
|
||||
current_value = 0;
|
||||
}
|
||||
else {
|
||||
Int incr{std::stoll(line)};
|
||||
current_value += incr;
|
||||
}
|
||||
}
|
||||
max_value = std::max(max_value, current_value);
|
||||
std::cout << "Max value: " << max_value << '\n';
|
||||
return EXIT_SUCCESS;
|
||||
}
|
37
2022/puzzle-01-02.cc
Normal file
37
2022/puzzle-01-02.cc
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by Matthew Gretton-Dann on 01/12/2022.
|
||||
//
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
using Int = std::int64_t;
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
Int current_value{0};
|
||||
std::multiset<Int> values;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
if (line.empty()) {
|
||||
values.insert(current_value);
|
||||
current_value = 0;
|
||||
}
|
||||
else {
|
||||
Int incr{std::stoll(line)};
|
||||
current_value += incr;
|
||||
}
|
||||
}
|
||||
values.insert(current_value);
|
||||
|
||||
Int top_three{0};
|
||||
auto it = values.crbegin();
|
||||
top_three += *it++;
|
||||
top_three += *it++;
|
||||
top_three += *it++;
|
||||
std::cout << "Sum of values: " << top_three << '\n';
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user