aboutsummaryrefslogtreecommitdiff
path: root/2025/src/day06.cpp
blob: af562cd684a7e2679ef7e2e75338b168c7ffa2a9 (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
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

namespace {

using Grid = vector<vector<long>>;
using Ops  = vector<char>;

vector<string>
read_file(const filesystem::path& filename)
{
	ifstream       file{ filename };
	vector<string> lines;

	for ( string line; getline(file, line); ) {
		lines.emplace_back(line);
	}

	return lines;
}

vector<char>
read_ops(const string& line)
{
	stringstream strm{ line };
	vector<char> ops;
	for ( char chr{}; strm >> chr; ) {
		ops.emplace_back(chr);
	}
	return ops;
}

vector<long>
read_numbers(const string& line)
{
	stringstream strm{ line };
	vector<long> numbers;
	for ( long number{}; strm >> number; ) {
		numbers.emplace_back(number);
	}
	return numbers;
}

void
part1(vector<string> lines)
{
	ranges::reverse(lines);

	const auto ops = read_ops(lines[0]);

	vector<vector<long>> grid;
	for ( size_t i = 1; i < lines.size(); ++i ) {
		grid.emplace_back(read_numbers(lines[i]));
	}

	long sum = 0;
	for ( size_t col = 0; col != ops.size(); ++col ) {
		long value = ops[col] == '*' ? 1 : 0;
		for ( auto& row: grid ) {
			if ( ops[col] == '*' ) {
				value *= row[col];
			}
			else {
				value += row[col];
			}
		}
		sum += value;
	}
	cout << "Part 1: " << sum << '\n';
}

vector<string>
rotateLeft90(const vector<string>& matrix)
{
	if ( matrix.empty() ) {
		return {};
	}

	auto rows = matrix.size();
	auto cols = matrix[0].size();

	vector<string> res(cols, string(rows, ' '));

	for ( size_t row = 0; row != rows; ++row ) {
		for ( size_t col = 0; col != cols; ++col ) {
			// Position aus ursprünglicher Matrix auf neue übertragen
			res[cols - 1 - col][row] = matrix[row][col];
		}
	}

	return res;
}

void
part2(vector<string> lines)
{
	auto ops = read_ops(lines.back());
	ranges::reverse(ops);
	lines.pop_back(); // ops entfernen

	// Matrix drehen
	lines = rotateLeft90(lines);
	lines.emplace_back("");

	vector<vector<long>> grid;
	vector<long>         curr;

	// Matrix neu aufbauen
	for ( const auto& line: lines ) {
		if ( long number{}; sscanf(line.data(), "%ld", &number) == 1 ) {
			curr.emplace_back(number);
		}
		else {
			grid.emplace_back(curr);
			curr.clear();
		}
	}

	// ausrechnen
	long sum = 0;
	for ( size_t row = 0; row != ops.size(); ++row ) {
		long value = ops[row] == '*' ? 1 : 0;
		for ( const auto number: grid[row] ) {
			if ( ops[row] == '*' ) {
				value *= number;
			}
			else {
				value += number;
			}
		}
		sum += value;
	}
	cout << "Part 2: " << sum << '\n';
}

} // namespace

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