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
159
160
161
162
163
164
165
166
167
168
|
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
// use std::cmp::max;
// use std::cmp::min;
fn is_symbol(c: char) -> bool {
if c.is_digit(10) || c == '.' {
false
} else {
true
}
}
fn is_valid(x: usize, y: usize, m: &Vec<Vec<char>>) -> bool {
for dy in -1i32..2 {
for dx in -1i32..2 {
let mut xx: usize;
let mut yy: usize;
if x == 0 && dx == -1 {
xx = x;
} else if x == m[0].len() - 1 && dx == 1 {
xx = x;
} else {
xx = ((x as i32) + dx) as usize;
}
if y == 0 && dy == -1 {
yy = y;
} else if y == m.len() - 1 && dy == 1 {
yy = y;
} else {
yy = ((y as i32) + dy) as usize;
}
// println!("{} {} {} {} {} {}", x, y, dx, dy, xx, yy);
// print!("{}", m[y][x]);
if is_symbol(m[yy][xx]) {
return true;
}
}
}
return false;
}
fn main() {
// let filename = "in/day03.ref";
let filename = "in/day03.pzl";
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();
// println!("{:?}", lines);
let mut res1 = 0;
let mut res2 = 0;
let mut m: Vec<Vec<char>> = Vec::new();
for line in &lines {
let mut l: Vec<char> = Vec::new();
for c in line.bytes() {
l.push(c as char)
}
m.push(l)
}
let mut hm: HashMap<(usize, usize), (u32, u32)> = HashMap::new();
let mut id: u32 = 0;
for y in 0..m.len() {
let mut have_num = false;
let mut num_valid = false;
let mut start_x = 0;
for x in 0..m[0].len() {
if have_num {
if m[y][x].is_digit(10) == true && x == (m[0].len() - 1) {
if num_valid {
let num: u32 = String::from_iter(m[y][start_x..=x].iter()).parse().unwrap();
for xx in start_x..=x {
hm.insert((xx, y), (id, num));
}
id += 1;
res1 += num;
}
have_num = false;
num_valid = false;
} else if m[y][x].is_digit(10) == false {
if num_valid {
let num: u32 = String::from_iter(m[y][start_x..x].iter()).parse().unwrap();
for xx in start_x..x {
hm.insert((xx, y), (id, num));
}
id += 1;
res1 += num;
}
have_num = false;
num_valid = false;
}
} else if m[y][x].is_digit(10) {
have_num = true;
start_x = x;
}
if have_num {
num_valid = num_valid || is_valid(x, y, &m);
}
}
}
println!("{:?}", hm);
for y in 0..m.len() {
for x in 0..m[0].len() {
if m[y][x] == '*' {
// println!("{} {}", y+1, x+1);
let mut neighbour: HashMap<u32, u32> = HashMap::new();
for dy in -1i32..2 {
for dx in -1i32..2 {
let mut xx: usize;
let mut yy: usize;
if x == 0 && dx == -1 {
xx = x;
} else if x == m[0].len() - 1 && dx == 1 {
xx = x;
} else {
xx = ((x as i32) + dx) as usize;
}
if y == 0 && dy == -1 {
yy = y;
} else if y == m.len() - 1 && dy == 1 {
yy = y;
} else {
yy = ((y as i32) + dy) as usize;
}
if let Some((id, val)) = hm.get(&(xx, yy)) {
neighbour.insert(*id, *val);
}
}
}
if neighbour.len() == 2 {
let mut mul: u32 = 1;
for (key, val) in neighbour.iter() {
mul *= val;
}
// res2 += neighbour.iter().map(|(_, val)| val).reduce(|a,b| a*b).unwrap();
println!("{}", mul);
res2 += mul;
}
// let a: u32 = neighbour.iter()
// .inspect(|x| println!("asdf {:?}", x))
// .map(|(_, val)| val)
// .reduce(|a,b| a*b);
// println!("{:?}", a);
// println!("{:?}", neighbour);
// asdf
}
}
}
println!("res1: {}", res1);
println!("res2: {}", res2);
}
|