Day 1 Part 1

This commit is contained in:
Thomas Muller 2024-12-01 00:24:52 -05:00
commit 07e84ae119
4 changed files with 1042 additions and 0 deletions

6
day_1/hint1.txt Normal file
View file

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

1000
day_1/input.txt Normal file

File diff suppressed because it is too large Load diff

BIN
day_1/part1 Executable file

Binary file not shown.

36
day_1/part1.rs Normal file
View file

@ -0,0 +1,36 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::env;
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn main() {
let mut left_list: Vec<i32> = vec![];
let mut right_list: Vec<i32> = vec![];
let args: Vec<_> = env::args().collect();
if let Ok(lines) = read_lines(args[1].clone()) {
for line in lines.flatten() {
let parts = line.split(" ").collect::<Vec<_>>();
let lnum = parts[0].parse::<i32>().unwrap();
let rnum = parts[1].parse::<i32>().unwrap();
left_list.push(lnum);
right_list.push(rnum);
}
left_list.sort();
right_list.sort();
let mut sum = 0;
for (i, left_n) in left_list.iter().enumerate() {
let right_n = right_list[i];
println!("{} {} {}", i, left_n, right_n);
sum += (left_n - right_n).abs();
}
println!("{}", sum);
}
}