Select Git revision
Forked from
KIF / AKPlanning
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
types.rs 1.21 KiB
use std::{fs::read_to_string, path::Path};
use regex::Regex;
#[derive(Debug)]
pub struct SensorData {
pub position: Point,
pub beacon: Point,
}
impl SensorData {
pub fn new(position: Point, beacon: Point) -> Self {
Self { position, beacon }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
pub fn distance(&self, other: Point) -> u32 {
self.x.abs_diff(other.x) + self.y.abs_diff(other.y)
}
}
pub fn parse_input(path: impl AsRef<Path>) -> Vec<SensorData> {
let data = read_to_string(path).unwrap();
let mut sensors = Vec::new();
let reg =
Regex::new(r"x=(?P<s_x>-?\d+), y=(?P<s_y>-?\d+).*x=(?P<b_x>-?\d+), y=(?P<b_y>-?\d+)").unwrap();
for cap in reg.captures_iter(&data) {
sensors.push(SensorData::new(
Point::new(
cap["s_x"].parse::<i32>().unwrap(),
cap["s_y"].parse::<i32>().unwrap(),
),
Point::new(
cap["b_x"].parse::<i32>().unwrap(),
cap["b_y"].parse::<i32>().unwrap(),
),
));
}
sensors
}