Day 4 was MUCH easier than REGEX DAY 3.
This commit is contained in:
169
2024/4/main.cpp
Normal file
169
2024/4/main.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
int main()
|
||||
{
|
||||
const std::string filename = "data.txt";
|
||||
std::ifstream ifs(filename);
|
||||
if(!ifs.is_open())
|
||||
{
|
||||
std::cerr << "Missing " << filename << "." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned long total = 0;
|
||||
unsigned long total_pt2 = 0;
|
||||
|
||||
std::vector<std::vector<char>> game;
|
||||
|
||||
for(std::string line; std::getline(ifs, line); )
|
||||
{
|
||||
if(line == "")
|
||||
continue;
|
||||
|
||||
// Add new row to fill out:
|
||||
game.push_back({});
|
||||
|
||||
// for each value in the line:
|
||||
for(const char &c : line)
|
||||
{
|
||||
// Fill out the row:
|
||||
game.back().push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
// DEBUG:
|
||||
// for(auto row : game)
|
||||
// {
|
||||
// for(auto c : row)
|
||||
// std::cout << c;
|
||||
// std::cout << std::endl;
|
||||
// }
|
||||
|
||||
for(int y = 0; y < game.size(); ++y)
|
||||
{
|
||||
for(int x = 0; x < game[y].size(); ++x)
|
||||
{
|
||||
// right:
|
||||
if(x + 3 < game[y].size())
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y][x+1] == 'M' &&
|
||||
game[y][x+2] == 'A' &&
|
||||
game[y][x+3] == 'S')
|
||||
++total;
|
||||
}
|
||||
|
||||
// down right:
|
||||
if(x + 3 < game[y].size() &&
|
||||
y + 3 < game.size())
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y+1][x+1] == 'M' &&
|
||||
game[y+2][x+2] == 'A' &&
|
||||
game[y+3][x+3] == 'S')
|
||||
++total;
|
||||
}
|
||||
|
||||
// down:
|
||||
if(y + 3 < game.size())
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y+1][x] == 'M' &&
|
||||
game[y+2][x] == 'A' &&
|
||||
game[y+3][x] == 'S')
|
||||
++total;
|
||||
}
|
||||
|
||||
// down left:
|
||||
if(x - 3 >= 0 &&
|
||||
y + 3 < game.size())
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y+1][x-1] == 'M' &&
|
||||
game[y+2][x-2] == 'A' &&
|
||||
game[y+3][x-3] == 'S')
|
||||
++total;
|
||||
}
|
||||
|
||||
// left:
|
||||
if(x - 3 >= 0)
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y][x-1] == 'M' &&
|
||||
game[y][x-2] == 'A' &&
|
||||
game[y][x-3] == 'S')
|
||||
++total;
|
||||
}
|
||||
|
||||
// up left:
|
||||
if(x - 3 >= 0 &&
|
||||
y - 3 >= 0)
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y-1][x-1] == 'M' &&
|
||||
game[y-2][x-2] == 'A' &&
|
||||
game[y-3][x-3] == 'S')
|
||||
++total;
|
||||
}
|
||||
|
||||
// up:
|
||||
if(y - 3 >= 0)
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y-1][x] == 'M' &&
|
||||
game[y-2][x] == 'A' &&
|
||||
game[y-3][x] == 'S')
|
||||
++total;
|
||||
}
|
||||
|
||||
// up right:
|
||||
if(x + 3 < game[y].size() &&
|
||||
y - 3 >= 0)
|
||||
{
|
||||
if(game[y][x] == 'X' &&
|
||||
game[y-1][x+1] == 'M' &&
|
||||
game[y-2][x+2] == 'A' &&
|
||||
game[y-3][x+3] == 'S')
|
||||
++total;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int y = 1; y < game.size() - 1; ++y)
|
||||
{
|
||||
for(int x = 1; x < game[y].size() - 1; ++x)
|
||||
{
|
||||
if(game[y][x] != 'A')
|
||||
continue;
|
||||
|
||||
int good = 0;
|
||||
// top left to bottom right
|
||||
if((game[y-1][x-1] == 'M' &&
|
||||
game[y+1][x+1] == 'S')
|
||||
||
|
||||
(game[y-1][x-1] == 'S' &&
|
||||
game[y+1][x+1] == 'M'))
|
||||
++good;
|
||||
// top right to bottom left
|
||||
if((game[y-1][x+1] == 'M' &&
|
||||
game[y+1][x-1] == 'S')
|
||||
||
|
||||
(game[y-1][x+1] == 'S' &&
|
||||
game[y+1][x-1] == 'M'))
|
||||
++good;
|
||||
|
||||
if(good == 2)
|
||||
++total_pt2;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << " Total: " << total << std::endl;
|
||||
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user