day 1 done
This commit is contained in:
parent
b59c855a30
commit
c22b4f753d
49
01/main.rs
49
01/main.rs
@ -22,10 +22,55 @@ fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
// note that this discards a final newline
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
||||
fn part_one() {}
|
||||
fn part_one() {
|
||||
if let Ok(lines) = read_lines("./inputs/input") {
|
||||
let mut biggest = 0;
|
||||
let mut accumulator = 0;
|
||||
for line in lines {
|
||||
if let Ok(calories) = line {
|
||||
if calories != "" {
|
||||
accumulator += calories.parse::<u32>().unwrap();
|
||||
} else {
|
||||
if accumulator > biggest {
|
||||
biggest = accumulator
|
||||
}
|
||||
accumulator = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// catching final elf
|
||||
if accumulator > biggest {
|
||||
biggest = accumulator
|
||||
}
|
||||
println!("{}", biggest);
|
||||
}
|
||||
}
|
||||
|
||||
fn part_two() {}
|
||||
fn part_two() {
|
||||
if let Ok(lines) = read_lines("./inputs/input") {
|
||||
let mut elves = Vec::new();
|
||||
let mut accumulator = 0;
|
||||
for line in lines {
|
||||
if let Ok(calories) = line {
|
||||
if calories != "" {
|
||||
accumulator += calories.parse::<u32>().unwrap();
|
||||
} else {
|
||||
elves.push(accumulator);
|
||||
accumulator = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// catching final elf
|
||||
elves.push(accumulator);
|
||||
elves.sort_unstable();
|
||||
println!(
|
||||
"{}",
|
||||
elves.pop().unwrap() + elves.pop().unwrap() + elves.pop().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
// note that this discards a final newline
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user