2022 begins.

This commit is contained in:
David Vereb
2022-12-01 09:14:56 -05:00
parent 7717713bde
commit 79a6123526
11 changed files with 2667 additions and 1 deletions

2238
2022/1/data.txt Normal file

File diff suppressed because it is too large Load Diff

57
2022/1/main.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
int main()
{
std::ifstream ifs("data.txt");
if(!ifs.is_open())
{
std::cerr << "Couldn't open file." << std::endl;
return -1;
}
static bool last_line_blank = false;
std::vector<long> numbers;
long total = 0;
long most = 0;
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
{
if(!last_line_blank)
{
numbers.push_back(total);
if(total > most)
most = total;
total = 0;
}
last_line_blank = true;
}
else
{
last_line_blank = false;
total += std::atoi(line.c_str());
}
}
std::cout << "Most: " << most << std::endl;
std::sort(numbers.begin(), numbers.end());
if(numbers.size())
{
int three = 3;
long total_total = 0;
for(int i = numbers.size() - 1; i >= 0; i--)
{
if(three-- > 0)
total_total += numbers[i];
}
std::cout << "Top 3: " << total_total << std::endl;
}
return 0;
}

38
2022/1/main.rs Normal file
View File

@@ -0,0 +1,38 @@
use std::fs;
fn main()
{
let contents = fs::read_to_string("data.txt")
.expect("Could not find data.txt.");
let mut numbers = Vec::new();
let mut total = 0;
let mut max = 0;
for line in contents.split('\n')
{
if line == ""
{
if total > max
{
max = total;
}
numbers.push(total);
total = 0;
}
else
{
let i = line.parse::<i32>().unwrap();
total += i;
}
}
println!("Max: {max}");
numbers.sort();
let mut top_three = 0;
for amt in &numbers[numbers.len() - 3..numbers.len()]
{
top_three += amt;
}
println!("Top Three: {top_three}");
}