summaryrefslogtreecommitdiffstats
path: root/2021/day5/src/main.rs
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:38:11 +0300
committerYigit Sever2021-12-13 10:38:11 +0300
commit74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch)
treee27db4cd0873c81a53d32277446d926d176304e0 /2021/day5/src/main.rs
parent3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff)
parentac8125750abed263619da4cc6d653bb5ab76f007 (diff)
downloadaoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip
Merge remote-tracking branch 'origin/main'
Diffstat (limited to '2021/day5/src/main.rs')
-rw-r--r--2021/day5/src/main.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/2021/day5/src/main.rs b/2021/day5/src/main.rs
new file mode 100644
index 0000000..498deb0
--- /dev/null
+++ b/2021/day5/src/main.rs
@@ -0,0 +1,95 @@
1use std::cmp::{max, min};
2use std::collections::HashMap;
3use std::env;
4use std::fs::File;
5use std::io::{BufRead, BufReader};
6
7fn main() {
8 let reader = open_file();
9
10 let mut coords: HashMap<(i32, i32), i32> = HashMap::new();
11
12 for line in reader.lines() {
13 if let Ok(line) = line {
14 let vents = line.split(" -> ").collect::<Vec<&str>>();
15 let first = vents[0].split(",").collect::<Vec<&str>>();
16 let second = vents[1].split(",").collect::<Vec<&str>>();
17
18 let first: Vec<i32> = first.into_iter().map(|s| s.parse().unwrap()).collect();
19 let second: Vec<i32> = second.into_iter().map(|s| s.parse().unwrap()).collect();
20
21 let xstep: i32;
22 let ystep: i32;
23 let start_x = min(first[0], second[0]);
24 let end_x = max(first[0], second[0]);
25 let start_y = min(first[1], second[1]);
26 let end_y = max(first[1], second[1]);
27 let distance = max(end_x - start_x, end_y - start_y);
28
29 if first[0] > second[0] {
30 xstep = -1;
31 } else if first[0] < second[0] {
32 xstep = 1;
33 } else {
34 xstep = 0;
35 }
36
37 if first[1] > second[1] {
38 ystep = -1;
39 } else if first[1] < second[1] {
40 ystep = 1;
41 } else {
42 ystep = 0;
43 }
44
45 for step in 0..=distance {
46 let counter = coords
47 .entry((first[0] + step * xstep, first[1] + step * ystep))
48 .or_insert(0);
49 *counter += 1;
50 }
51 }
52 }
53
54 // println!("coords: {:?}", coords);
55 // visualize(&coords);
56
57 let result = coords.iter().filter(|(_, &y)| y >= 2).count();
58 println!("{}", result);
59}
60
61fn visualize(map: &HashMap<(i32, i32), i32>) {
62 let start_x = map.iter().map(|((x, _), _)| x).min().unwrap();
63 let start_y = map.iter().map(|((_, y), _)| y).min().unwrap();
64 let end_x = map.iter().map(|((x, _), _)| x).max().unwrap();
65 let end_y = map.iter().map(|((_, y), _)| y).max().unwrap();
66 println!("start_x: {}", start_x);
67 println!("start_y: {}", start_y);
68 println!("end_x: {}", end_x);
69 println!("end_y: {}", end_y);
70
71 for y in *start_y..=*end_y {
72 for x in *start_x..=*end_x {
73 if let Some(val) = map.get(&(x, y)) {
74 print!("{}", val);
75 } else {
76 print!(".")
77 }
78 }
79 println!("");
80 }
81}
82
83fn open_file() -> BufReader<File> {
84 let args: Vec<String> = env::args().collect();
85
86 if args.len() != 2 {
87 eprintln!("Usage: {} <filename>", args[0]);
88 std::process::exit(1);
89 }
90
91 let filename = &args[1];
92
93 let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename));
94 BufReader::new(file)
95}