From b6ac5462ff85dfea2824b836ca4fa62b747e2602 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 22 Dec 2022 06:08:38 +0000 Subject: [PATCH] Solutions for 2018 day 11 --- 2018/puzzle-11-01.cc | 66 ++++++++++++++++++++++++++++++++++++++++++ 2018/puzzle-11-02.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 2018/puzzle-11-01.cc create mode 100644 2018/puzzle-11-02.cc diff --git a/2018/puzzle-11-01.cc b/2018/puzzle-11-01.cc new file mode 100644 index 0000000..b910425 --- /dev/null +++ b/2018/puzzle-11-01.cc @@ -0,0 +1,66 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include +#include + +constexpr std::size_t grid_size{300}; +using Int = std::int64_t; +using Grid = std::array, grid_size>; +using SmallGrid = std::array, grid_size - 2>; + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line.\n"; + return EXIT_FAILURE; + } + + Int const serial{std::stoll(line)}; + Grid grid; + for (Int y{0}; y < grid_size; ++y) { + for (Int x{0}; x < grid_size; ++x) { + Int const rack_id{x + 1 + 10}; + Int power{(rack_id * (y + 1) + serial) * rack_id}; + power /= 100; + power %= 10; + power -= 5; + grid.at(y).at(x) = power; + } + } + + SmallGrid small_grid; + for (Int y{0}; y < small_grid.size(); ++y) { + for (Int x{0}; x < small_grid.size(); ++x) { + small_grid[y][x] = 0; + for (Int dy{0}; dy < 3; ++dy) { + for (Int dx{0}; dx < 3; ++dx) { + small_grid[y][x] += grid[y + dy][x + dx]; + } + } + } + } + + Int max_power{std::numeric_limits::min()}; + Int max_x{0}; + Int max_y{0}; + for (Int y{0}; y < small_grid.size(); ++y) { + for (Int x{0}; x < small_grid.size(); ++x) { + if (max_power < small_grid[y][x]) { + max_power = small_grid[y][x]; + max_x = x + 1; + max_y = y + 1; + } + } + } + std::cout << "Maximum power: " << max_power << "\n"; + std::cout << "Coordinates: " << max_x << "," << max_y << "\n"; + + return EXIT_SUCCESS; +} diff --git a/2018/puzzle-11-02.cc b/2018/puzzle-11-02.cc new file mode 100644 index 0000000..f877935 --- /dev/null +++ b/2018/puzzle-11-02.cc @@ -0,0 +1,69 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include +#include + +constexpr std::size_t grid_size{300}; +using Int = std::int64_t; +using Grid = std::array, grid_size>; +using SmallGrid = std::array, grid_size - 2>; + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line.\n"; + return EXIT_FAILURE; + } + + Int const serial{std::stoll(line)}; + Grid grid; + for (Int y{0}; y < grid_size; ++y) { + for (Int x{0}; x < grid_size; ++x) { + Int const rack_id{x + 1 + 10}; + Int power{(rack_id * (y + 1) + serial) * rack_id}; + power /= 100; + power %= 10; + power -= 5; + grid.at(y).at(x) = power; + } + } + + Int max_power{std::numeric_limits::min()}; + Int max_x{0}; + Int max_y{0}; + Int max_g{0}; + + Grid small_grid{grid}; + + for (Int g{0}; g < grid_size; ++g) { + Int sgs(grid_size - g); + for (Int y{0}; y < sgs; ++y) { + for (Int x{0}; x < sgs; ++x) { + for (Int dx{0}; dx <= g; ++dx) { + small_grid[y][x] += grid[y + g][x + dx]; + } + for (Int dy{0}; dy < g; ++dy) { + small_grid[y][x] += grid[y + dy][x + g]; + } + + if (max_power < small_grid[y][x]) { + max_power = small_grid[y][x]; + max_x = x + 1; + max_y = y + 1; + max_g = g + 1; + } + } + } + } + std::cout << "Maximum power: " << max_power << "\n"; + std::cout << "Coordinates: " << max_x << "," << max_y << "," << max_g << "\n"; + + return EXIT_SUCCESS; +}