-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rs
51 lines (40 loc) · 1.58 KB
/
day18.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
use std::fs;
pub(crate) fn day18() {
println!("{}", solve(fs::read_to_string("input/2016/day18/input.txt").unwrap(), 40));
println!("{}", solve(fs::read_to_string("input/2016/day18/input.txt").unwrap(), 400000));
}
fn solve(input: String, total_rows: usize) -> usize {
let mut ans = 0;
let mut prev_row: Vec<char> = input.chars().collect();
ans += prev_row.iter().filter(|c| **c == '.').count();
for _ in 1..total_rows {
let mut new_row = vec!();
for i in 0..prev_row.len() {
let left = if i > 0 { prev_row[i - 1] } else { '.' };
let center = prev_row[i];
let right = if i < prev_row.len() - 1 { prev_row[i + 1] } else { '.' };
let is_trap = (left == '^' && center == '^' && right == '.')
|| (left == '.' && center == '^' && right == '^')
|| (left == '^' && center == '.' && right == '.')
|| (left == '.' && center == '.' && right == '^');
new_row.push(if is_trap { '^' } else { '.' });
}
ans += new_row.iter().filter(|c| **c == '.').count();
prev_row = new_row
}
ans
}
#[cfg(test)]
mod day18_tests {
use std::fs;
use crate::y2016::day18::{solve};
#[test]
fn test_works() {
assert_eq!(38, solve(fs::read_to_string("input/2016/day18/test.txt").unwrap(), 10));
}
#[test]
fn input_works() {
assert_eq!(2005, solve(fs::read_to_string("input/2016/day18/input.txt").unwrap(), 40));
assert_eq!(20008491, solve(fs::read_to_string("input/2016/day18/input.txt").unwrap(), 400000));
}
}