summary refs log tree commit diff
path: root/22.3/main.cpp
blob: 9827909608500b33c939e18e380e824764d846b6 (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
#include <algorithm>
#include <iostream>
#include <string>
#include <cstdint>
using namespace std;

int
getPriority(char c)
{
	// [1;52]
	if ('a' <= c && c <= 'z') return c - 'a' + 1;
	if ('A' <= c && c <= 'Z') return c - 'A' + 27;
	throw "unexpected input";
}

int
main()
{
	long total1 = 0;
	long total2 = 0;
	string line; // don't reallocate
	while (!cin.eof()) {
		uint8_t masks[52] = {0};
		for (int i = 0; i < 3; i++) {
			bool inFirstHalf[52] = {0};
			int found = -1;

			if (!getline(cin, line))	break;
			if (line.length() % 2 != 0)	throw "unexpected input";

			for (int j = 0; j < line.length(); j++) {
				int p = getPriority(line[j]);
				masks[p-1] = masks[p-1] | (1 << i);
				if (j < line.length() / 2) {
					inFirstHalf[p-1] = true;
				} else {
					if (inFirstHalf[p-1]) {
						if (found != -1 && found != p)
							throw "bad input";
						found = p;
					}
				}
			}
			if (found == -1)	throw "bad input";
			total1 += found;
		}
		if (!cin.eof()) {
			if (count(begin(masks), end(masks), 7) != 1)
				throw "bad input";
			total2 += find(begin(masks), end(masks), 7) - begin(masks) + 1;
		}
	}
	cout << total1 << endl;
	cout << total2 << endl;
}