Added day 4, part 1 and 2.
This commit is contained in:
		
							
								
								
									
										2
									
								
								2022/4/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								2022/4/Makefile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
a.out: main.cpp
 | 
			
		||||
	clang++ -std=c++2b -g -O0 main.cpp
 | 
			
		||||
							
								
								
									
										1000
									
								
								2022/4/data.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1000
									
								
								2022/4/data.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										79
									
								
								2022/4/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								2022/4/main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <set>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
std::vector<std::string> SplitStr(std::string str, const std::string &delim)
 | 
			
		||||
{
 | 
			
		||||
	std::vector<std::string> rtn;
 | 
			
		||||
 | 
			
		||||
	for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim))
 | 
			
		||||
	{
 | 
			
		||||
		rtn.push_back(str.substr(0, split));
 | 
			
		||||
		str = str.substr(split + delim.size()); // chop off beginning
 | 
			
		||||
	}
 | 
			
		||||
	if(str.size())
 | 
			
		||||
		rtn.push_back(str);
 | 
			
		||||
 | 
			
		||||
	return rtn;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
	std::ifstream ifs("data.txt");
 | 
			
		||||
	if(!ifs.is_open())
 | 
			
		||||
	{
 | 
			
		||||
		std::cerr << "Missing data.txt." << std::endl;
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	unsigned long total = 0;
 | 
			
		||||
	unsigned long total_pt2 = 0;
 | 
			
		||||
 | 
			
		||||
	for(std::string line; std::getline(ifs, line); )
 | 
			
		||||
	{
 | 
			
		||||
		if(line == "")
 | 
			
		||||
			continue;
 | 
			
		||||
 | 
			
		||||
		auto sections = SplitStr(line, ",");
 | 
			
		||||
		if(sections.size() != 2)
 | 
			
		||||
		{
 | 
			
		||||
			std::cerr << "Incorrect amount of data for line: " << line << std::endl;
 | 
			
		||||
			exit(-1);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		auto elf_a = SplitStr(sections[0], "-");
 | 
			
		||||
		auto elf_b = SplitStr(sections[1], "-");
 | 
			
		||||
 | 
			
		||||
		if(elf_a.size() != 2 || elf_b.size() != 2)
 | 
			
		||||
		{
 | 
			
		||||
			std::cerr << "Incorrect amount of data for line: " << line << std::endl;
 | 
			
		||||
			exit(-2);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		int elf_a_min = std::atoi(elf_a[0].c_str());
 | 
			
		||||
		int elf_a_max = std::atoi(elf_a[1].c_str());
 | 
			
		||||
		int elf_b_min = std::atoi(elf_b[0].c_str());
 | 
			
		||||
		int elf_b_max = std::atoi(elf_b[1].c_str());
 | 
			
		||||
 | 
			
		||||
		if((elf_a_min <= elf_b_min && elf_a_max >= elf_b_max) ||
 | 
			
		||||
		   (elf_b_min <= elf_a_min && elf_b_max >= elf_a_max))
 | 
			
		||||
			++total;
 | 
			
		||||
 | 
			
		||||
		// sort
 | 
			
		||||
		if(elf_a_min > elf_b_min)
 | 
			
		||||
		{
 | 
			
		||||
			std::swap(elf_a_min, elf_b_min);
 | 
			
		||||
			std::swap(elf_a_max, elf_b_max);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if(elf_b_min <= elf_a_max)
 | 
			
		||||
			++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