d3p2
This commit is contained in:
parent
e802d8dd91
commit
72c1ab9f9e
@ -4,21 +4,17 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap();
|
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;
|
let mut sum: u64 = 0;
|
||||||
for line in input.lines() {
|
while (next_line = lines.next()) == () && !next_line.is_none() {
|
||||||
let mut left_chars: HashSet<char> = HashSet::new();
|
let line1: HashSet<char> = next_line.unwrap().chars().collect();
|
||||||
|
let line2: HashSet<char> = lines.next().unwrap().chars().collect();
|
||||||
for (i, c) in line.char_indices() {
|
'chars: for c in lines.next().unwrap().chars() {
|
||||||
if i < line.len()/2 {
|
if line1.contains(&c) && line2.contains(&c) {
|
||||||
left_chars.insert(c);
|
sum += value_of(c);
|
||||||
} else if left_chars.contains(&c) {
|
break 'chars;
|
||||||
if c >= 'a' && c <= 'z' {
|
|
||||||
sum += (c as u64) - ('a' as u64) + 1;
|
|
||||||
} else {
|
|
||||||
sum += (c as u64) - ('A' as u64) + 27;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,3 +22,10 @@ fn main() {
|
|||||||
println!("{:?}", sum);
|
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
28
day3/src/main1.rs
Normal 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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user