-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 1 Part 1.cpp
89 lines (85 loc) · 2 KB
/
Day 1 Part 1.cpp
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
#include <iostream>
#include <cmath>
#include <vector>
#include <sstream>
#include <utility>
#include <map>
enum Direction { North, East, South, West };
struct Position
{
Position(int x, int y, Direction d) : x(x), y(y), direction(d) {}
int x;
int y;
Direction direction;
};
struct Line
{
int x1;
int y1;
int x2;
int y2;
};
int main()
{
std::string directions = "R3, L2, L2, R4, L1, R2, R3, R4, L2, R4, L2, L5, L1, R5, R2, R2, L1, R4, R1, L5, L3, R4, R3, R1, L1, L5, L4, L2, R5, L3, L4, R3, R1, L3, R1, L3, R3, L4, R2, R5, L190, R2, L3, R47, R4, L3, R78, L1, R3, R190, R4, L3, R4, R2, R5, R3, R4, R3, L1, L4, R3, L4, R1, L4, L5, R3, L3, L4, R1, R2, L4, L3, R3, R3, L2, L5, R1, L4, L1, R5, L5, R1, R5, L4, R2, L2, R1, L5, L4, R4, R4, R3, R2, R3, L1, R4, R5, L2, L5, L4, L1, R4, L4, R4, L4, R1, R5, L1, R1, L5, R5, R1, R1, L3, L1, R4, L1, L4, L4, L3, R1, R4, R1, R1, R2, L5, L2, R4, L1, R3, L5, L2, R5, L4, R5, L5, R3, R4, L3, L3, L2, R2, L5, L5, R3, R4, R3, R4, R3, R1,";
std::istringstream ss(directions);
char direction, comma;
int distance;
Position pos(0, 0, North);
while (ss >> direction >> distance >> comma)
{
Position previous = pos;
switch (pos.direction)
{
case North:
if (direction == 'R')
{
pos.x += distance;
pos.direction = East;
}
else
{
pos.x -= distance;
pos.direction = West;
}
break;
case East:
if (direction == 'R')
{
pos.y -= distance;
pos.direction = South;
}
else
{
pos.y += distance;
pos.direction = North;
}
break;
case South:
if (direction == 'R')
{
pos.x -= distance;
pos.direction = West;
}
else
{
pos.x += distance;
pos.direction = East;
}
break;
case West:
if (direction == 'R')
{
pos.y += distance;
pos.direction = North;
}
else
{
pos.y -= distance;
pos.direction = South;
}
break;
}
}
std::cout << std::abs(pos.x) + std::abs(pos.y);
}