summary refs log tree commit diff
path: root/05/src/main.rs
blob: 336f434dd96e0bee4e4de81427520a5d237e3b26 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use advent_lib::prelude::*;

use std::collections::HashMap;


fn main() -> Result<()> {
  let mut args = std::env::args();
  if args.len() != 2 {
    eprintln!("Usage: advent input");
  }
  let _ = args.next();
  let filename = args.next().unwrap();

  let input = advent_lib::read_lines_file(&filename)?;

  let mut line_specs = Vec::new();
  for line in &input {
    let words: Vec<&str> = line.split_whitespace().collect();
    let start = parse_coords(words[0])?;
    let end = parse_coords(words[2])?;
    line_specs.push((start, end));
  }

  {
    let mut counts: HashMap<(i64, i64), i64> = HashMap::new();
    for ((x_start, y_start), (x_end, y_end)) in &line_specs {
      if x_start == x_end {
        let x = *x_start;

        let (y_min, y_max) = if y_start <= y_end {
          (y_start, y_end)
        } else {
          (y_end, y_start)
        };

        for y in *y_min .. y_max + 1 {
          let n = match counts.get(&(x, y)) {
            Some(n) => *n,
            None => 0,
          };
          counts.insert((x, y), n + 1);
        }
      } else if y_start == y_end {
        let y = *y_start;

        let (x_min, x_max) = if x_start <= x_end {
          (x_start, x_end)
        } else {
          (x_end, x_start)
        };

        for x in *x_min .. x_max + 1 {
          let n = match counts.get(&(x, y)) {
            Some(n) => *n,
            None => 0,
          };
          counts.insert((x, y), n + 1);
        }
      }
    }

    let mut n_intersections = 0;
    for cell in counts.values() {
      if *cell > 1 {
        n_intersections += 1;
      }
    }
    println!("{}", n_intersections);
  }

  {
    let mut counts: HashMap<(i64, i64), i64> = HashMap::new();
    for ((x_start, y_start), (x_end, y_end)) in &line_specs {
      if x_start == x_end {
        let x = *x_start;

        let (y_min, y_max) = if y_start <= y_end {
          (y_start, y_end)
        } else {
          (y_end, y_start)
        };

        for y in *y_min .. y_max + 1 {
          let n = match counts.get(&(x, y)) {
            Some(n) => *n,
            None => 0,
          };
          counts.insert((x, y), n + 1);
        }
      } else if y_start == y_end {
        let y = *y_start;

        let (x_min, x_max) = if x_start <= x_end {
          (x_start, x_end)
        } else {
          (x_end, x_start)
        };

        for x in *x_min .. x_max + 1 {
          let n = match counts.get(&(x, y)) {
            Some(n) => *n,
            None => 0,
          };
          counts.insert((x, y), n + 1);
        }
      } else {
        let (x_start, x_end, y_start, y_end) = if x_start <= x_end {
          (x_start, x_end, y_start, y_end)
        } else {
          (x_end, x_start, y_end, y_start)
        };

        if y_start <= y_end {
          for i in 0 .. x_end + 1 - x_start {
            let x = x_start + i;
            let y = y_start + i;

            let n = match counts.get(&(x, y)) {
              Some(n) => *n,
              None => 0,
            };
            counts.insert((x, y), n + 1);
          }
        } else {
          for i in 0 .. x_end + 1 - x_start {
            let x = x_start + i;
            let y = y_start - i;

            let n = match counts.get(&(x, y)) {
              Some(n) => *n,
              None => 0,
            };
            counts.insert((x, y), n + 1);
          }
        }
      }
    }

    let mut n_intersections = 0;
    for cell in counts.values() {
      if *cell > 1 {
        n_intersections += 1;
      }
    }
    println!("{}", n_intersections);
  }

  Ok(())
}


fn parse_coords(word: &str) -> Result<(i64, i64)> {
  let values: Vec<&str> = word.split(',').collect();
  let x = values[0].parse::<i64>()?;
  let y = values[1].parse::<i64>()?;
  Ok((x, y))
}