1
0
Fork 0

Day 3 Completed.

This commit is contained in:
Yohan Boujon 2023-10-12 22:24:09 +02:00
parent 4077ebe37e
commit 02ec699023
2 changed files with 41 additions and 3 deletions

6
rust/day3/exemple Normal file
View file

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

View file

@ -8,10 +8,10 @@ fn convert(num: u8) -> u8 {
} }
} }
fn main() { fn first_part(str: &String) {
let binding = read_to_string("input").unwrap(); let file_content: Vec<&str> = str.split("\r\n").collect();
let file_content: Vec<&str> = binding.split("\r\n").collect();
let mut ruckstack: Vec<u64> = Vec::new(); let mut ruckstack: Vec<u64> = Vec::new();
for con in &file_content { for con in &file_content {
let splitted = con.split_at(con.len() / 2); let splitted = con.split_at(con.len() / 2);
let mut iter = splitted let mut iter = splitted
@ -23,3 +23,35 @@ fn main() {
} }
println!("Sum of the priorities : {}", ruckstack.iter().sum::<u64>()); println!("Sum of the priorities : {}", ruckstack.iter().sum::<u64>());
} }
fn second_part(str: &String) {
let file_content: Vec<&str> = str.split("\r\n").collect();
let mut ruckstack: Vec<u64> = Vec::new();
let mut badge: Vec<[&str; 3]> = Vec::new();
let mut j : i64 = -1;
for (i, con) in file_content.iter().enumerate() {
if i%3 == 0 {
badge.push(["","",""]);
j +=1;
}
badge[j as usize][i%3] = con;
}
for strs in &badge {
//let badge_second = strs[1].to_owned()+&strs[2].to_owned();
let iter : Vec<char> = strs[0]
.chars()
.into_iter()
.filter(|ch| strs[1].find(*ch).is_some())
.filter(|ch| strs[2].find(*ch).is_some())
.collect();
ruckstack.push(convert(iter[0] as u8) as u64);
}
println!("Sum of the badges : {}", ruckstack.iter().sum::<u64>());
}
fn main() {
let binding = read_to_string("input").unwrap();
first_part(&binding);
second_part(&binding);
}