Compare commits

..

3 commits

Author SHA1 Message Date
8b15158631 2022 day3/rust: use tuple instead of struct 2022-12-04 09:59:33 +01:00
be02406c0f 2022 day4/rust: add solution 2022-12-04 09:59:17 +01:00
3e27ce8334 2022 day4: add data 2022-12-04 09:58:53 +01:00
7 changed files with 1065 additions and 10 deletions

2
2022/data/day4.expected Normal file
View file

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

1000
2022/data/day4.input Normal file

File diff suppressed because it is too large Load diff

View file

@ -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()

View 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]

View 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
View file

@ -473,6 +473,10 @@ version = "0.1.0"
name = "rust_2022_03" name = "rust_2022_03"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "rust_2022_04"
version = "0.1.0"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.11" version = "1.0.11"

View file

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