Added solution to Day 5 Part 1.

This commit is contained in:
David Vereb 2024-12-05 09:51:02 -05:00
parent 8fb1a7d95c
commit 80555c4d9c
4 changed files with 1548 additions and 0 deletions

2
2024/5/Makefile Normal file
View File

@ -0,0 +1,2 @@
a.out: main.cpp
clang++ -std=c++2b -g -O0 main.cpp

1387
2024/5/data.txt Normal file

File diff suppressed because it is too large Load Diff

28
2024/5/data_test.txt Normal file
View File

@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

131
2024/5/main.cpp Normal file
View File

@ -0,0 +1,131 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#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::map<long, std::set<long>> rules;
std::vector<std::vector<long>> production;
bool reading_page_numbers = true;
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
{
// one blank line is the break between reading page numbers and records.
if(reading_page_numbers)
reading_page_numbers = false;
continue;
}
std::istringstream input(line);
if(reading_page_numbers)
{
// for each value in the line:
std::vector<long> numbers;
for(std::string value; std::getline(input, value, '|'); )
{
try {
numbers.push_back(std::atoi(value.c_str()));
} catch (const std::exception &e) {
std::cout << "Error parsing rule number from " << value << std::endl;
exit(-1);
}
}
if(numbers.size() != 2)
{
std::cout << "Error: Incorrect amount of page numbers (" << numbers.size()
<< ") in rules import sequence." << std::endl;
exit(-1);
}
rules[numbers[0]].insert(numbers[1]);
}
else
{
// for each value in the line:
production.push_back({});
for(std::string value; std::getline(input, value, ','); )
{
try {
production.back().push_back(std::atoi(value.c_str()));
} catch (const std::exception &e) {
std::cout << "Error parsing production number from " << value << std::endl;
exit(-1);
}
}
}
}
// // Debug:
// std::cout << "RULES:" << std::endl;
// for(auto rule : rules)
// {
// std::cout << rule.first << " prints before ";
// for(auto page : rule.second)
// std::cout << page << ",";
// std::cout << std::endl;
// }
// std::cout << std::endl;
// std::cout << "PRODUCTION:" << std::endl;
// for(auto row : production)
// {
// for(auto num : row)
// std::cout << num << ",";
// std::cout << std::endl;
// }
// For each production run:
for(auto row : production)
{
std::set<long> already_passed;
bool bad = false;
// For each page number in that production run:
for(auto num : row)
{
// Check against all page numbers it shouldn't print after
for(auto before : rules[num])
{
// If found, it's a bad row!
if(std::find(already_passed.begin(), already_passed.end(), before) != already_passed.end())
{
bad = true;
break;
}
}
// Keep track of numbers we've looked at already:
already_passed.insert(num);
}
if(!bad)
total += row[row.size() / 2];
// else
// std::cout << "not ";
// std::cout << "safe: ";
// for(auto num : row)
// std::cout << num << ",";
// std::cout << std::endl;
}
std::cout << " Total: " << total << std::endl;
std::cout << "PT2 Total: " << total_pt2 << std::endl;
return 0;
}