Add includes of cassert

This commit is contained in:
2023-11-30 20:29:35 +00:00
parent f421aee783
commit be27621663
41 changed files with 140 additions and 77 deletions

View File

@@ -6,6 +6,7 @@
#define ADVENT_OF_CODE_GRAPH_UTILS_H
#include <algorithm>
#include <cassert>
#include <limits>
#include <map>
#include <numeric>
@@ -133,14 +134,14 @@ auto dijkstra(Node const& initial, Cost initial_cost, TransitionManager transiti
current_cost = cost_it->first;
assert(iter < nodes.size());
assert(std::accumulate(costs.begin(), costs.end(), std::size_t{0}, [](auto a, auto c) {
return a + c.second.size();
}) == nodes.size() - iter);
return a + c.second.size();
}) == nodes.size() - iter);
for (auto& nodep : cost_it->second) {
if (iter++ % 100'000 == 0) {
std::cout << "Iteration: " << iter << " cost " << current_cost
<< " total number of nodes: " << nodes.size()
<< ", number of costs left to visit: " << costs.size()
<< ", number of nodes left: " << (nodes.size() - iter) << '\n';
<< " total number of nodes: " << nodes.size()
<< ", number of costs left to visit: " << costs.size()
<< ", number of nodes left: " << (nodes.size() - iter) << '\n';
}
if (transition_manager.is_finished(*nodep)) {
auto result{std::make_pair(*nodep, current_cost)};

View File

@@ -6,6 +6,7 @@
#include <iostream>
#include <map>
#include <string>
#include <cassert>
using Int = std::int64_t;

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 02/12/2022.
//
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <map>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 02/12/2022.
//
#include <cassert>
#include <algorithm>
#include <cstdlib>
#include <iostream>
@@ -20,7 +21,7 @@ auto main() -> int
auto left{line.substr(0, line.size() / 2)};
auto right{line.substr(line.size() / 2)};
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
std::sort(right.begin(), right.end());
std::string result;
std::set_intersection(left.begin(), left.end(), right.begin(), right.end(),
std::back_inserter(result));

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 02/12/2022.
//
#include <cassert>
#include <algorithm>
#include <cstdlib>
#include <iostream>

View File

@@ -6,6 +6,7 @@
#include <regex>
#include <string>
#include <vector>
#include <cassert>
using UInt = std::uint64_t;

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 05/12/2021.
//
#include <cassert>
#include <iostream>
#include <regex>
#include <string>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 07/12/2022.
//
#include <cassert>
#include <iostream>
#include <list>
#include <map>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 07/12/2022.
//
#include <cassert>
#include <iostream>
#include <list>
#include <map>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 08/12/2022.
//
#include <cassert>
#include <complex>
#include <iostream>
#include <map>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 08/12/2022.
//
#include <cassert>
#include <complex>
#include <iostream>
#include <map>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 09/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>
@@ -18,17 +19,20 @@ enum class Op { undef, add, mult, dbl, square };
struct Monkey
{
auto add_item(UInt score) { items_.push_back(score); }
auto op(Op op)
{
assert(op == Op::dbl || op == Op::square);
op_ = op;
}
auto op(Op op, UInt amt)
{
assert(op == Op::add || op == Op::mult);
op_ = op;
amt_ = amt;
}
auto test(UInt test) { test_ = test; }
auto true_monkey(UInt monkey) { true_monkey_ = monkey; }
auto false_monkey(UInt monkey) { false_monkey_ = monkey; }

View File

@@ -7,6 +7,7 @@
#include <string>
#include <utility>
#include <vector>
#include <cassert>
#include "position.h"

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 09/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>
@@ -54,14 +55,14 @@ auto main() -> int
// Distance(ish) to row we're interested in:
Int const row_dist{std::abs(sy - row)};
std::cout << "Sensor (" << sx << ", " << sy << "):\n"
<< " Nearest beacon is: " << bx << ", " << by << "\n"
<< " Distance: " << dist << "\n"
<< " y-Distance to row " << row << ": " << row_dist << "\n";
<< " Nearest beacon is: " << bx << ", " << by << "\n"
<< " Distance: " << dist << "\n"
<< " y-Distance to row " << row << ": " << row_dist << "\n";
// If that distance is non-negative then we have places the beacon can't be
if (row_dist <= dist) {
Int const x_dist{dist - row_dist};
std::cout << " x distance left: " << x_dist << "\n"
<< " Range: " << sx - x_dist << " - " << sx + x_dist + 1 << "\n";
<< " Range: " << sx - x_dist << " - " << sx + x_dist + 1 << "\n";
Range const missing{sx - x_dist, sx + x_dist + 1};
ranges.insert(missing);
}
@@ -83,7 +84,7 @@ auto main() -> int
}
auto b{std::max(r.first, last_pos)};
std::cout << " Taking range start as: " << b << '\n'
<< " Adding to count: " << r.second - b << "\n";
<< " Adding to count: " << r.second - b << "\n";
count += (r.second - b);
last_pos = r.second;
}

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 09/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>
@@ -21,7 +22,8 @@ using namespace std::string_literals;
struct Grid3
{
explicit Grid3(std::size_t size) : size_(size) { map_.resize(size * size * size, false); }
explicit Grid3(std::size_t size)
: size_(size) { map_.resize(size * size * size, false); }
auto point(Point3 const& p) const noexcept -> bool
{

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>
@@ -21,7 +22,8 @@ using namespace std::string_literals;
struct Grid3
{
explicit Grid3(std::size_t size) : size_(size) { map_.resize(size * size * size, ' '); }
explicit Grid3(std::size_t size)
: size_(size) { map_.resize(size * size * size, ' '); }
auto point(Point3 const& p) const noexcept -> char
{

View File

@@ -8,6 +8,7 @@
#include <numeric>
#include <stdexcept>
#include <utility>
#include <cassert>
using Int = std::int64_t;
using UInt = std::uint64_t;
@@ -19,10 +20,13 @@ struct Monkey
{
Monkey() = default;
explicit Monkey(Int value) : value_(value), value_known_(true) {}
explicit Monkey(Int value)
: value_(value), value_known_(true)
{
}
Monkey(std::string lhs, char op, std::string rhs)
: lhs_(std::move(lhs)), rhs_(std::move(rhs)), op_(op)
: lhs_(std::move(lhs)), rhs_(std::move(rhs)), op_(op)
{
}

View File

@@ -8,6 +8,7 @@
#include <numeric>
#include <stdexcept>
#include <utility>
#include <cassert>
using Int = std::int64_t;
using UInt = std::uint64_t;
@@ -19,10 +20,13 @@ struct Monkey
{
Monkey() = default;
explicit Monkey(Int value) : value_(value), value_known_(true) {}
explicit Monkey(Int value)
: value_(value), value_known_(true)
{
}
Monkey(std::string lhs, char op, std::string rhs)
: lhs_(std::move(lhs)), rhs_(std::move(rhs)), op_(op)
: lhs_(std::move(lhs)), rhs_(std::move(rhs)), op_(op)
{
}
@@ -68,13 +72,13 @@ auto calculate(Int lhs, char op, Int rhs)
auto find_lhs(Int result, char op, Int rhs)
{
switch (op) {
case '+': // result = lhs + rhs -> lhs = result - rhs;
case '+': // result = lhs + rhs -> lhs = result - rhs;
return result - rhs;
case '-': // result = lhs - rhs -> lhs = result + rhs;
case '-': // result = lhs - rhs -> lhs = result + rhs;
return result + rhs;
case '*': // result = lhs * rhs -> lhs = result / rhs
case '*': // result = lhs * rhs -> lhs = result / rhs
return result / rhs;
case '/': // result = lhs / rhs -> lhs = result * rhs;
case '/': // result = lhs / rhs -> lhs = result * rhs;
return result * rhs;
case '=':
return rhs;
@@ -86,13 +90,13 @@ auto find_lhs(Int result, char op, Int rhs)
auto find_rhs(Int result, Int lhs, char op)
{
switch (op) {
case '+': // result = lhs + rhs -> rhs = result - lhs;
case '+': // result = lhs + rhs -> rhs = result - lhs;
return result - lhs;
case '-': // result = lhs - rhs -> rhs = lhs - result;
case '-': // result = lhs - rhs -> rhs = lhs - result;
return lhs - result;
case '*': // result = lhs * rhs -> rhs = result / lhs
case '*': // result = lhs * rhs -> rhs = result / lhs
return result / lhs;
case '/': // result = lhs / rhs -> rhs = lhs / result;
case '/': // result = lhs / rhs -> rhs = lhs / result;
return lhs / result;
case '=':
return lhs;

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <map>
@@ -150,7 +151,7 @@ auto main() -> int
grid.push_back(line);
max_row_length = std::max(max_row_length, line.size());
}
for (auto& row : grid) { row.resize(max_row_length, ' ');};
for (auto& row : grid) { row.resize(max_row_length, ' '); };
std::string instructions;
if (!std::getline(std::cin, instructions)) {
@@ -183,7 +184,7 @@ auto main() -> int
Int const facing{encode_direction(direction)};
std::cout << " Position: " << pos.first << ", " << pos.second << " Facing: " << direction.first
<< ", " << direction.second << " = " << facing << "\n";
<< ", " << direction.second << " = " << facing << "\n";
}
Int const facing{encode_direction(direction)};
@@ -193,7 +194,7 @@ auto main() -> int
std::cout << row << "\n";
}
std::cout << "Position: " << pos.first << ", " << pos.second << " Facing: " << direction.first
<< ", " << direction.second << " = " << facing << "\n";
<< ", " << direction.second << " = " << facing << "\n";
std::cout << "Encoding: " << pos.second * 1000 + pos.first * 4 + facing;
return EXIT_SUCCESS;

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <map>
@@ -127,7 +128,7 @@ auto wrap_round(Point const& pos, Point const& direction) -> std::pair<Point, Po
return {Point{square_size * 2 + x, 0}, down};
}
assert(((sx == 1 || sx == 2) && sy == 0) || (sx == 1 && sy == 1) ||
((sx == 0 || sx == 1) && sy == 2) || (sx == 0 && sy == 3));
((sx == 0 || sx == 1) && sy == 2) || (sx == 0 && sy == 3));
return {pos, direction};
}
@@ -180,8 +181,8 @@ auto move_pos(Grid& grid, Point pos, Point direction, Int amt) -> std::pair<Poin
}
if (direction != next_direction) {
std::cout << " Change of direction: " << pos.first << ", " << pos.second << " -> "
<< next_pos.first << ", " << next_pos.second << ". Direction "
<< direction_char(direction) << " -> " << direction_char(next_direction) << "\n";
<< next_pos.first << ", " << next_pos.second << ". Direction "
<< direction_char(direction) << " -> " << direction_char(next_direction) << "\n";
}
pos = next_pos;
direction = next_direction;
@@ -259,7 +260,7 @@ auto main() -> int
Int const facing{encode_direction(direction)};
std::cout << " Position: " << pos.first << ", " << pos.second << " Facing: " << direction.first
<< ", " << direction.second << " = " << facing << "\n";
<< ", " << direction.second << " = " << facing << "\n";
}
Int const facing{encode_direction(direction)};
@@ -269,7 +270,7 @@ auto main() -> int
std::cout << row << "\n";
}
std::cout << "Position: " << pos.first << ", " << pos.second << " Facing: " << direction.first
<< ", " << direction.second << " = " << facing << "\n";
<< ", " << direction.second << " = " << facing << "\n";
std::cout << "Encoding: " << pos.second * 1000 + pos.first * 4 + facing;
return EXIT_SUCCESS;

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>
@@ -45,8 +46,8 @@ auto next_state(Grid const& grid, Dirs const& dirs) -> Grid
++num_pts;
if (std::all_of(all_dirs.begin(), all_dirs.end(), [&x, &y, &grid](auto const& dir) {
return grid.at(dir.second + y).at(dir.first + x) == '.';
})) {
return grid.at(dir.second + y).at(dir.first + x) == '.';
})) {
result.at(y).at(x) = '#';
continue;
}
@@ -55,8 +56,8 @@ auto next_state(Grid const& grid, Dirs const& dirs) -> Grid
Point direction{0, 0};
for (auto const& ds : dirs) {
if (std::all_of(ds.begin(), ds.end(), [&x, &y, &grid](auto const& d) {
return grid.at(y + d.second).at(x + d.first) == '.';
})) {
return grid.at(y + d.second).at(x + d.first) == '.';
})) {
moved = true;
direction = ds[1];
}
@@ -160,6 +161,7 @@ auto calc_score(Grid const& grid) -> UInt
}
using namespace std::string_literals;
auto main() -> int
{
std::string line;

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>
@@ -45,8 +46,8 @@ auto next_state(Grid const& grid, Dirs const& dirs) -> Grid
++num_pts;
if (std::all_of(all_dirs.begin(), all_dirs.end(), [&x, &y, &grid](auto const& dir) {
return grid.at(dir.second + y).at(dir.first + x) == '.';
})) {
return grid.at(dir.second + y).at(dir.first + x) == '.';
})) {
result.at(y).at(x) = '#';
continue;
}
@@ -55,8 +56,8 @@ auto next_state(Grid const& grid, Dirs const& dirs) -> Grid
Point direction{0, 0};
for (auto const& ds : dirs) {
if (std::all_of(ds.begin(), ds.end(), [&x, &y, &grid](auto const& d) {
return grid.at(y + d.second).at(x + d.first) == '.';
})) {
return grid.at(y + d.second).at(x + d.first) == '.';
})) {
moved = true;
direction = ds[1];
}
@@ -160,6 +161,7 @@ auto calc_score(Grid const& grid) -> UInt
}
using namespace std::string_literals;
auto main() -> int
{
std::string line;
@@ -206,6 +208,6 @@ auto main() -> int
UInt const score{calc_score(grid)};
std::cout << "Score: " << score << "\n";
std::cout << "Number of turns: " << num_rounds<< "\n";
std::cout << "Number of turns: " << num_rounds << "\n";
return EXIT_SUCCESS;
}

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>

View File

@@ -2,6 +2,7 @@
// Created by Matthew Gretton-Dann on 16/12/2022.
//
#include <cassert>
#include <array>
#include <iostream>
#include <list>