Unable to load image

Advent of Code 2022 : Day 9

grate filter edition

:#marseycapyhacker:

17
Jump in the discussion.

No email address required.

In my part one, i just set the tail = previous position of head, if head and tail were >sqrt(2) away, took me way to long, to notice it


#include <iostream> 
#include <cmath>
#include <set>
#include <fstream>
#include <string>
#include <vector>
#include <numbers>

struct Point {
    int x = 0;
    int y = 0;

    auto operator<=>(const Point&) const = default;

    double inline distance(const Point& b) const {
        return std::sqrt(
            std::pow(x - b.x, 2) +
            std::pow(y - b.y, 2)
        );
    }

    Point calculate_move_vector(const Point& p) const {
        Point out;
        out.x = (p.x - x);
        out.y = (p.y - y);
        if(out.x != 0) out.x = out.x / std::abs(out.x);
        if(out.y != 0) out.y = out.y / std::abs(out.y);
        return out;
    }

    Point& operator+=(const Point& p) {
        this->x += p.x;
        this->y += p.y;
        return *this;
    }
};

struct Instruction {
    char direction;
    int moves;
};

void move(std::vector<Point>& knots, Instruction ins, std::set<Point>& v) {
    Point dir;
    if (ins.direction == 'R') dir = { 1, 0 };
    else if (ins.direction == 'U') dir = { 0, 1 };
    else if (ins.direction == 'L') dir = { -1, 0 };
    else dir = { 0, -1 };

    while (ins.mp4es > 0) {
        ins.mp4es = ins.mp4es - 1;
        knots[0] += dir;
        for (size_t i{ 1 }; i < knots.size(); ++i) {
            if (knots[i - 1].distance(knots[i]) > std::numbers::sqrt2_v<double>) knots[i] += knots[i].calculate_move_vector(knots[i - 1]);
        }
        v.insert(knots.back());
    }
}


int main()
{
    std::set<Point> visited_part_one;
    std::set<Point> visited_part_two;

    std::vector<Point> partOne{ { 0, 0 } , { 0, 0 } };
    std::vector<Point> partTwo(10);

    std::ifstream file("input.txt");

    std::string line;
    char cmd;
    while (file >> cmd) {
        Instruction ins;
        ins.direction = cmd;
        file >> ins.mp4es;
        move(partOne, ins, visited_part_one);
        move(partTwo, ins, visited_part_two);
    }
    std::cout << "Part One:" << visited_part_one.size() << '\n';
    std::cout << "Part Two:" << visited_part_two.size() << '\n';

    return -1;
}


Jump in the discussion.

No email address required.

That's nice sweaty. Why don't you have a seat in the time out corner with Pizzashill until you calm down, then you can have your Capri Sun.

Jump in the discussion.

No email address required.

In my part one, i just set the tail = previous position of head, if head and tail were >sqrt(2) away, took me way to long, to notice it

Seems to be true even for part2 (have not tested), if so then we are all r-slurs and you're our half-r-slur king.

Jump in the discussion.

No email address required.

No, that implementation works similar to the snake game, but part two requires it to actually work like a rope.

There is a visualisation on reddit that kinda illustrate how it is supposed to work https://old.reddit.com/r/adventofcode/comments/zgq3nr/2022_day_9_rope_pull/ and why just setting the position of the following parts to the old poisition of the current part doesnt work.

Jump in the discussion.

No email address required.

Ah, I see, thank you, the whole rope behavior is different. Interesting.

Jump in the discussion.

No email address required.



Now playing: Hot-Head Bop (DKC2).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.