aboutsummaryrefslogtreecommitdiff
path: root/2022/src/day07.cpp
blob: 8a8decdfbf5ce9d76b4e216fa81eda3f6b117f79 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <string>
#include <vector>
using namespace std;

struct Node {
	Node* parent = nullptr;

	map<string, shared_ptr<Node>> sub;
	map<string, long>             files;
};

Node
read_file(string_view filename, ostream& debug)
{
	Node  root;
	Node* current = &root;

	fstream input{ filename };

	string line;
	getline(input, line);

	while ( input ) {
		if ( line.starts_with("$ ") ) {
			if ( line == "$ cd /" ) {
				debug << "cd root" << endl;
				current = &root;
				getline(input, line);
			}
			else if ( line == "$ cd .." ) {
				debug << "cd one up" << endl;
				current = current->parent;
				getline(input, line);
			}
			else if ( line == "$ ls" ) {
				debug << "list dir" << endl;
				getline(input, line);

				while ( input && !line.starts_with("$ ") ) {
					if ( line.starts_with("dir ") ) {
						const auto name    = line.substr(4);
						const auto node    = make_shared<Node>();
						node->parent       = current;
						current->sub[name] = node;
						debug << "new subdir " << name << endl;
					}
					else {
						const auto pos       = line.find(' ');
						const auto size      = stol(line.substr(0, pos));
						const auto name      = line.substr(pos + 1);
						current->files[name] = size;
						debug << "new file " << name << " (" << size << ")" << endl;
					}

					getline(input, line);
				}
			}
			else if ( line.starts_with("$ cd ") ) {
				const auto dirname = line.substr(5);
				current            = current->sub[dirname].get();
				debug << "cd into " << dirname << endl;
				getline(input, line);
			}
			else {
				cerr << "error in line: " << line << endl;
				exit(EXIT_FAILURE);
			}
		}
		else {
			cerr << "error II in line: " << line << endl;
			exit(EXIT_FAILURE);
		}
	}

	return root;
}

void
print(const Node& node, const string& indent = "")
{
	if ( node.parent == nullptr ) {
		cout << indent << "/" << endl;
	}

	for ( const auto& [name, sub]: node.sub ) {
		cout << indent << name << endl;
		print(*sub.get(), indent + "  ");
	}

	for ( const auto& [filename, filesize]: node.files ) {
		cout << indent << filename << " (" << filesize << ")" << endl;
	}
}

long
part1_rec(const Node& node, long& all)
{
	long size = 0;

	for ( const auto& [name, sub]: node.sub ) {
		const auto sub_size = part1_rec(*sub.get(), all);
		if ( sub_size <= 100000 ) {
			all += sub_size;
		}
		size += sub_size;
	}

	for ( const auto& [filename, filesize]: node.files ) {
		size += filesize;
	}

	return size;
}

void
part1(const Node& node)
{
	long all = 0;
	part1_rec(node, all);
	cout << all << endl;
}

long
part2_rec(const Node& node, vector<long>& sizes)
{
	long size = 0;

	for ( const auto& [name, sub]: node.sub ) {
		const auto sub_size = part2_rec(*sub.get(), sizes);
		sizes.emplace_back(sub_size);
		size += sub_size;
	}

	for ( const auto& [filename, filesize]: node.files ) {
		size += filesize;
	}

	return size;
}

void
part2(const Node& node)
{
	vector<long> sizes;
	const auto   needed = 30000000 - (70000000 - part2_rec(node, sizes));

	vector<long> candidates;
	copy_if(begin(sizes), end(sizes), back_inserter(candidates), [&](auto size) {
		return size >= needed;
	});

	sort(begin(candidates), end(candidates));
	cout << candidates[0] << endl;
}

int
main()
{
	struct NullBuffer : public streambuf {
		int overflow(int chr) final { return chr; }
	};

	NullBuffer nullBuffer;
	ostream    nullStream(&nullBuffer);

	const auto node = read_file("data/day07.txt", nullStream);

	part1(node);
	part2(node);
}