Compare commits
2 Commits
4fe10ee925
...
54dad514be
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54dad514be | ||
|
|
fcf2c58b71 |
46
2025/05.rs
Normal file
46
2025/05.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
---cargo
|
||||
---
|
||||
|
||||
static CONTENT: &'static str = include_str!("./inputs/05_1.txt");
|
||||
|
||||
fn main() {
|
||||
let mut ranges: Vec<_> = CONTENT.lines().map(|l| l.trim()).take_while(|l| !l.is_empty()).map(|r| {
|
||||
let (start, end) = r.split_once('-').unwrap();
|
||||
let start = start.parse::<usize>().unwrap();
|
||||
let end = end.parse::<usize>().unwrap();
|
||||
|
||||
start..=end
|
||||
}).collect();
|
||||
|
||||
loop {
|
||||
let result: Option<(usize, usize)> = ranges.iter().enumerate().find_map(|(idx, r)| {
|
||||
ranges.iter().enumerate().filter(|(o_idx, _)| idx != *o_idx).find_map(|(o_idx, other)| {
|
||||
r.contains(other.start()).then_some((idx, o_idx))
|
||||
})
|
||||
});
|
||||
|
||||
let (idx, o_idx) = match result {
|
||||
Some(r) => r,
|
||||
None => break,
|
||||
};
|
||||
|
||||
let first = ranges[idx].clone();
|
||||
let second = ranges[o_idx].clone();
|
||||
|
||||
let new_range_start: usize = *first.start().min(second.start());
|
||||
let new_range_end: usize = *first.end().max(second.end());
|
||||
|
||||
let new_range = new_range_start..=new_range_end;
|
||||
|
||||
println!("Merge\n{:?} +\n{:?} =\n{:?}", first, second, new_range);
|
||||
ranges[idx] = new_range;
|
||||
ranges.remove(o_idx);
|
||||
}
|
||||
|
||||
ranges.sort_by_key(|v| (*v.start(), *v.end()));
|
||||
|
||||
println!("Ranges: \n{:#?}", ranges);
|
||||
|
||||
let result: usize = ranges.iter().map(|r| r.clone().count()).sum();
|
||||
println!("Result: {:?}", result);
|
||||
}
|
||||
1191
2025/inputs/05_1.txt
Normal file
1191
2025/inputs/05_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
11
2025/inputs/05_example.txt
Normal file
11
2025/inputs/05_example.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
Reference in New Issue
Block a user