Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- const INPUT: &str = include_str!("../data/input.txt");
- pub fn main() {
- let claims = INPUT
- .lines()
- .filter_map(|l| {
- let mut nums = l
- .split(|c| c == '#' || c == '@' || c == ',' || c == ':' || c == 'x')
- .filter_map(|num| num.trim().parse::<usize>().ok());
- let c = Claim {
- id: nums.next().unwrap(),
- x: nums.next().unwrap(),
- y: nums.next().unwrap(),
- width: nums.next().unwrap(),
- height: nums.next().unwrap(),
- };
- Some(c)
- }).collect::<Vec<_>>();
- let mut inserted = HashMap::new();
- let mut all_claims = claims.iter().map(|c| c.id).collect::<Vec<_>>();
- for claim in claims {
- for x in claim.x..claim.x + claim.width {
- for y in claim.y..claim.y + claim.height {
- *inserted.entry((x, y)).or_insert(0) += 1;
- if *inserted.entry((x, y)).or_default() > 1 {
- if let Some(index) = all_claims.iter().position(|c| c == &claim.id) {
- all_claims.remove(index);
- }
- }
- }
- }
- }
- println!(
- "{}",
- inserted
- .iter()
- .fold(0, |acc, (_, v)| if *v > 1 { acc + 1 } else { acc })
- );
- println!("{:?}", all_claims);
- }
- struct Claim {
- id: usize,
- x: usize,
- y: usize,
- width: usize,
- height: usize,
- }
- impl fmt::Debug for Claim {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(
- f,
- "Claim #{} @{},{}: {}x{}",
- self.id, self.x, self.y, self.width, self.height
- )
- }
- }
Add Comment
Please, Sign In to add comment