first commit
This commit is contained in:
commit
cfe3798d17
42 changed files with 9617 additions and 0 deletions
64
2022/day3/src/main.rs
Executable file
64
2022/day3/src/main.rs
Executable file
|
@ -0,0 +1,64 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{Read, Result},
|
||||
};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut file = File::open("./data").expect("Data file not found");
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content)?;
|
||||
|
||||
// Part 1
|
||||
let rucksack: Vec<char> = content
|
||||
.split("\n")
|
||||
.map(|line| {
|
||||
let mid = line.len() / 2;
|
||||
let left = &line[..mid];
|
||||
let right = &line[mid..];
|
||||
let mut left_chars = left.chars();
|
||||
left_chars.find(|c| right.contains(c.to_owned())).unwrap()
|
||||
})
|
||||
.collect();
|
||||
|
||||
let weigth = vec![
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
|
||||
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
];
|
||||
let priority: Vec<usize> = rucksack
|
||||
.iter()
|
||||
.map(|c| weigth.iter().position(|x| x == c).unwrap())
|
||||
.collect();
|
||||
println!("{:?}", priority.iter().map(|x| x + 1).sum::<usize>());
|
||||
|
||||
// Part 2
|
||||
let mut rucksacks: Vec<&str> = content.split("\n").collect();
|
||||
let mut group = Vec::new();
|
||||
for _ in 0..rucksacks.len() / 3 {
|
||||
let one = rucksacks.pop().unwrap();
|
||||
let two = rucksacks.pop().unwrap();
|
||||
let three = rucksacks.pop().unwrap();
|
||||
group.push(vec![one, two, three])
|
||||
}
|
||||
let group: Vec<char> = group
|
||||
.iter()
|
||||
.map(|group| {
|
||||
let one = group[0];
|
||||
let two = group[1];
|
||||
let three = group[2];
|
||||
let one_chars = one.chars();
|
||||
let three_chars = three.chars();
|
||||
let one_two = one_chars.filter(|&c| two.contains(c)).collect::<Vec<char>>();
|
||||
let mut one_two_three =
|
||||
three_chars.filter(|&c| one_two.contains(&c)).collect::<Vec<char>>();
|
||||
one_two_three.pop().unwrap()
|
||||
//three_chars.find(|c|one_two.)
|
||||
})
|
||||
.collect();
|
||||
let priority: Vec<usize> = group
|
||||
.iter()
|
||||
.map(|c| weigth.iter().position(|x| x == c).unwrap())
|
||||
.collect();
|
||||
println!("{:?}", priority.iter().map(|x| x + 1).sum::<usize>());
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue