diff --git a/day3/src/main.rs b/day3/src/main.rs index 0c722ed..4519a74 100644 --- a/day3/src/main.rs +++ b/day3/src/main.rs @@ -4,21 +4,17 @@ use std::collections::HashSet; fn main() { let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap(); + let mut lines = input.lines(); + let mut next_line; let mut sum: u64 = 0; - for line in input.lines() { - let mut left_chars: HashSet = HashSet::new(); - - for (i, c) in line.char_indices() { - if i < line.len()/2 { - left_chars.insert(c); - } else if left_chars.contains(&c) { - if c >= 'a' && c <= 'z' { - sum += (c as u64) - ('a' as u64) + 1; - } else { - sum += (c as u64) - ('A' as u64) + 27; - } - break; + while (next_line = lines.next()) == () && !next_line.is_none() { + let line1: HashSet = next_line.unwrap().chars().collect(); + let line2: HashSet = lines.next().unwrap().chars().collect(); + 'chars: for c in lines.next().unwrap().chars() { + if line1.contains(&c) && line2.contains(&c) { + sum += value_of(c); + break 'chars; } } } @@ -26,3 +22,10 @@ fn main() { println!("{:?}", sum); } +fn value_of(c: char) -> u64 { + if c >= 'a' && c <= 'z' { + (c as u64) - ('a' as u64) + 1 + } else { + (c as u64) - ('A' as u64) + 27 + } +} diff --git a/day3/src/main1.rs b/day3/src/main1.rs new file mode 100644 index 0000000..0c722ed --- /dev/null +++ b/day3/src/main1.rs @@ -0,0 +1,28 @@ +use std::env; +use std::fs; +use std::collections::HashSet; + +fn main() { + let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap(); + + let mut sum: u64 = 0; + for line in input.lines() { + let mut left_chars: HashSet = HashSet::new(); + + for (i, c) in line.char_indices() { + if i < line.len()/2 { + left_chars.insert(c); + } else if left_chars.contains(&c) { + if c >= 'a' && c <= 'z' { + sum += (c as u64) - ('a' as u64) + 1; + } else { + sum += (c as u64) - ('A' as u64) + 27; + } + break; + } + } + } + + println!("{:?}", sum); +} +