Added some missing old files and new day2 of 2024.
This commit is contained in:
parent
450b9c790b
commit
f7083ef726
@ -53,3 +53,4 @@ Monkey 7:
|
||||
Test: divisible by 11
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 2
|
||||
|
||||
|
@ -4,6 +4,40 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum Operation {
|
||||
ADD,
|
||||
MULTIPLY,
|
||||
};
|
||||
|
||||
class Monkey {
|
||||
public:
|
||||
Monkey(const std::vector<int> &starting_items, Operation operation_type, int operation_amount,
|
||||
int division_by, int true_monkey, int false_monkey)
|
||||
: items(starting_items), operation_type(operation_type), operation_amount(operation_amount),
|
||||
division_by(division_by), true_monkey(true_monkey), false_monkey(false_monkey)
|
||||
{
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream &out, const Monkey &monkey);
|
||||
|
||||
private:
|
||||
std::vector<int> items;
|
||||
|
||||
Operation operation_type;
|
||||
int operation_amount;
|
||||
|
||||
int division_by;
|
||||
|
||||
int true_monkey;
|
||||
int false_monkey;
|
||||
};
|
||||
|
||||
std::vector<int> ParseItems(const std::string &line);
|
||||
std::pair<Operation, int> ParseOperation(const std::string &line);
|
||||
int ParseDivisibleBy(const std::string &line);
|
||||
int ParseTrue(const std::string &line);
|
||||
int ParseFalse(const std::string &line);
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream ifs("data.txt");
|
||||
@ -16,14 +50,65 @@ int main()
|
||||
unsigned long total = 0;
|
||||
unsigned long total_pt2 = 0;
|
||||
|
||||
std::vector<Monkey> monkeys;
|
||||
int line_num = 0;
|
||||
std::vector<std::string> buf;
|
||||
for(std::string line; std::getline(ifs, line); )
|
||||
{
|
||||
if(line == "")
|
||||
{
|
||||
if(buf.size() == 5)
|
||||
{
|
||||
auto op = ParseOperation(buf[2]);
|
||||
monkeys.push_back(Monkey(//std::atoi(buf[0].substr(7, buf[0].size() - 7).c_str()),
|
||||
ParseItems(buf[1]),
|
||||
op.first,
|
||||
op.second,
|
||||
ParseDivisibleBy(buf[3]),
|
||||
ParseTrue(buf[4]),
|
||||
ParseFalse(buf[5])));
|
||||
buf.clear();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else if(line.starts_with(" "))
|
||||
buf.push_back(line);
|
||||
}
|
||||
|
||||
for(const auto &monkey : monkeys)
|
||||
std::cout << monkey << std::endl;
|
||||
|
||||
std::cout << " Total: " << total << std::endl;
|
||||
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const Monkey &monkey)
|
||||
{
|
||||
out << " Starting items: ";
|
||||
for(auto item : monkey.items)
|
||||
out << item << ", ";
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<int> ParseItems(const std::string &line)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
std::pair<Operation, int> ParseOperation(const std::string &line)
|
||||
{
|
||||
return { Operation::ADD, 0 };
|
||||
}
|
||||
int ParseDivisibleBy(const std::string &line)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
int ParseTrue(const std::string &line)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
int ParseFalse(const std::string &line)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
2
2023/1/Makefile
Normal file
2
2023/1/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
a.out: main.cpp
|
||||
clang++ -g -std=c++20 main.cpp
|
1000
2023/1/input.txt
Normal file
1000
2023/1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
2023/1/input2.txt
Normal file
7
2023/1/input2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
134
2023/1/main.cpp
Normal file
134
2023/1/main.cpp
Normal file
@ -0,0 +1,134 @@
|
||||
#include <cctype>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#define INPUT_FILE "input2.txt"
|
||||
|
||||
int ParseNumLine1(const std::string &line);
|
||||
int ParseNumLine2(const std::string &line);
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream ifs(INPUT_FILE);
|
||||
if(!ifs.is_open())
|
||||
{
|
||||
std::cerr << "Couldn't open " INPUT_FILE "." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
long result1 = 0;
|
||||
long result2 = 0;
|
||||
for(std::string line = ""; std::getline(ifs, line); )
|
||||
{
|
||||
result1 += ParseNumLine1(line);
|
||||
result2 += ParseNumLine2(line);
|
||||
}
|
||||
|
||||
std::cout << "Part 1 Result: " << result1 << std::endl;
|
||||
std::cout << "Part 2 Result: " << result2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ParseNumLine1(const std::string &line)
|
||||
{
|
||||
char found[3] = { '\0', '\0', '\0' };
|
||||
bool found_first = false;
|
||||
for(auto &ch : line)
|
||||
{
|
||||
if(std::isdigit(ch))
|
||||
{
|
||||
if(!found_first)
|
||||
{
|
||||
found[0] = ch;
|
||||
found_first = true;
|
||||
}
|
||||
|
||||
// same as first, if only one
|
||||
found[1] = ch;
|
||||
}
|
||||
}
|
||||
return std::atoi(found);
|
||||
}
|
||||
|
||||
int ParseNumLine2(const std::string &line)
|
||||
{
|
||||
char found[3] = { '\0', '\0', '\0' };
|
||||
bool found_first = false;
|
||||
for(auto &ch : line)
|
||||
{
|
||||
if(std::isdigit(ch))
|
||||
{
|
||||
if(!found_first)
|
||||
{
|
||||
found[0] = ch;
|
||||
found_first = true;
|
||||
}
|
||||
|
||||
// same as first, if only one
|
||||
found[1] = ch;
|
||||
}
|
||||
else
|
||||
{
|
||||
const static std::vector<std::string> available = {
|
||||
"one",
|
||||
"two",
|
||||
"three",
|
||||
"four",
|
||||
"five",
|
||||
"six",
|
||||
"seven",
|
||||
"eight",
|
||||
"nine",
|
||||
};
|
||||
|
||||
int num = 0;
|
||||
for(auto i = 0; i < line.size(); ++i)
|
||||
{
|
||||
for(auto w = 0; w < available.size(); ++w)
|
||||
{
|
||||
const std::string &word = available[w];
|
||||
if(line.size() - i < word.size())
|
||||
continue; // not enough letters for this word
|
||||
|
||||
bool word_matches = true;
|
||||
for(auto letter = 0; i + letter < line.size() && word_matches; ++letter)
|
||||
{
|
||||
if(line[i + letter] != word[letter])
|
||||
{
|
||||
word_matches = false;
|
||||
break;
|
||||
}
|
||||
if(letter == word.size() - 1)
|
||||
{
|
||||
num = w + 1;
|
||||
// std::cout << "Found: ";
|
||||
// for(auto a = 0; a <= letter; ++a)
|
||||
// std::cout << line[i + a];
|
||||
// std::cout << " == " << available[w] << " @ " << letter << std::endl;
|
||||
break; // true!
|
||||
}
|
||||
}
|
||||
if(word_matches)
|
||||
break;
|
||||
}
|
||||
|
||||
if(num)
|
||||
{
|
||||
if(!found_first)
|
||||
{
|
||||
found[0] = num + '0';
|
||||
found_first = true;
|
||||
}
|
||||
|
||||
found[1] = num + '0';
|
||||
|
||||
num = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << "Return: " << found << " == " << std::atoi(found) << std::endl;
|
||||
return std::atoi(found);
|
||||
}
|
4
2023/1/sample.txt
Normal file
4
2023/1/sample.txt
Normal file
@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
2
2024/2/Makefile
Normal file
2
2024/2/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
a.out: main_pt2.cpp
|
||||
clang++ -std=c++2b -g -O0 main_pt2.cpp
|
1000
2024/2/data.txt
Normal file
1000
2024/2/data.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2024/2/data_test.txt
Normal file
6
2024/2/data_test.txt
Normal file
@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
83
2024/2/main.cpp
Normal file
83
2024/2/main.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
|
||||
std::istringstream input(line);
|
||||
long *last = nullptr;
|
||||
bool *increasing = nullptr;
|
||||
bool safe = true;
|
||||
for(std::string value; std::getline(input, value, ' '); )
|
||||
{
|
||||
long current;
|
||||
// Get the value:
|
||||
try {
|
||||
current = std::atoi(value.c_str());
|
||||
} catch (const std::exception &e) {
|
||||
break;
|
||||
}
|
||||
|
||||
// See if it's the first value:
|
||||
if(!last)
|
||||
{
|
||||
last = new long(current);
|
||||
continue;
|
||||
}
|
||||
|
||||
// See if it's the second value:
|
||||
if(!increasing)
|
||||
{
|
||||
increasing = new bool(current > *last);
|
||||
}
|
||||
|
||||
auto diff = current - *last;
|
||||
if(*increasing &&
|
||||
(diff < 1 || diff > 3))
|
||||
{
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
if(!*increasing &&
|
||||
(diff < -3 || diff > -1))
|
||||
{
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(last)
|
||||
*last = current;
|
||||
}
|
||||
if(increasing && last // actually made it through at least 2 values
|
||||
&& safe)
|
||||
total++;
|
||||
|
||||
if(increasing)
|
||||
delete increasing, increasing = nullptr;
|
||||
if(last)
|
||||
delete last, last = nullptr;
|
||||
}
|
||||
|
||||
std::cout << " Total: " << total << std::endl;
|
||||
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
55
2024/2/main_pt2.cpp
Normal file
55
2024/2/main_pt2.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
|
||||
std::vector<std::vector<long>> data;
|
||||
|
||||
for(std::string line; std::getline(ifs, line); )
|
||||
{
|
||||
if(line == "")
|
||||
continue;
|
||||
|
||||
// add a row for this line:
|
||||
data.push_back({});
|
||||
|
||||
std::istringstream input(line);
|
||||
// for each value in the line:
|
||||
for(std::string value; std::getline(input, value, ' '); )
|
||||
{
|
||||
try {
|
||||
// add it to the row
|
||||
data.back().push_back(std::atoi(value.c_str()));
|
||||
} catch (const std::exception &e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DEBUG:
|
||||
for(auto row : data)
|
||||
{
|
||||
for(auto col : row)
|
||||
std::cout << col << ", ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
std::cout << " Total: " << total << std::endl;
|
||||
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user