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
|
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
fn is_symbol(c: char) -> bool {
if c.is_digit(10) || c == '.' {
false
} else {
true
}
}
fn dydx() -> Vec<(i32, i32)> {
let mut ret = Vec::new();
for y in -1i32..=1 {
for x in -1i32..=1 {
ret.push((y, x));
}
}
ret
}
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();
let pad: Vec<_> = (0..lines[0].bytes().len() + 2).map(|_| '.').collect();
m.push(pad.clone());
for line in &lines {
let mut l: Vec<char> = Vec::new();
l.push('.');
for c in line.bytes() {
l.push(c as char)
}
l.push('.');
m.push(l)
}
m.push(pad.clone());
let mut hm: HashMap<(usize, usize), (u32, u32)> = HashMap::new();
let mut id: u32 = 0;
for y in 1..m.len() - 1 {
let mut have_num = false;
let mut num_valid = false;
let mut start_x = 0;
for x in 1..m[0].len() {
if have_num {
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 x == m[0].len() - 1 {
continue;
}
if have_num {
num_valid = num_valid
|| dydx()
.into_iter()
.map(|(dy, dx)| {
is_symbol(m[((y as i32) + dy) as usize][((x as i32) + dx) as usize])
})
.any(|x| x);
}
}
}
for y in 0..m.len() {
for x in 0..m[0].len() {
if m[y][x] == '*' {
let mut neighbour: HashMap<u32, u32> = HashMap::new();
for (dy, dx) in dydx().into_iter() {
let xx = ((x as i32) + dx) as usize;
let yy = ((y as i32) + dy) as usize;
if let Some((id, val)) = hm.get(&(xx, yy)) {
neighbour.insert(*id, *val);
}
}
if neighbour.len() == 2 {
res2 += neighbour
.iter()
.map(|(_, val)| *val)
.reduce(|a, b| a * b)
.unwrap();
}
}
}
}
println!("res1: {}", res1);
println!("res2: {}", res2);
assert_eq!(res1, 532445);
assert_eq!(res2, 79842967);
}
|