summary refs log tree commit diff
path: root/22.9/main.cpp
blob: 9e0ebca8f00e161b313ffa9ee6f8130c6e896619 (plain)
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
#include <cstdio>
#include <unordered_set>
#include <iostream>
using namespace std;

int signum(int n) {
	if (0 < n) return 1;
	if (n < 0) return -1;
	return 0;
}

struct Pos {
	int x, y;

	Pos add(Pos other) {
		return Pos{this->x+other.x,this->y+other.y};
	}
	Pos sub(Pos other) {
		return Pos{this->x-other.x,this->y-other.y};
	}
	
	bool touching(Pos other) {
		return abs(this->x - other.x) <= 1
			&& abs(this->y - other.y) <= 1;
	}

	Pos moveTowards(Pos other) {
		Pos p = *this;
		if (!touching(other)) {
			p.x += signum(other.x - this->x);
			p.y += signum(other.y - this->y);
		}
		return p;
	}

	bool operator==(Pos const& p) const noexcept
	{
		return x == p.x && y == p.y;
	}
};

template<>
struct std::hash<Pos>
{
	size_t operator()(Pos const& p) const noexcept
	{
		return std::hash<int>{}(p.x) ^ (std::hash<int>{}(p.x) << 1);
	}
};

int main() {
	char dir;
	int steps;
	Pos bridge[10] = {0};
	unordered_set<Pos> visited1;
	unordered_set<Pos> visited2;
	while (scanf("%c %d ", &dir, &steps) == 2) {
		Pos d;
		switch (dir) {
			case 'R': d = {1, 0}; break;
			case 'L': d = {-1, 0}; break;
			case 'U': d = {0, -1}; break;
			case 'D': d = {0, 1}; break;
			default: throw "bad input";
		}
		for (int i = 0; i < steps; i++) {
			bridge[0] = bridge[0].add(d);
			for (int j = 1; j < 10; j++) {
				bridge[j] = bridge[j].moveTowards(bridge[j-1]);
			}
			visited1.insert(bridge[1]);
			visited2.insert(bridge[9]);
		}
	}
	cout << visited1.size() << endl;
	cout << visited2.size() << endl;
}