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
|
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use num::complex::Complex as c;
use num::Complex;
use std::cmp::min;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs::File;
use std::io::Read;
type Matrix<T> = Vec<Vec<T>>;
fn bfs1(m: &Matrix<u32>) -> u32 {
let mut s: HashMap<(Complex<i32>, Complex<i32>, u32), u32> = HashMap::new();
let mut hs: HashSet<(Complex<i32>, Complex<i32>, u32, u32)> = HashSet::new();
let mut hs_next: HashSet<(Complex<i32>, Complex<i32>, u32, u32)> = HashSet::new();
hs.insert((c::new(0, 0), c::new(1, 0), 0, 0));
let mut res: Vec<u32> = Vec::new();
while hs.len() > 0 {
for (pos, dir, conti, mut hl) in hs {
if conti > 2 {
continue;
}
if !(0 <= pos.re
&& pos.re < m[0].len() as i32
&& 0 <= pos.im
&& pos.im < m.len() as i32)
{
continue;
}
hl += m[pos.im as usize][pos.re as usize];
if pos == c::new(m[0].len() as i32 - 1, m.len() as i32 - 1) {
res.push(hl);
continue;
}
if let Some(hl_) = s.get(&(pos, dir, conti)) {
if hl_ <= &hl {
continue;
}
}
s.insert((pos, dir, conti), hl);
let new_dir = dir;
let new_pos = pos + new_dir;
hs_next.insert((new_pos, new_dir, conti + 1, hl));
let new_dir = dir * c::i();
let new_pos = pos + new_dir;
hs_next.insert((new_pos, new_dir, 0, hl));
let new_dir = dir * (-c::i());
let new_pos = pos + new_dir;
hs_next.insert((new_pos, new_dir, 0, hl));
}
hs = hs_next;
hs_next = HashSet::new();
}
return res.into_iter().fold(999999999, min) - m[0][0];
}
fn bfs2(m: &Matrix<u32>) -> u32 {
let mut s: HashMap<(Complex<i32>, Complex<i32>, u32), u32> = HashMap::new();
let mut hs: HashSet<(Complex<i32>, Complex<i32>, u32, u32)> = HashSet::new();
let mut hs_next: HashSet<(Complex<i32>, Complex<i32>, u32, u32)> = HashSet::new();
hs.insert((c::new(0, 0), c::new(1, 0), 0, 0));
let mut res: Vec<u32> = Vec::new();
while hs.len() > 0 {
for (pos, dir, conti, mut hl) in hs {
if conti > 9 {
continue;
}
if !(0 <= pos.re
&& pos.re < m[0].len() as i32
&& 0 <= pos.im
&& pos.im < m.len() as i32)
{
continue;
}
hl += m[pos.im as usize][pos.re as usize];
if conti > 2 && pos == c::new(m[0].len() as i32 - 1, m.len() as i32 - 1) {
res.push(hl);
continue;
}
if let Some(hl_) = s.get(&(pos, dir, conti)) {
if hl_ <= &hl {
continue;
}
}
s.insert((pos, dir, conti), hl);
let new_dir = dir;
let new_pos = pos + new_dir;
hs_next.insert((new_pos, new_dir, conti + 1, hl));
if conti > 2 {
let new_dir = dir * c::i();
let new_pos = pos + new_dir;
hs_next.insert((new_pos, new_dir, 0, hl));
let new_dir = dir * (-c::i());
let new_pos = pos + new_dir;
hs_next.insert((new_pos, new_dir, 0, hl));
}
}
hs = hs_next;
hs_next = HashSet::new();
}
return res.into_iter().fold(999999999, min) - m[0][0];
}
fn main() {
// let filename = "in/day17.ref";
let filename = "in/day17.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 = content.trim_end().split('\n');
let mut m: Matrix<u32> = Vec::new();
for line in lines {
let mut mr: Vec<u32> = Vec::new();
for c in line.chars() {
mr.push(c.to_digit(10).unwrap());
}
m.push(mr);
}
let res1 = bfs1(&m);
let res2 = bfs2(&m);
println!("res1: {}", res1);
println!("res2: {}", res2);
assert_eq!(res1, 635);
assert_eq!(res2, 734);
}
|