2023 Day 1 now tidied
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
using UInt = std::uint64_t;
|
using UInt = std::uint64_t;
|
||||||
|
|
||||||
@@ -44,8 +47,8 @@ auto find_last_digit(std::string const& line) -> int
|
|||||||
|
|
||||||
auto main() -> int
|
auto main() -> int
|
||||||
{
|
{
|
||||||
UInt sum1{0};
|
int sum1{0};
|
||||||
UInt sum2{0};
|
int sum2{0};
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
while (std::getline(std::cin, line)) {
|
while (std::getline(std::cin, line)) {
|
||||||
@@ -54,7 +57,7 @@ auto main() -> int
|
|||||||
assert(first != std::string::npos);
|
assert(first != std::string::npos);
|
||||||
assert(last != std::string::npos);
|
assert(last != std::string::npos);
|
||||||
sum1 += (line[first] - '0') * 10;
|
sum1 += (line[first] - '0') * 10;
|
||||||
sum1 += (line[last] - '0');
|
sum1 += line[last] - '0';
|
||||||
sum2 += find_first_digit(line) * 10;
|
sum2 += find_first_digit(line) * 10;
|
||||||
sum2 += find_last_digit(line);
|
sum2 += find_last_digit(line);
|
||||||
}
|
}
|
||||||
|
68
2023/puzzle-01-02.cc
Normal file
68
2023/puzzle-01-02.cc
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include <array>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
using UInt = std::uint64_t;
|
||||||
|
|
||||||
|
constexpr std::array nums = {
|
||||||
|
std::make_pair("one", 1), std::make_pair("two", 2), std::make_pair("three", 3),
|
||||||
|
std::make_pair("four", 4),
|
||||||
|
std::make_pair("five", 5), std::make_pair("six", 6),
|
||||||
|
std::make_pair("seven", 7),
|
||||||
|
std::make_pair("eight", 8), std::make_pair("nine", 9)};
|
||||||
|
|
||||||
|
auto find_first_digit(std::string const& line) -> int
|
||||||
|
{
|
||||||
|
auto pos = line.find_first_of("0123456789");
|
||||||
|
auto value = line[pos] - '0';
|
||||||
|
for (auto [s, v] : nums) {
|
||||||
|
if (auto p = line.find(s); p != std::string::npos) {
|
||||||
|
if (p < pos) {
|
||||||
|
pos = p;
|
||||||
|
value = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto find_last_digit(std::string const& line) -> int
|
||||||
|
{
|
||||||
|
auto pos = line.find_last_of("0123456789");
|
||||||
|
auto value = line[pos] - '0';
|
||||||
|
for (auto [s, v] : nums) {
|
||||||
|
if (auto p = line.rfind(s); p != std::string::npos) {
|
||||||
|
if (p > pos) {
|
||||||
|
pos = p;
|
||||||
|
value = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto main() -> int
|
||||||
|
{
|
||||||
|
int sum1{0};
|
||||||
|
int sum2{0};
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(std::cin, line)) {
|
||||||
|
auto first = line.find_first_of("0123456789");
|
||||||
|
auto last = line.find_last_of("0123456789");
|
||||||
|
assert(first != std::string::npos);
|
||||||
|
assert(last != std::string::npos);
|
||||||
|
sum1 += (line[first] - '0') * 10;
|
||||||
|
sum1 += line[last] - '0';
|
||||||
|
sum2 += find_first_digit(line) * 10;
|
||||||
|
sum2 += find_last_digit(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Sum 1: " << sum1 << "\n";
|
||||||
|
std::cout << "Sum 2: " << sum2 << "\n";
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
@@ -4,6 +4,7 @@ project(advent-of-code)
|
|||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
file(GLOB years LIST_DIRECTORIES true RELATIVE "${CMAKE_SOURCE_DIR}" CONFIGURE_DEPENDS
|
file(GLOB years LIST_DIRECTORIES true RELATIVE "${CMAKE_SOURCE_DIR}" CONFIGURE_DEPENDS
|
||||||
"20[1-9][0-9]")
|
"20[1-9][0-9]")
|
||||||
|
Reference in New Issue
Block a user