diff options
author | Guillermo Ramos | 2024-12-01 14:23:04 +0100 |
---|---|---|
committer | Guillermo Ramos | 2024-12-01 22:18:04 +0100 |
commit | 1bf1bcc0662d9f9631cedd5cd4842d4fe4915e9c (patch) | |
tree | d3999aba2fa4e0d39603ea1bec1919e821d9cec4 /2024/1/p1.rs | |
download | AoC-1bf1bcc0662d9f9631cedd5cd4842d4fe4915e9c.tar.gz |
2024.1
Diffstat (limited to '2024/1/p1.rs')
-rw-r--r-- | 2024/1/p1.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/2024/1/p1.rs b/2024/1/p1.rs new file mode 100644 index 0000000..4ffeb63 --- /dev/null +++ b/2024/1/p1.rs @@ -0,0 +1,23 @@ +const INPUT_FILE: &str = "input"; + +fn main() { + let input = std::fs::read_to_string(INPUT_FILE).expect("Reading input file"); + let mut left: Vec<u32> = vec![]; + let mut right: Vec<u32> = vec![]; + for line in input.lines() { + let mut fields = line.split_whitespace(); + let e = "Wrong file format"; + left.push(fields.next().expect(e).parse().expect(e)); + right.push(fields.next().expect(e).parse().expect(e)); + } + left.sort_unstable(); + right.sort_unstable(); + + let mut distance: u32 = 0; + for i in 0..left.len() { + let l = left[i]; + let r = right[i]; + distance += if l >= r { l - r } else { r - l }; + } + println!("Distance: {}", distance); +} |