Compare commits
No commits in common. "8b15158631ed69202811bc763e943372ec448424" and "d13125c4ad457b17b0ce0ca99fda2974dd27f6bd" have entirely different histories.
8b15158631
...
d13125c4ad
7 changed files with 10 additions and 1065 deletions
|
@ -1,2 +0,0 @@
|
||||||
576
|
|
||||||
905
|
|
1000
2022/data/day4.input
1000
2022/data/day4.input
File diff suppressed because it is too large
Load diff
|
@ -50,6 +50,11 @@ 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();
|
||||||
|
@ -60,16 +65,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: ItemSet = left.chars().collect();
|
let left = left.chars().collect();
|
||||||
let right: ItemSet = right.chars().collect();
|
let right = right.chars().collect();
|
||||||
|
|
||||||
(left, right)
|
Rucksack { left, right }
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let sum1: u32 = sacks
|
let sum1: u32 = sacks
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&(left, right)| (left & right).priority_of_single_item().unwrap())
|
.map(|sack| (sack.left & sack.right).priority_of_single_item().unwrap())
|
||||||
.sum();
|
.sum();
|
||||||
|
|
||||||
let sum2: u32 = sacks
|
let sum2: u32 = sacks
|
||||||
|
@ -78,7 +83,7 @@ fn main() {
|
||||||
.map(|sacks| {
|
.map(|sacks| {
|
||||||
sacks
|
sacks
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&(left, right)| left | right)
|
.map(|sack| sack.left | sack.right)
|
||||||
.reduce(BitAnd::bitand)
|
.reduce(BitAnd::bitand)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.priority_of_single_item()
|
.priority_of_single_item()
|
||||||
|
|
|
@ -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]
|
|
|
@ -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
4
Cargo.lock
generated
|
@ -473,10 +473,6 @@ 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"
|
||||||
|
|
|
@ -12,5 +12,4 @@ members = [
|
||||||
"2022/day1/rust",
|
"2022/day1/rust",
|
||||||
"2022/day2/rust",
|
"2022/day2/rust",
|
||||||
"2022/day3/rust",
|
"2022/day3/rust",
|
||||||
"2022/day4/rust",
|
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue