56 lines
1.9 KiB
Rust
56 lines
1.9 KiB
Rust
use std::fs::File;
|
|
use std::io::{self, BufRead};
|
|
use std::path::Path;
|
|
use std::collections::HashSet;
|
|
|
|
fn main() {
|
|
let file_path = String::from("input");
|
|
println!("In file {}", file_path);
|
|
let mut sensors: Vec<(i32, i32)> = vec![];
|
|
let mut beacon: Vec<(i32, i32)> = vec![];
|
|
let mut distances: Vec<i32> = vec![];
|
|
|
|
if let Ok(lines) = read_lines(file_path) {
|
|
// Consumes the iterator, returns an (Optional) String
|
|
for line in lines {
|
|
if let Ok(ip) = line {
|
|
let split = ip.split(" ")
|
|
.enumerate()
|
|
.filter(|&(i, _)| i == 2 || i == 3 || i == 8 || i == 9)
|
|
.map(|(i, v)| v[2..v.len() - 1 + (i == 9) as usize].parse::<i32>().unwrap())
|
|
.collect::<Vec<i32>>();
|
|
sensors.push((split[0], split[1]));
|
|
beacon.push((split[2], split[3]));
|
|
distances.push((split[0] - split[2]).abs() + (split[1] - split[3]).abs());
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut count = HashSet::<i32>::new();
|
|
for (i, _) in sensors.iter().enumerate() {
|
|
let range = distances[i] - (sensors[i].1 - 2000000).abs() + 1;
|
|
println!("i {} range {}", i, range);
|
|
for j in 0..range {
|
|
if i == 6 {
|
|
println!("{}, {}", sensors[i].0 + j, sensors[i].0 - j);
|
|
}
|
|
count.insert(sensors[i].0 + j);
|
|
count.insert(sensors[i].0 - j);
|
|
}
|
|
}
|
|
println!("{:#?}", distances);
|
|
if count.contains(&-362535) {
|
|
println!("Contains key ! {} is answer", count.len() - 1);
|
|
}
|
|
if count.contains(&2) {
|
|
println!("EXAMPLE Contains key ! {} is answer", count.len() - 1);
|
|
}
|
|
println!("{}", count.len());
|
|
}
|
|
|
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
where P: AsRef<Path>, {
|
|
let file = File::open(filename)?;
|
|
Ok(io::BufReader::new(file).lines())
|
|
}
|