-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday9.cpp
More file actions
135 lines (123 loc) · 3.35 KB
/
Copy pathday9.cpp
File metadata and controls
135 lines (123 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include <map>
#include <vector>
#include "utils/coordinate.h"
#define LOG(x) std::cout << x << std::endl;
const std::string inputFilePath = "inputs/day9.txt";
const std::map<char, Coordinate> Directions = {
{'L',{-1, 0}},
{'R',{1, 0}},
{'U',{0, -1}},
{'D',{0, 1}}
};
void moveTail(Coordinate &tailsPos, Coordinate headPos, std::vector<Coordinate>* visitedPositions = nullptr, int* tailsVisitedPos = nullptr){
if (abs(headPos.x - tailsPos.x) > 1)
{
if (headPos.x > tailsPos.x)
{
tailsPos.x++;
}
else
{
tailsPos.x--;
}
if (headPos.y != tailsPos.y)
{ // move diagonally
if (headPos.y > tailsPos.y)
{
tailsPos.y++;
}
else
{
tailsPos.y--;
}
}
}
if (abs(headPos.y - tailsPos.y) > 1)
{
if (headPos.y > tailsPos.y)
{
tailsPos.y++;
}
else
{
tailsPos.y--;
}
if (headPos.x != tailsPos.x)
{ // move diagonally
if (headPos.x > tailsPos.x)
{
tailsPos.x++;
}
else
{
tailsPos.x--;
}
}
}
if (visitedPositions != nullptr){
bool alreadyVisited = false;
for (int i = 0; i < visitedPositions->size(); i++)
{
if (visitedPositions->at(i) == tailsPos)
{
alreadyVisited = true;
break;
}
}
if (!alreadyVisited)
{
visitedPositions->push_back(tailsPos);
(*tailsVisitedPos)++;
}
}
}
int main(){
Coordinate headPos = {1,1};
std::vector<Coordinate> tailsPositions = {
{1,1}, // 1
{1,1},
{1,1},
{1,1},
{1,1},
{1,1},
{1,1},
{1,1},
{1,1} // 9
};
int tailsVisitedPos = 1;
int ninthTailsVisitedPos = 1;
std::vector<Coordinate> visitedPositions;
std::vector<Coordinate> visitedPositionsNinthTail;
visitedPositions.push_back({1, 1});
visitedPositionsNinthTail.push_back({1, 1});
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::string line;
while(std::getline(ifs, line)){
// Parse input
char direction = line[0];
int amount = stoi(line.substr(2));
// Move head
for(int i = 0; i < amount; i++){
headPos += Directions.at(direction);
for(int j=0; j < tailsPositions.size(); j++){
// Move tails. For tails 1 and 9 calculate their visited positions (Ex1 and Ex2)
if (j == 0){
moveTail(tailsPositions[j], headPos, &visitedPositions, &tailsVisitedPos);
}
else if (j == 8){
moveTail(tailsPositions[j], tailsPositions[j-1], &visitedPositionsNinthTail, &ninthTailsVisitedPos);
}
else{
moveTail(tailsPositions[j], tailsPositions[j-1]);
}
}
}
}
std::cout << "--Ex1 Output: " << tailsVisitedPos << std::endl;
std::cout << "--Ex2 Output: " << ninthTailsVisitedPos << std::endl;
return 0;
}