2022 day3/rust: use tuple instead of struct
This commit is contained in:
parent
be02406c0f
commit
8b15158631
1 changed files with 5 additions and 10 deletions
|
@ -50,11 +50,6 @@ impl BitAnd for ItemSet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Rucksack {
|
|
||||||
left: ItemSet,
|
|
||||||
right: ItemSet,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut data = String::new();
|
let mut data = String::new();
|
||||||
stdin().read_to_string(&mut data).unwrap();
|
stdin().read_to_string(&mut data).unwrap();
|
||||||
|
@ -65,16 +60,16 @@ fn main() {
|
||||||
let (left, right) = line.split_at(line.len() / 2);
|
let (left, right) = line.split_at(line.len() / 2);
|
||||||
assert_eq!(left.len(), right.len());
|
assert_eq!(left.len(), right.len());
|
||||||
|
|
||||||
let left = left.chars().collect();
|
let left: ItemSet = left.chars().collect();
|
||||||
let right = right.chars().collect();
|
let right: ItemSet = right.chars().collect();
|
||||||
|
|
||||||
Rucksack { left, right }
|
(left, right)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let sum1: u32 = sacks
|
let sum1: u32 = sacks
|
||||||
.iter()
|
.iter()
|
||||||
.map(|sack| (sack.left & sack.right).priority_of_single_item().unwrap())
|
.map(|&(left, right)| (left & right).priority_of_single_item().unwrap())
|
||||||
.sum();
|
.sum();
|
||||||
|
|
||||||
let sum2: u32 = sacks
|
let sum2: u32 = sacks
|
||||||
|
@ -83,7 +78,7 @@ fn main() {
|
||||||
.map(|sacks| {
|
.map(|sacks| {
|
||||||
sacks
|
sacks
|
||||||
.iter()
|
.iter()
|
||||||
.map(|sack| sack.left | sack.right)
|
.map(|&(left, right)| left | right)
|
||||||
.reduce(BitAnd::bitand)
|
.reduce(BitAnd::bitand)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.priority_of_single_item()
|
.priority_of_single_item()
|
||||||
|
|
Loading…
Reference in a new issue