2022 begins.
This commit is contained in:
2238
2022/1/data.txt
Normal file
2238
2022/1/data.txt
Normal file
File diff suppressed because it is too large
Load Diff
57
2022/1/main.cpp
Normal file
57
2022/1/main.cpp
Normal 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
38
2022/1/main.rs
Normal 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}");
|
||||
}
|
Reference in New Issue
Block a user