generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.rs
179 lines (148 loc) · 4.36 KB
/
10.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
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
169
170
171
172
173
174
175
176
177
178
179
advent_of_code::solution!(10);
use advent_of_code::util::list::Array2D;
enum Direction {
Left,
Right,
Up,
Down,
}
struct Grid {
data: Array2D<Vec<Direction>>,
len_x: usize,
len_y: usize,
}
struct State {
grid: Grid,
start_position: (usize, usize),
}
fn parse_data(input: &str) -> State {
let len_x = input.lines().next().unwrap().len();
let len_y = input.lines().count();
let start_position = input
.lines()
.enumerate()
.filter_map(|(y, line)| line.chars().position(|v| v == 'S').map(|x| (x, y)))
.next()
.unwrap();
let mut grid = Array2D::new(len_x);
input
.lines()
.map(|line| {
line.as_bytes().iter().map(|v| match v {
b'|' => vec![Direction::Down, Direction::Up],
b'-' => vec![Direction::Left, Direction::Right],
b'L' => vec![Direction::Up, Direction::Right],
b'J' => vec![Direction::Left, Direction::Up],
b'7' => vec![Direction::Left, Direction::Down],
b'F' => vec![Direction::Right, Direction::Down],
_ => vec![],
})
})
.for_each(|line| grid.add_line(line));
State {
grid: Grid {
data: grid,
len_x,
len_y,
},
start_position,
}
}
fn get_next_position(
grid: &Grid,
position: &(usize, usize),
direction: &Direction,
) -> Option<(usize, usize)> {
let result = match direction {
Direction::Left => (position.0.wrapping_sub(1), position.1),
Direction::Right => (position.0 + 1, position.1),
Direction::Up => (position.0, position.1.wrapping_sub(1)),
Direction::Down => (position.0, position.1 + 1),
};
if (0..grid.len_x).contains(&result.0) && (0..grid.len_y).contains(&result.1) {
Some(result)
} else {
None
}
}
fn part_x(
grid: &Grid,
start_position: (usize, usize),
start_direction: Direction,
) -> Option<Vec<(usize, usize)>> {
let mut result = vec![start_position];
if let Some(new_position) = get_next_position(grid, &start_position, &start_direction) {
result.push(new_position);
} else {
return None;
}
loop {
let prev_position = &result[result.len() - 2];
let position = &result[result.len() - 1];
let new_possible_position = grid.data[position]
.iter()
.filter_map(|d| get_next_position(grid, position, d))
.find(|next_position| next_position != prev_position);
if let Some(new_position) = new_possible_position {
result.push(new_position);
if new_position == start_position {
return Some(result);
}
} else {
return None;
}
}
}
const STARTING_DIRECTIONS: [Direction; 4] = [
Direction::Left,
Direction::Right,
Direction::Up,
Direction::Down,
];
pub fn part_one(input: &str) -> Option<u32> {
let state = parse_data(input);
let best_path = STARTING_DIRECTIONS
.into_iter()
.filter_map(|d| part_x(&state.grid, state.start_position, d))
.max_by(|a, b| a.len().cmp(&b.len()))
.unwrap();
let result = best_path.len() as u32 / 2;
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let state = parse_data(input);
let best_path = STARTING_DIRECTIONS
.into_iter()
.filter_map(|d| part_x(&state.grid, state.start_position, d))
.max_by(|a, b| a.len().cmp(&b.len()))
.unwrap();
// Shoelace formula
let area_twice = best_path
.windows(2)
.map(|w| (w[0].1 as i32 + w[1].1 as i32) * (w[0].0 as i32 - w[1].0 as i32))
.sum::<i32>()
.unsigned_abs();
// Pick's theorem
let boundaries = best_path.len() as u32 - 1;
let interior_points = (area_twice / 2) - boundaries / 2 + 1;
let result = interior_points;
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file_part(
"examples", DAY, 1,
));
assert_eq!(result, Some(8));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(10));
}
}