fiddle
This commit is contained in:
parent
f5f1680de3
commit
277b24e9f4
|
@ -18,15 +18,11 @@ auto Solve(std::string const& input, std::size_t n) -> std::size_t
|
||||||
std::size_t uniques {0};
|
std::size_t uniques {0};
|
||||||
|
|
||||||
auto const inc = [&](char c) {
|
auto const inc = [&](char c) {
|
||||||
if (0 == counts[static_cast<std::uint8_t>(c)]++) {
|
uniques += 1 == ++counts[static_cast<std::uint8_t>(c)];
|
||||||
uniques++;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto const dec = [&](char c) {
|
auto const dec = [&](char c) {
|
||||||
if (0 == --counts[static_cast<std::uint8_t>(c)]) {
|
uniques -= 0 == --counts[static_cast<std::uint8_t>(c)];
|
||||||
uniques--;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (std::size_t i = 0; i < n; ++i) {
|
for (std::size_t i = 0; i < n; ++i) {
|
||||||
|
|
64
2022/09.cpp
64
2022/09.cpp
|
@ -15,9 +15,9 @@ using namespace aocpp;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using Dir = Coord(*)(Coord);
|
using DirFn = Coord(Coord);
|
||||||
|
|
||||||
auto ToDir(char c) -> Dir {
|
auto ToDir(char c) -> DirFn* {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'L': return Left;
|
case 'L': return Left;
|
||||||
case 'R': return Right;
|
case 'R': return Right;
|
||||||
|
@ -35,38 +35,48 @@ auto Sign(std::int64_t x) -> std::int64_t
|
||||||
return (x>0) - (x<0);
|
return (x>0) - (x<0);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Solve(std::istream & in) -> std::pair<std::int64_t, std::int64_t>
|
class Logic {
|
||||||
{
|
Coord knots[10] {}; // Current location of each knot in the rope
|
||||||
std::size_t constexpr knotn {10};
|
std::set<Coord> s1{{}}, s9{{}};
|
||||||
Coord knots[knotn] {};
|
|
||||||
std::set<Coord> p1{knots[1]}, p2{knots[9]};
|
|
||||||
char d;
|
|
||||||
long n;
|
|
||||||
|
|
||||||
while (in >> d >> n) {
|
public:
|
||||||
auto const dir = ToDir(d);
|
auto answer() const -> std::pair<std::size_t, std::size_t>
|
||||||
while (n--) {
|
{
|
||||||
knots[0] = dir(knots[0]);
|
return {s1.size(), s9.size()};
|
||||||
|
}
|
||||||
std::size_t k;
|
|
||||||
for (k = 1; k < knotn; ++k) {
|
auto move(DirFn* dirFn) -> void
|
||||||
auto const delta {knots[k-1] - knots[k]};
|
{
|
||||||
if (NormInf(delta) <= 1) {
|
knots[0] = dirFn(knots[0]);
|
||||||
break;
|
|
||||||
}
|
std::size_t k;
|
||||||
knots[k] += {Sign(delta.x), Sign(delta.y)};
|
for (k = 1; k < 10; ++k) {
|
||||||
|
auto const delta {knots[k-1] - knots[k]};
|
||||||
|
if (NormInf(delta) <= 1) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
knots[k] += {Sign(delta.x), Sign(delta.y)};
|
||||||
|
}
|
||||||
|
|
||||||
if (k > 1) {
|
// only update the sets when a knot actually moved
|
||||||
p1.insert(knots[1]);
|
if (k > 1) {
|
||||||
if (k > 9) {
|
s1.insert(knots[1]);
|
||||||
p2.insert(knots[9]);
|
if (k > 9) {
|
||||||
}
|
s9.insert(knots[9]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {p1.size(), p2.size()};
|
auto Solve(std::istream & in) -> std::pair<std::size_t, std::size_t>
|
||||||
|
{
|
||||||
|
Logic logic;
|
||||||
|
char d;
|
||||||
|
std::size_t n;
|
||||||
|
while (in >> d >> n) {
|
||||||
|
while (n--) { logic.move(ToDir(d)); }
|
||||||
|
}
|
||||||
|
return logic.answer();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -33,10 +33,8 @@ auto Solve(Input const& input, std::size_t const rounds) -> std::int64_t
|
||||||
std::size_t const len = input.size();
|
std::size_t const len = input.size();
|
||||||
std::vector<std::size_t> fwds(len), bwds(len);
|
std::vector<std::size_t> fwds(len), bwds(len);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < len; ++i) {
|
std::iota(fwds.begin(), fwds.end()-1, 1); fwds.back() = 0;
|
||||||
fwds[i] = aocpp::Mod<std::int64_t>(i+1, len);
|
std::iota(bwds.begin()+1, bwds.end(), 0); bwds.front() = len-1;
|
||||||
bwds[i] = aocpp::Mod<std::int64_t>(i-1, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (std::size_t r = 0; r < rounds; ++r) {
|
for (std::size_t r = 0; r < rounds; ++r) {
|
||||||
for (std::size_t i = 0; i < len; ++i) {
|
for (std::size_t i = 0; i < len; ++i) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user