-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_03.rs
140 lines (130 loc) · 3.39 KB
/
day_03.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
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
//! https://adventofcode.com/2017/day/3
//! https://adventofcode.com/2017/day/3/input
use std::{fs::read_to_string, time::Instant};
fn parse(input: &str) -> isize {
input.parse().unwrap()
}
pub mod part1 {
use super::parse;
pub fn solve(input: &str) -> usize {
let target = parse(input);
let mut accumulated = 1;
let mut size = 1;
while accumulated < target {
size += 2;
accumulated += size * 4 - 4;
}
let layer = size / 2;
let last = (size - 2) * (size - 2);
let (mut x, mut y) = (layer, layer - 1);
let mut n = last + 1;
while y >= -layer {
if n == target {
return x.unsigned_abs() + y.unsigned_abs();
}
y -= 1;
n += 1;
}
y += 1;
x -= 1;
while x >= -layer {
if n == target {
return x.unsigned_abs() + y.unsigned_abs();
}
x -= 1;
n += 1;
}
x += 1;
y += 1;
while y <= layer {
if n == target {
return x.unsigned_abs() + y.unsigned_abs();
}
y += 1;
n += 1;
}
y -= 1;
x += 1;
while x < layer {
if n == target {
return x.unsigned_abs() + y.unsigned_abs();
}
x += 1;
n += 1;
}
unreachable!()
}
}
pub mod part2 {
use std::collections::HashMap;
use super::parse;
const NEIGHBORS: [(isize, isize); 8] = [
(1, 0),
(1, 1),
(0, 1),
(-1, 1),
(-1, 0),
(-1, -1),
(0, -1),
(1, -1),
];
fn sum_neighbors((x, y): (isize, isize), cells: &HashMap<(isize, isize), isize>) -> isize {
let mut sum = 0;
for neighbor in NEIGHBORS.map(|(nx, ny)| (x + nx, y + ny)) {
if let Some(&n) = cells.get(&neighbor) {
sum += n;
}
}
sum
}
pub fn solve(input: &str) -> isize {
let target = parse(input);
let mut cells = HashMap::from([((0, 0), 1)]);
let (mut x, mut y) = (1, 0);
let mut layer = 1;
let (mut dx, mut dy) = (0, -1);
loop {
let mut out = false;
if y < -layer {
y += 1;
out = true;
}
if x < -layer {
x += 1;
out = true;
}
if y > layer {
y -= 1;
out = true;
}
if x > layer {
layer += 1;
out = true;
}
if out {
(dx, dy) = (dy, -dx);
}
let n = sum_neighbors((x, y), &cells);
if n > target {
return n;
}
cells.insert((x, y), n);
x += dx;
y += dy;
}
}
}
pub fn main(test: bool) {
let test_input = "747".to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2017/day_03_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}