// // Created by Grant Horner on 12/1/25. // #include #include #include #include "doctest.h" #include "utils.h" namespace day1 { enum Direction { L = 'L', R = 'R', }; struct Instruction { Direction direction; uint32_t amount; }; Instruction parse_instruction(std::string_view line) { assert(!line.empty()); const auto direction = static_cast(line[0]); uint32_t amount; std::from_chars(&line[1], line.data() + line.size(), amount); return Instruction{direction, amount}; } } template<> struct std::formatter : std::formatter { static auto format(const day1::Instruction &i, std::format_context &ctx) { return std::format_to(ctx.out(), "Instruction[direction: {}, amount: {}]", static_cast(i.direction), i.amount); } }; namespace day1 { const std::string sample = "L68\n" "L30\n" "R48\n" "L5\n" "R60\n" "L55\n" "L1\n" "L99\n" "R14\n" "L82\n"; uint64_t spin2(int &value, Instruction i) { uint64_t spins = i.amount / 100; int amount_to_spin = i.amount % 100; if (i.direction == L) { amount_to_spin *= -1; } value += amount_to_spin; if (value == 0) { spins++; } else if (value < 0) { spins++; value = 100 + value; } else if (100 < value) { spins++; value = value - 100; } return spins; } bool spin(int &value, Instruction i) { value += static_cast(i.amount) * (i.direction == L ? -1 : 1); value %= 100; return value == 0; } uint32_t solve_first(const std::string &input) { int value = 50; uint32_t spins = 0; for (const auto instr: std::views::split(std::string_view{input}, '\n') | std::views::transform(utils::to_string_view) | std::views::filter(utils::non_empty) | std::views::transform(parse_instruction) ) { if (spin(value, instr)) { spins++; } // std::println("Total spins: {}, instruction: {}, value: {}", spins, instr, value); } return spins; } TEST_SUITE_BEGIN("Day 1"); TEST_CASE("puzzle 1") { CHECK(solve_first(sample) == 3); CHECK(solve_first(utils::read_file("day1input.txt")) == 1158); } uint32_t solve_second(const std::string &input) { int value = 50; uint32_t spins = 0; for (const auto instr: std::views::split(std::string_view{input}, '\n') | std::views::transform(utils::to_string_view) | std::views::filter(utils::non_empty) | std::views::transform(parse_instruction) ) { spins += spin2(value, instr); // std::println("Total spins: {}, instruction: {}, value: {}", spins, instr, value); } return spins; } TEST_CASE("puzzle 2") { CHECK(solve_second(sample) == 6); CHECK(solve_second(utils::read_file("day1input.txt")) == 6860); } TEST_SUITE_END(); }