day 2
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream file ("./1i.txt");
|
||||
std::ifstream file ("./1.txt");
|
||||
std::vector<int> input;
|
||||
std::string line;
|
||||
|
||||
|
||||
79
2020/2.cpp
Normal file
79
2020/2.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> split(const std::string& str, const std::string& delim)
|
||||
{
|
||||
std::vector<std::string> tokens;
|
||||
size_t prev = 0, pos = 0;
|
||||
do
|
||||
{
|
||||
pos = str.find(delim, prev);
|
||||
if (pos == std::string::npos) pos = str.length();
|
||||
std::string token = str.substr(prev, pos-prev);
|
||||
if (!token.empty()) tokens.push_back(token);
|
||||
prev = pos + delim.length();
|
||||
}
|
||||
while (pos < str.length() && prev < str.length());
|
||||
return tokens;
|
||||
}
|
||||
|
||||
std::vector<std::string> readFile(std::string path)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
std::vector<std::string> input;
|
||||
std::string line;
|
||||
|
||||
while(std::getline(file, line))
|
||||
{
|
||||
input.push_back(line);
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
std::vector<std::string> input = readFile("./2.txt");
|
||||
|
||||
int total = 0;
|
||||
for (auto& line : input)
|
||||
{
|
||||
std::vector<std::string> parts = split(line, " ");
|
||||
|
||||
int lower, upper;
|
||||
std::vector<std::string> bounds = split(parts[0], "-");
|
||||
lower = std::stoi(bounds[0]);
|
||||
upper = std::stoi(bounds[1]);
|
||||
|
||||
char searchterm = parts[1][0];
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < parts[2].length(); i++)
|
||||
if (parts[2][i] == searchterm) count++;
|
||||
|
||||
if (count >= lower && count <= upper) total++;
|
||||
}
|
||||
|
||||
std::cout << "Part 1, valid passwords: " << total << std::endl;
|
||||
|
||||
total = 0;
|
||||
for (auto& line : input)
|
||||
{
|
||||
std::vector<std::string> parts = split(line, " ");
|
||||
|
||||
int first, second;
|
||||
std::vector<std::string> bounds = split(parts[0], "-");
|
||||
first = std::stoi(bounds[0]) - 1;
|
||||
second = std::stoi(bounds[1]) - 1;
|
||||
|
||||
char searchterm = parts[1][0];
|
||||
|
||||
// A terrible logical XOR
|
||||
if (!(parts[2][first] == searchterm) != !(parts[2][second] == searchterm)) total++;
|
||||
}
|
||||
|
||||
std::cout << "Part 2, valid passwords: " << total << std::endl;
|
||||
|
||||
}
|
||||
1000
2020/2.txt
Normal file
1000
2020/2.txt
Normal file
File diff suppressed because it is too large
Load Diff
76
2020/adventofcode.hpp
Normal file
76
2020/adventofcode.hpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#ifndef AOC_H_
|
||||
#define AOC_H_
|
||||
|
||||
int day1part1();
|
||||
int day1part2();
|
||||
|
||||
int day2part1();
|
||||
int day2part2();
|
||||
|
||||
int day3part1();
|
||||
int day3part2();
|
||||
|
||||
int day4part1();
|
||||
int day4part2();
|
||||
|
||||
int day5part1();
|
||||
int day5part2();
|
||||
|
||||
int day6part1();
|
||||
int day6part2();
|
||||
|
||||
int day7part1();
|
||||
int day7part2();
|
||||
|
||||
int day8part1();
|
||||
int day8part2();
|
||||
|
||||
int day9part1();
|
||||
int day9part2();
|
||||
|
||||
int day10part1();
|
||||
int day10part2();
|
||||
|
||||
int day11part1();
|
||||
int day11part2();
|
||||
|
||||
int day12part1();
|
||||
int day12part2();
|
||||
|
||||
int day13part1();
|
||||
int day13part2();
|
||||
|
||||
int day14part1();
|
||||
int day14part2();
|
||||
|
||||
int day15part1();
|
||||
int day15part2();
|
||||
|
||||
int day16part1();
|
||||
int day16part2();
|
||||
|
||||
int day17part1();
|
||||
int day17part2();
|
||||
|
||||
int day18part1();
|
||||
int day18part2();
|
||||
|
||||
int day19part1();
|
||||
int day19part2();
|
||||
|
||||
int day20part1();
|
||||
int day20part2();
|
||||
|
||||
int day21part1();
|
||||
int day21part2();
|
||||
|
||||
int day22part1();
|
||||
int day22part2();
|
||||
|
||||
int day23part1();
|
||||
int day23part2();
|
||||
|
||||
int day24part1();
|
||||
int day24part2();
|
||||
|
||||
#endif
|
||||
0
2020/helpers.cpp
Normal file
0
2020/helpers.cpp
Normal file
0
2020/main.cpp
Normal file
0
2020/main.cpp
Normal file
Reference in New Issue
Block a user