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
66
67
68
69
70
71
72
73
74
|
use std::cmp::{max, min};
use std::collections::{HashMap};
use std::io::{BufRead};
fn parse(s: & str) -> [u64; 4] {
let mut halves = s.split(" -> ");
let half_1 = halves.next().unwrap().split(',');
let half_2 = halves.next().unwrap().split(',');
let mut it = half_1.chain(half_2);
let mut ret: [u64; 4] = [0; 4];
ret[0] = it.next().expect("Malformed input").parse().expect("Malformed input");
ret[1] = it.next().expect("Malformed input").parse().expect("Malformed input");
ret[2] = it.next().expect("Malformed input").parse().expect("Malformed input");
ret[3] = it.next().expect("Malformed input").parse().expect("Malformed input");
ret
}
fn print_small_diagram(coverage: & HashMap<(u64, u64), u64>) {
for y in 0..20 {
for x in 0..20 {
let &n = coverage.get(&(x, y)).unwrap_or(&0);
if n == 0 { print!("."); }
else { print!("{}", n); }
}
println!("");
}
}
pub fn main() {
let mut stdin = std::io::stdin();
let mut handle = stdin.lock();
let mut coverage: HashMap<(u64, u64), u64> = HashMap::new();
for l in handle.lines() {
let line = parse(& l.unwrap());
let (dx, dy) = (max(line[2], line[0]) - min(line[2], line[0]), max(line[3], line[1]) - min(line[3], line[1]));
if dx == 0 { // Vertical
let x = line[0];
let start_y = min(line[1], line[3]);
let stop_y = max(line[1], line[3]);
for y in start_y..stop_y+1 {
let &n = coverage.get(&(x, y)).unwrap_or(&0);
coverage.insert((x, y), n + 1);
}
}
else if dy == 0 { // Horizonal
let y = line[1];
let start_x = min(line[0], line[2]);
let stop_x = max(line[0], line[2]);
for x in start_x..stop_x+1 {
let &n = coverage.get(&(x, y)).unwrap_or(&0);
coverage.insert((x, y), n + 1);
}
}
else if dx == dy { // Diagonal
let start_x = line[0];
let start_y = line[1];
for d in 0..dx+1 {
let x = if line[0] <= line[2] { start_x + d } else { start_x - d };
let y = if line[1] <= line[3] { start_y + d } else { start_y - d };
let &n = coverage.get(&(x, y)).unwrap_or(&0);
coverage.insert((x, y), n + 1);
}
}
}
let n: usize = coverage.into_iter().map(|(k, v)| v).filter(|&count| count >= 2).count();
println!("{}", n);
}
|