Skip to content
Snippets Groups Projects
Commit 464ea496 authored by Felix Agner's avatar Felix Agner
Browse files

sorry, no code cleaning here...

parent 5b407987
Branches
No related tags found
No related merge requests found
#include <bits/stdc++.h>
using namespace std;
const bool DEBUG = false;
#define coord pair<int, int>
vector<vector<int>> trailMap;
vector<coord> trailHeads;
coord mapSize;
const vector<coord> DIRS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void parseMap() {
DEBUG && cout << "Parsing map" << endl;
string line;
stringstream ss;
char c;
int row = 0;
int col = 0;
while (getline(cin, line)) {
DEBUG && cout << "Read line: " << line << endl;
ss = stringstream(line);
col = 0;
while (ss >> c) {
if (c == '0') trailHeads.push_back({row, col});
if (col == 0) trailMap.push_back({c - '0'});
else trailMap[row].push_back(c - '0');
col++;
}
row++;
}
mapSize = {row, col};
}
bool printMap() {
for (int i = 0; i < mapSize.first; i++) {
for (int j = 0; j < mapSize.second; j++) {
cout << trailMap[i][j] << " ";
}
cout << endl;
}
return false;
}
bool isInBounds(coord c) {
return c.first >= 0 && c.first < mapSize.first && c.second >= 0 && c.second < mapSize.second;
}
bool islegall(coord curr, coord next) {
return isInBounds(next) && trailMap[next.first][next.second] == trailMap[curr.first][curr.second] + 1;
}
int searchPart1(coord head) {
DEBUG && cout << "Searching from " << head.first << " " << head.second << endl;
vector<coord> toSearch = {head};
int n_nines = 0;
array<array<bool, 100>, 100> visited = {false};
while (!toSearch.empty()) {
coord curr = toSearch.back();
toSearch.pop_back();
if (visited[curr.first][curr.second]) continue;
visited[curr.first][curr.second] = true;
if (trailMap[curr.first][curr.second] == 9) {
n_nines++;
DEBUG && cout << " Found a 9 at " << curr.first << " " << curr.second << endl;
continue;
}
for (coord dir : DIRS) {
coord next = {curr.first + dir.first, curr.second + dir.second};
if (islegall(curr,next)) toSearch.push_back(next);
}
}
return n_nines;
}
int searchPart2(coord head) {
DEBUG && cout << "Searching from " << head.first << " " << head.second << endl;
vector<coord> toSearch = {head};
int n_nines = 0;
//array<array<bool, 100>, 100> visited = {false};
while (!toSearch.empty()) {
coord curr = toSearch.back();
toSearch.pop_back();
//if (visited[curr.first][curr.second]) continue;
//visited[curr.first][curr.second] = true;
if (trailMap[curr.first][curr.second] == 9) {
n_nines++;
DEBUG && cout << " Found a 9 at " << curr.first << " " << curr.second << endl;
continue;
}
for (coord dir : DIRS) {
coord next = {curr.first + dir.first, curr.second + dir.second};
if (islegall(curr,next)) toSearch.push_back(next);
}
}
return n_nines;
}
int main () {
parseMap();
DEBUG && printMap();
int p1 = 0;
int p2 = 0;
for (coord head : trailHeads) {
p1 += searchPart1(head);
p2 += searchPart2(head);
}
cout << "Part 1: " << p1 << endl;
cout << "Part 2: " << p2 << endl;
return 0;
}
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
const bool DEBUG = true;
#define ll long long
void add_to_map(map<ll, ll> &x, ll key, ll value) {
if (x.find(key) == x.end()) {
x[key] = value;
} else {
x[key] += value;
}
}
map<ll,ll> step(const map<ll,ll> &x0) {
map<ll, ll> x1;
for (auto [stone, count] : x0) {
if (count == 0) continue;
string numstring = to_string(stone);
if (stone == 0) add_to_map(x1, 1, count);
else if (numstring.size() % 2 == 0) {
ll left = stoi(numstring.substr(0, numstring.size() / 2));
ll right = stoi(numstring.substr(numstring.size() / 2, numstring.size() / 2));
add_to_map(x1, left, count);
add_to_map(x1, right, count);
} else {
if (stone >= LLONG_MAX / 2024) cout << "TOO LARGE! " << stone << endl;
add_to_map(x1, stone * 2024, count);
}
}
return x1;
}
map<ll,ll> read_input() {
map<ll, ll> state;
ll stone;
DEBUG && cout << "Reading input" << endl;
while (cin >> stone) {
DEBUG && cout << " Stone: " << stone << endl;
add_to_map(state, stone, 1);
}
DEBUG && cout << "Input read" << endl;
return state;
}
ll sum_over(map<ll,ll> x) {
ll sum = 0;
for (auto [stone, count] : x) {
sum += count;
}
return sum;
}
int main () {
map<ll,ll> state = read_input();
for (int i = 0; i < 25; i++) {
state = step(state);
}
cout << "Part 1: " << sum_over(state) << endl;
for (int i = 0; i < 50; i++) {
state = step(state);
DEBUG && cout << "Step: " << i+25 << endl;
for (auto [stone, count] : state) {
DEBUG && cout << " Stone: " << stone << " Count: " << count << endl;
}
}
cout << "Part 2: " << sum_over(state) << endl;
return 0;
}
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
const bool DEBUG = true;
#define coord pair<int, int>
map <char, vector<coord>> beacons;
int nrows = 0;
int ncols = 0;
void parseInput() {
string line;
int row = 0;
while (getline(cin, line)) {
nrows++;
ncols = line.size();
for (int col = 0 ; col < line.size(); col++) {
if (line[col] != '.') {
beacons[line[col]].push_back({row, col});
}
}
row++;
}
}
bool isInBounds(coord a) {
return a.first >= 0 && a.first < nrows && a.second >= 0 && a.second < ncols;
}
pair<coord, coord> antinodePositions(coord a, coord b) {
coord newA = {a.first - (b.first - a.first), a.second - (b.second - a.second)};
coord newB = {b.first + (b.first - a.first), b.second + (b.second - a.second)};
return {newA, newB};
}
vector<coord> resonanceNodes(coord a, coord b) {
vector<coord> resNodes;
int dx = b.first - a.first;
int dy = b.second - a.second;
// Check modulus?
while (dx % 3 == 0 && dy % 3 == 0) {
dx = dx/3;
dy = dy/3;
cout << "Found modulus 3!" << endl;
}
while (dx % 2 == 0 && dy % 2 == 0) {
dx = dx/2;
dy = dy/2;
cout << "Found modulus 2!" << endl;
}
while (dx % 5 == 0 && dy % 5 == 0) {
dx = dx/5;
dy = dy/5;
cout << "Found modulus 5!" << endl;
}
coord newNode = a;
while (isInBounds(newNode)) {
resNodes.push_back(newNode);
newNode = {newNode.first - dx, newNode.second - dy};
}
newNode = {a.first + dx, a.second + dy};
while (isInBounds(newNode)) {
resNodes.push_back(newNode);
newNode = {newNode.first + dx, newNode.second + dy};
}
return resNodes;
}
int countAntinodes() {
int nantinodes = 0;
array<array<bool, 100>, 100> hasAntiNode = {false};
for (auto [key, freqbeacons] : beacons) {
for (int i = 0; i < freqbeacons.size(); i++) {
for (int j = i + 1; j < freqbeacons.size(); j++) {
auto [a, b] = antinodePositions(freqbeacons[i], freqbeacons[j]);
DEBUG && cout << "Beacons (" << freqbeacons[i].first << ", " << freqbeacons[i].second << ") and (" << freqbeacons[j].first << ", " << freqbeacons[j].second << ")" << endl;
if (isInBounds(a) && !hasAntiNode[a.first][a.second]) {
nantinodes++;
hasAntiNode[a.first][a.second] = true;
DEBUG && cout << "Antinode at " << a.first << " " << a.second << endl;
}
if (isInBounds(b) && !hasAntiNode[b.first][b.second]) {
nantinodes++;
hasAntiNode[b.first][b.second] = true;
DEBUG && cout << "Antinode at " << a.first << " " << a.second << endl;
}
}
}
}
return nantinodes;
}
bool printNodes(const array<array<bool, 100>, 100> &hasAntiNode) {
vector<string> grid;
for (int row = 0 ; row < nrows ; row++) {
grid.push_back("");
for (int col = 0; col < ncols; col++) {
if (hasAntiNode[row][col]) {
grid[row] += "#";
} else {
grid[row] += ".";
}
}
}
for (auto [key, freqbeacons] : beacons) {
for (auto beacon : freqbeacons) {
grid[beacon.first][beacon.second] = key;
}
}
for (auto row : grid) {
cout << row << endl;
}
return false;
}
int countFrequencyNodes() {
int nantinodes = 0;
array<array<bool, 100>, 100> hasAntiNode = {false};
for (auto [key, freqbeacons] : beacons) {
for (int i = 0; i < freqbeacons.size(); i++) {
for (int j = i + 1; j < freqbeacons.size(); j++) {
DEBUG && cout << key << "-beacons (" << freqbeacons[i].first << ", " << freqbeacons[i].second << ") and (" << freqbeacons[j].first << ", " << freqbeacons[j].second << ")" << endl;
DEBUG && cout << "Resonance nodes at ";
for (auto resNode : resonanceNodes(freqbeacons[i], freqbeacons[j])) {
DEBUG && cout << "(" << resNode.first << ", " << resNode.second << "), ";
if (!hasAntiNode[resNode.first][resNode.second]) {
nantinodes++;
hasAntiNode[resNode.first][resNode.second] = true;
}
}
DEBUG && cout << endl;
}
}
}
DEBUG && printNodes(hasAntiNode);
return nantinodes;
}
bool printBeacons() {
for (auto [key, freqbeacons] : beacons) {
cout << key << ": ";
for (auto beacon : freqbeacons) {
cout << "(" << beacon.first << ", " << beacon.second << ") ";
}
cout << endl;
}
return false;
}
int main () {
parseInput();
DEBUG && printBeacons();
cout << "Part 1: " << countAntinodes() << endl;
cout << "Part 2: " << countFrequencyNodes() << endl;
DEBUG && cout << "nrow: " << nrows << " ncols: " << ncols << endl;
return 0;
}
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
const bool DEBUG = false;
deque<long long> input;
deque<long long> fwdOriginal;
void readInput() {
char c;
long long i = 0;
bool iseven = true;
while (cin >> c) {
input.push_back(c-'0');
if (iseven){
for (int j=0; j<c-'0'; j++) fwdOriginal.push_back(i);
}
if (iseven) i++;
iseven = !iseven;
}
}
long long checkSum() {
deque<long long> fwd = fwdOriginal;
long long dig = 0;
long long hole = 0;
long long sum = 0;
long long pos = 0;
while (input.size() > 0) {
dig = input.front();
input.pop_front();
DEBUG && cout << "DIGIT " << dig << endl;
for (int i=0; i<dig; i++) {
long long val = fwd.front();
sum += pos * val;
DEBUG && cout << "Added " << val << " * " << pos << endl;
fwd.pop_front();
if (fwd.size() == 0) return sum;
pos++;
}
if (input.size() < 1) return sum;
hole = input.front();
input.pop_front();
DEBUG && cout << "HOLE" << endl;
for (int i=0; i<hole; i++) {
long long val = fwd.back();
sum += pos * val;
DEBUG && cout << "Added " << val << " * " << pos << endl;
fwd.pop_back();
if (fwd.size() == 0) return sum;
pos++;
}
}
return sum;
}
int main () {
readInput();
long long part1 = checkSum();
cout << "Part 1: " << part1 << endl;
return 0;
}
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
const bool DEBUG = true;
#define ll long long
vector <ll> fileid;
vector <ll> filesize;
vector <ll> filestart;
vector <ll> holestart;
vector <ll> holesize;
void readInput() {
char c;
ll i = 0;
bool iseven = true;
ll pos = 0;
while (cin >> c) {
if (iseven) {
fileid.push_back(i);
filesize.push_back(c-'0');
filestart.push_back(pos);
i++;
pos += c-'0';
} else {
holestart.push_back(pos);
holesize.push_back(c-'0');
pos += c-'0';
}
iseven = !iseven;
}
}
void rearrange() {
DEBUG && cout << "Rearranging files" << endl;
for (int file = filesize.size()-1; file >= 0; file--) {
for (int hole = 0; hole < file; hole++) {
if (holesize[hole] >= filesize[file]) {
// insert file into hole
DEBUG && cout << "Inserting file " << fileid[file] << " into hole pos " << holestart[hole] << endl;
holesize[hole] -= filesize[file];
filestart[file] = holestart[hole];
holestart[hole] += filesize[file];
break;
}
}
}
}
ll fileValue(int file) {
ll val = 0;
for (ll i = 0; i < filesize[file]; i++) {
val += filestart[file] + i;
}
return val * fileid[file];
}
ll checkSum() {
ll sum = 0;
for (int file = 0; file < fileid.size(); file++) {
sum += fileValue(file);
}
return sum;
}
int main () {
readInput();
rearrange();
long long part2 = checkSum();
cout << "Part 2: " << part2 << endl;
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment