-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.rs
113 lines (99 loc) · 2.66 KB
/
day3.rs
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
const MAX: i32 = 280;
fn strip(
m: u32,
target: u32,
n: &mut u32,
x: &mut i32,
y: &mut i32,
xstep: i32,
ystep: i32,
) -> Option<i32> {
for _ in 1..m {
*n += 1;
*x += xstep;
*y += ystep;
let dx = *x - MAX;
let dy = *y - MAX;
if *n == target {
return Some(dx.abs() + dy.abs());
}
}
None
}
fn task1(target: u32) -> i32 {
let mut x = MAX;
let mut y = MAX;
let mut n = 1u32;
let mut m = 1u32;
for _ in 1..MAX {
m += 2;
x += 1;
y += 1;
if let Some(result) = strip(m, target, &mut n, &mut x, &mut y, 0, -1) {
return result;
}
if let Some(result) = strip(m, target, &mut n, &mut x, &mut y, -1, 0) {
return result;
}
if let Some(result) = strip(m, target, &mut n, &mut x, &mut y, 0, 1) {
return result;
}
if let Some(result) = strip(m, target, &mut n, &mut x, &mut y, 1, 0) {
return result;
}
}
-1
}
fn calcsum(v: &mut Vec<Vec<u32>>, x: usize, y: usize) -> u32 {
v[y - 1][x - 1] + v[y - 1][x] + v[y - 1][x + 1] + v[y][x - 1] + v[y][x + 1] + v[y + 1][x - 1]
+ v[y + 1][x] + v[y + 1][x + 1]
}
fn task2(target: u32) -> u32 {
let mut v = vec![vec![0u32; (MAX + MAX + 1) as usize]; (MAX + MAX + 1) as usize];
let mut x = MAX;
let mut y = MAX;
v[y as usize][x as usize] = 1;
let mut m = 1;
'outer: for _ in 1..MAX {
m += 2;
x += 1;
y += 1;
for _ in 1..m {
y -= 1;
let sum = calcsum(&mut v, x as usize, y as usize);
if sum > target {
return sum;
}
v[y as usize][x as usize] = sum;
}
for _ in 1..m {
x -= 1;
let sum = calcsum(&mut v, x as usize, y as usize);
if sum > target {
return sum;
}
v[y as usize][x as usize] = sum;
}
for _ in 1..m {
y += 1;
let sum = calcsum(&mut v, x as usize, y as usize);
if sum > target {
return sum;
}
v[y as usize][x as usize] = sum;
}
for _ in 1..m {
x += 1;
let sum = calcsum(&mut v, x as usize, y as usize);
if sum > target {
return sum;
}
v[y as usize][x as usize] = sum;
}
}
0
}
fn main() {
let day3 = 289326u32;
println!("{} {}", task1(day3), task2(day3));
}