aboutsummaryrefslogtreecommitdiff
path: root/2022/15/common.rs
blob: bece48400672eefc6fa6ec145a645c6accb6bd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
pub struct Pos {
    pub x: i64,
    pub y: i64
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Range {
    pub left: i64,
    pub right: i64
}

#[derive(Copy, Clone, Hash, Eq, PartialEq)]
pub struct Sensor {
    pub s: Pos,
    pub b: Pos,
    pub d: i64
}

pub fn distance(p: Pos, q: Pos) -> i64 {
   (p.x-q.x).abs() + (p.y-q.y).abs() 
}

impl Sensor {
    pub fn from_line(line: &str) -> Sensor {
        let mut i = 1 + line.find('=').unwrap();
        let mut j = line.find(',').unwrap();
        let sx = line[i..j].parse::<i64>().unwrap();

        i = j+4;
        j = line.find(':').unwrap();
        let sy = line[i..j].parse::<i64>().unwrap();

        i = 1 + j + line[j..].find('=').unwrap();
        j = i + line[i..].find(',').unwrap();
        let bx = line[i..j].parse::<i64>().unwrap();

        i = j+4;
        j = line.len()-1;
        let by = line[i..j].parse::<i64>().unwrap();

        let s = Pos { x: sx, y: sy };
        let b = Pos { x: bx, y: by };
        let d = distance(s, b);
        Sensor { s, b, d }
    }

    pub fn get_range(&self, y: i64) -> Range {
        let d = distance(self.s, Pos { x: self.s.x, y });
        Range {
            left: self.s.x - self.d + d,
            right: self.s.x + self.d - d
        }
    }
}

pub fn read_sensors_from_stdin() -> Vec<Sensor> {
    let mut v = Vec::<Sensor>::new();
    let mut line = String::new();
    while std::io::stdin().read_line(&mut line).unwrap() > 0 {
        v.push(Sensor::from_line(&line));
        line.clear();
    }
    v
}

Generated with cgit - Back to sebastiano.tronto.net