This commit is contained in:
2016-12-21 14:32:08 +01:00
parent b0e30b1726
commit 34f5a96e1e
3 changed files with 111 additions and 0 deletions

4
2016/18/Cargo.lock generated Normal file
View File

@@ -0,0 +1,4 @@
[root]
name = "18"
version = "0.1.0"

6
2016/18/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "18"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]

101
2016/18/src/main.rs Normal file
View File

@@ -0,0 +1,101 @@
fn is_safe(l: char, c: char, r: char) -> bool {
// Rule #1.
if l == '^' && c == '^' && r == '.' {
return false;
}
// Rule #2.
if l == '.' && c == '^' && r == '^' {
return false;
}
// Rule #3.
if l == '^' && c == '.' && r == '.' {
return false;
}
// Rule #4.
if l == '.' && c == '.' && r == '^' {
return false;
}
true
}
pub fn evaluate(input: &str) -> String {
let input = input.chars().collect::<Vec<char>>();
let mut output = String::new();
for i in 0..input.len() {
let c = if i == 0 {
if is_safe('.', input[i], input[i + 1]) {
'.'
} else {
'^'
}
} else if i == input.len() - 1 {
if is_safe(input[i - 1], input[i], '.') {
'.'
} else {
'^'
}
} else {
if is_safe(input[i - 1], input[i], input[i + 1]) {
'.'
} else {
'^'
}
};
output.push(c);
}
output
}
fn main() {
let mut map = Vec::new();
map.push(".^^^^^.^^.^^^.^...^..^^.^.^..^^^^^^^^^^..^...^^.^..^^^^..^^^^...^.^.^^^^^^^^....^..^^^^^^.^^^.^^^.^^".to_owned());
while map.len() < 400000 {
let next = {
let last = map.last().unwrap();
evaluate(last)
};
map.push(next);
}
let count: usize = map.iter()
.map(|row| {
row.chars()
.filter(|c| *c == '.')
.count()
})
.sum();
println!("safe_tiles={}", count);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_01() {
assert_eq!(".^^^^".to_owned(), evaluate("..^^."));
}
#[test]
fn example_02() {
assert_eq!("^^..^".to_owned(), evaluate(".^^^^"));
}
#[test]
fn example_03() {
assert_eq!("^^^...^..^".to_owned(), evaluate(".^^.^.^^^^"));
}
}