Compare commits

..

No commits in common. "8b15158631ed69202811bc763e943372ec448424" and "d13125c4ad457b17b0ce0ca99fda2974dd27f6bd" have entirely different histories.

7 changed files with 10 additions and 1065 deletions

View file

@ -1,2 +0,0 @@
576
905

File diff suppressed because it is too large Load diff

View file

@ -50,6 +50,11 @@ impl BitAnd for ItemSet {
}
}
struct Rucksack {
left: ItemSet,
right: ItemSet,
}
fn main() {
let mut data = String::new();
stdin().read_to_string(&mut data).unwrap();
@ -60,16 +65,16 @@ fn main() {
let (left, right) = line.split_at(line.len() / 2);
assert_eq!(left.len(), right.len());
let left: ItemSet = left.chars().collect();
let right: ItemSet = right.chars().collect();
let left = left.chars().collect();
let right = right.chars().collect();
(left, right)
Rucksack { left, right }
})
.collect();
let sum1: u32 = sacks
.iter()
.map(|&(left, right)| (left & right).priority_of_single_item().unwrap())
.map(|sack| (sack.left & sack.right).priority_of_single_item().unwrap())
.sum();
let sum2: u32 = sacks
@ -78,7 +83,7 @@ fn main() {
.map(|sacks| {
sacks
.iter()
.map(|&(left, right)| left | right)
.map(|sack| sack.left | sack.right)
.reduce(BitAnd::bitand)
.unwrap()
.priority_of_single_item()

View file

@ -1,8 +0,0 @@
[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]

View file

@ -1,45 +0,0 @@
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
View file

@ -473,10 +473,6 @@ 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"

View file

@ -12,5 +12,4 @@ members = [
"2022/day1/rust",
"2022/day2/rust",
"2022/day3/rust",
"2022/day4/rust",
]