2017/08-09
This commit is contained in:
4
2017/09/Cargo.lock
generated
Normal file
4
2017/09/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day-09"
|
||||
version = "0.1.0"
|
||||
|
||||
6
2017/09/Cargo.toml
Normal file
6
2017/09/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day-09"
|
||||
version = "0.1.0"
|
||||
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
1
2017/09/input.txt
Normal file
1
2017/09/input.txt
Normal file
File diff suppressed because one or more lines are too long
105
2017/09/src/main.rs
Normal file
105
2017/09/src/main.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use std::io::{self, Read};
|
||||
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum State {
|
||||
Group,
|
||||
Garbage,
|
||||
Ignore,
|
||||
}
|
||||
|
||||
|
||||
fn scan(input: &str) -> (usize, usize) {
|
||||
let mut score = 0;
|
||||
let mut garbage = 0;
|
||||
|
||||
let mut indendation = 0;
|
||||
|
||||
let mut states = Vec::new();
|
||||
|
||||
for ch in input.chars() {
|
||||
let state = *states.last().unwrap_or(&State::Group);
|
||||
|
||||
match (state, ch) {
|
||||
// Group
|
||||
(State::Group, '{') => {
|
||||
indendation += 1;
|
||||
}
|
||||
(State::Group, '}') => {
|
||||
score += indendation;
|
||||
indendation -= 1;
|
||||
|
||||
states.pop();
|
||||
}
|
||||
(State::Group, '<') => {
|
||||
states.push(State::Garbage);
|
||||
}
|
||||
(State::Group, '!') => {
|
||||
states.push(State::Ignore);
|
||||
}
|
||||
|
||||
// Garbage
|
||||
(State::Garbage, '>') => {
|
||||
states.pop();
|
||||
}
|
||||
(State::Garbage, '!') => {
|
||||
states.push(State::Ignore);
|
||||
}
|
||||
(State::Garbage, _) => {
|
||||
garbage += 1;
|
||||
}
|
||||
|
||||
// Ignore
|
||||
(State::Ignore, _) => {
|
||||
states.pop();
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
(score, garbage)
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_to_string(&mut input)
|
||||
.expect("faild to read input");
|
||||
|
||||
let (score, garbage) = scan(&input);
|
||||
|
||||
println!("part_one={}", score);
|
||||
println!("part_two={}", garbage);
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_score() {
|
||||
assert_eq!(scan("{}"), (1, 0));
|
||||
assert_eq!(scan("{{{}}}"), (6, 0));
|
||||
assert_eq!(scan("{{},{}}"), (5, 0));
|
||||
assert_eq!(scan("{{{},{},{{}}}}"), (16, 0));
|
||||
assert_eq!(scan("{<a>,<a>,<a>,<a>}"), (1, 4));
|
||||
assert_eq!(scan("{{<ab>},{<ab>},{<ab>},{<ab>}}"), (9, 8));
|
||||
assert_eq!(scan("{{<!!>},{<!!>},{<!!>},{<!!>}}"), (9, 0));
|
||||
assert_eq!(scan("{{<a!>},{<a!>},{<a!>},{<ab>}}"), (3, 17));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_garbage() {
|
||||
assert_eq!(scan("<>"), (0, 0));
|
||||
assert_eq!(scan("<random characters>"), (0, 17));
|
||||
assert_eq!(scan("<<<<>"), (0, 3));
|
||||
assert_eq!(scan("<{!>}>"), (0, 2));
|
||||
assert_eq!(scan("<!!>"), (0, 0));
|
||||
assert_eq!(scan("<!!!>>"), (0, 0));
|
||||
assert_eq!(scan(r#"<{o"i!a,<{i<a>"#), (0, 10));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user