2022 day 1. Learning rust!

This commit is contained in:
Anna Rose Wiggins 2024-02-14 18:44:02 +00:00
parent 849fc74a15
commit 4bda83476c
5 changed files with 2294 additions and 0 deletions

26
2022/day01/src/main.rs Normal file
View file

@ -0,0 +1,26 @@
use std::io;
use std::io::prelude::*;
use std::fs::File;
fn read_lines(filename: &str) -> io::Result<io::Lines<io::BufReader<File>>> {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn main() {
let mut max = 0;
let mut current = 0;
for line in read_lines("input.txt").unwrap() {
let text = line.unwrap();
current = match text.parse::<i32>() {
Ok(value) => current + value,
Err(_) => {
if current > max { max = current; }
0
},
};
}
println!("{}", max);
}