aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

day09a.cpp (655B)


      1 #include <algorithm>
      2 #include <iostream>
      3 #include <map>
      4 #include <ranges>
      5 #include <set>
      6 #include <sstream>
      7 #include <string>
      8 #include <string_view>
      9 #include <vector>
     10 using namespace std;
     11 
     12 int main() {
     13 	vector<long long> a;
     14 	char c;
     15 	for (int id = 0; cin >> c; id++)
     16 		for (int j = 0; j < c-'0'; j++)
     17 			a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space
     18 
     19 	int j = 0;
     20 	for (auto& x : a | views::reverse) {
     21 		while (a[j] >= 0) j++;
     22 		if (a[j] == -2) break;
     23 		a[j] = x;
     24 		x = -2; // -2 = freed up
     25 	}
     26 
     27 	long long checksum = 0;
     28 	for (long long i = 0; i < (long long)a.size() && a[i] != -2; i++)
     29 		checksum += i*a[i];
     30 	cout << checksum << endl;
     31 
     32 	return 0;
     33 }