aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day07.cpp
blob: 899375de9ea4b1da446b261a0169818862d5c9f8 (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
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
136
137
138
139
140
141
142
143
144
145
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

using namespace std;

namespace {

struct Node {
	int              weight;
	optional<string> parent;
	vector<string>   children;
};

vector<string>
split(const string& line, const string& delimiters)
{
	vector<string> result;

	size_t start = 0;
	size_t end   = 0;

	while ( (end = line.find_first_of(delimiters, start)) != string::npos ) {
		if ( end != start ) {
			result.emplace_back(line.substr(start, end - start));
		}

		start = end + 1;
	}

	if ( start != line.size() ) {
		result.emplace_back(line.substr(start));
	}

	return result;
}

map<string, Node>
read_file(const filesystem::path& filename)
{
	ifstream file{ filename };

	map<string, Node> data; // name -> (weight, parent)

	for ( string line; getline(file, line); ) {
		const auto  parts  = split(line, " ()->,");
		const auto& parent = parts.at(0);

		data[parent].weight = stoi(parts.at(1));

		for ( size_t idx = 2; idx != parts.size(); ++idx ) {
			const auto& child  = parts.at(idx);
			data[child].parent = parent;
			data[parent].children.emplace_back(child);
		}
	}

	return data;
}

string
root_node(const map<string, Node>& data)
{
	for ( const auto& [name, node]: data ) {
		if ( !node.parent ) {
			return name;
		}
	}
	throw runtime_error("no root found");
}

void
part1(const map<string, Node>& data)
{
	cout << "Part1: " << root_node(data) << '\n';
}

int
calculate_weight(const map<string, Node>& data, const string& name) // NOLINT
{
	const auto& node = data.at(name);

	if ( node.children.empty() ) {
		return node.weight;
	}

	int total_weight = node.weight;

	map<string, int> child_weights;
	for ( const auto& child: node.children ) {
		auto child_weight = calculate_weight(data, child);
		child_weights.emplace(child, child_weight);
		total_weight += child_weight;
	}

	map<int, vector<string>> grouped;
	for ( const auto& [child, weight]: child_weights ) {
		grouped[weight].emplace_back(child);
	}

	if ( grouped.size() != 1 ) {
		int    wrong_weight{};
		int    correct_weight{};
		string wrong_name;

		for ( const auto& [weight, names]: grouped ) {
			if ( names.size() == 1 ) {
				wrong_weight = weight;
				wrong_name   = names.front();
			}
			else {
				correct_weight = weight;
			}
		}

		const auto& wrong_node = data.at(wrong_name);
		auto        diff       = wrong_weight - correct_weight;

		cout << "Part2: " << wrong_node.weight - diff << '\n';

		exit(0);
	}

	return total_weight;
}

void
part2(const map<string, Node>& data)
{
	auto node = root_node(data);
	calculate_weight(data, node);
}

} // namespace

int
main()
{
	auto data = read_file("data/day07.txt");
	part1(data);
	part2(data);
}