This commit is contained in:
Dory 2024-03-07 10:20:02 -08:00
parent e802d8dd91
commit 72c1ab9f9e
2 changed files with 44 additions and 13 deletions

View File

@ -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<char> = 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<char> = next_line.unwrap().chars().collect();
let line2: HashSet<char> = 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
}
}

28
day3/src/main1.rs Normal file
View File

@ -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<char> = 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);
}