Compare commits
3 commits
d13125c4ad
...
8b15158631
Author | SHA1 | Date | |
---|---|---|---|
8b15158631 | |||
be02406c0f | |||
3e27ce8334 |
7 changed files with 1065 additions and 10 deletions
2
2022/data/day4.expected
Normal file
2
2022/data/day4.expected
Normal file
|
@ -0,0 +1,2 @@
|
|||
576
|
||||
905
|
1000
2022/data/day4.input
Normal file
1000
2022/data/day4.input
Normal file
File diff suppressed because it is too large
Load diff
|
@ -50,11 +50,6 @@ impl BitAnd for ItemSet {
|
|||
}
|
||||
}
|
||||
|
||||
struct Rucksack {
|
||||
left: ItemSet,
|
||||
right: ItemSet,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut data = String::new();
|
||||
stdin().read_to_string(&mut data).unwrap();
|
||||
|
@ -65,16 +60,16 @@ fn main() {
|
|||
let (left, right) = line.split_at(line.len() / 2);
|
||||
assert_eq!(left.len(), right.len());
|
||||
|
||||
let left = left.chars().collect();
|
||||
let right = right.chars().collect();
|
||||
let left: ItemSet = left.chars().collect();
|
||||
let right: ItemSet = right.chars().collect();
|
||||
|
||||
Rucksack { left, right }
|
||||
(left, right)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let sum1: u32 = sacks
|
||||
.iter()
|
||||
.map(|sack| (sack.left & sack.right).priority_of_single_item().unwrap())
|
||||
.map(|&(left, right)| (left & right).priority_of_single_item().unwrap())
|
||||
.sum();
|
||||
|
||||
let sum2: u32 = sacks
|
||||
|
@ -83,7 +78,7 @@ fn main() {
|
|||
.map(|sacks| {
|
||||
sacks
|
||||
.iter()
|
||||
.map(|sack| sack.left | sack.right)
|
||||
.map(|&(left, right)| left | right)
|
||||
.reduce(BitAnd::bitand)
|
||||
.unwrap()
|
||||
.priority_of_single_item()
|
||||
|
|
8
2022/day4/rust/Cargo.toml
Normal file
8
2022/day4/rust/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "rust_2022_04"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
45
2022/day4/rust/src/main.rs
Normal file
45
2022/day4/rust/src/main.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use std::io::{stdin, Read};
|
||||
|
||||
fn main() {
|
||||
let mut data = String::new();
|
||||
stdin().read_to_string(&mut data).unwrap();
|
||||
|
||||
let pairs: Vec<_> = data
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let make_range = |s: &str| {
|
||||
let (start, end) = s.split_once('-').unwrap();
|
||||
|
||||
let start: u64 = start.parse().unwrap();
|
||||
let end = end.parse().unwrap();
|
||||
|
||||
assert!(start <= end);
|
||||
start..=end
|
||||
};
|
||||
|
||||
let (left, right) = line.split_once(',').unwrap();
|
||||
let left = make_range(left);
|
||||
let right = make_range(right);
|
||||
|
||||
(left, right)
|
||||
})
|
||||
.collect();
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
pairs
|
||||
.iter()
|
||||
.filter(|(l, r)| l.start() == r.start()
|
||||
|| l.end() == r.end()
|
||||
|| l.start().cmp(r.start()) != l.end().cmp(r.end()))
|
||||
.count()
|
||||
);
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
pairs
|
||||
.iter()
|
||||
.filter(|(l, r)| l.end() >= r.start() && r.end() >= l.start())
|
||||
.count()
|
||||
);
|
||||
}
|
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -473,6 +473,10 @@ version = "0.1.0"
|
|||
name = "rust_2022_03"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "rust_2022_04"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.11"
|
||||
|
|
|
@ -12,4 +12,5 @@ members = [
|
|||
"2022/day1/rust",
|
||||
"2022/day2/rust",
|
||||
"2022/day3/rust",
|
||||
"2022/day4/rust",
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue