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
|
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use itertools::Itertools;
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
use std::fs::File;
use std::io::Read;
fn get_antinodes(a: (i32, i32), b: (i32, i32)) -> Vec<(i32, i32)> {
let mut ans = Vec::new();
let (ax, ay) = (a.0, a.1);
let (bx, by) = (b.0, b.1);
let dx = bx - ax;
let dy = by - ay;
ans.push((bx + dx, by + dy));
ans.push((ax - dx, ay - dy));
return ans;
}
fn bounded(min: i32, max: i32, val: i32) -> bool {
min <= val && val < max
}
fn get_antinodes2(xmax: i32, ymax: i32, a: (i32, i32), b: (i32, i32)) -> Vec<(i32, i32)> {
let mut ans = Vec::new();
let (ax, ay) = (a.0, a.1);
let (bx, by) = (b.0, b.1);
let dx = bx - ax;
let dy = by - ay;
ans.push((ax, ay));
ans.push((bx, by));
for n in 1.. {
let (ux, uy) = (bx + n * dx, by + n * dy);
if !bounded(0, xmax, ux) || !bounded(0, ymax, uy) {
break;
}
ans.push((ux, uy));
}
for n in 1.. {
let (ux, uy) = (ax - n * dx, ay - n * dy);
if !bounded(0, xmax, ux) || !bounded(0, ymax, uy) {
break;
}
ans.push((ux, uy));
}
return ans;
}
fn main() {
let args: Vec<String> = env::args().collect();
let filename = if args.len() == 1 {
"in/".to_owned() + args[0].split('/').last().unwrap() + ".pzl"
} else {
args[1].clone()
};
let mut f = File::open(filename).expect("cannot open file");
let mut content = String::new();
f.read_to_string(&mut content).expect("cannot read file");
let lines: Vec<_> = content.trim_end().split("\n").collect();
let ymax = lines.len() as i32;
let xmax = lines[0].len() as i32;
let mut hm: HashMap<char, Vec<(i32, i32)>> = HashMap::new();
for (y, line) in lines.iter().enumerate() {
for (x, c) in line.chars().enumerate() {
if c != '.' {
hm.entry(c).or_default().push((x as i32, y as i32));
}
}
}
let mut antinodes: HashSet<(i32, i32)> = HashSet::new();
let mut antinodes2: HashSet<(i32, i32)> = HashSet::new();
for (key, val) in hm.clone().into_iter() {
for (a, b) in val.clone().into_iter().tuple_combinations() {
for (x, y) in get_antinodes(a, b) {
if bounded(0, xmax, x) && bounded(0, ymax, y) {
antinodes.insert((x, y));
}
}
for (x, y) in get_antinodes2(xmax, ymax, a, b) {
antinodes2.insert((x, y));
}
}
}
let res1 = antinodes.len();
let res2 = antinodes2.len();
println!("res1: {}", res1);
println!("res2: {}", res2);
assert_eq!(res1, 293);
assert_eq!(res2, 934);
}
|