Clean up day 9 solution a bit more

This commit is contained in:
Wesley Moore 2017-12-13 07:02:06 +11:00
parent 93fe95beba
commit 183bbd50ca

View file

@ -11,7 +11,9 @@ enum Flags {
} }
#[derive(Debug)] #[derive(Debug)]
struct State { pub struct State {
pub score: usize,
pub garbage_count: usize,
group_depth: usize, group_depth: usize,
flags: Flags, flags: Flags,
} }
@ -21,38 +23,34 @@ fn main() {
let reader = BufReader::new(file); let reader = BufReader::new(file);
let (score, garbage_count) = stream(reader); let (score, garbage_count) = stream(reader);
println!("{}", score); println!("Part 1 (score): {}", score);
println!("{}", garbage_count); println!("Part 2 (garbage count): {}", garbage_count);
} }
fn stream<R>(reader: R) -> (usize, usize) where R: Read { fn stream<R>(reader: R) -> (usize, usize) where R: Read {
let mut state = State { group_depth: 0, flags: Flags::Normal }; let state = reader.bytes()
let mut score = 0; .map(|byte| byte.ok().and_then(|b| char::from_u32(b as u32)))
let mut garbage_count = 0; .fold(State { group_depth: 0, score: 0, garbage_count: 0, flags: Flags::Normal }, next_state);
for byte in reader.bytes() { (state.score, state.garbage_count)
state = match (state.flags, byte.ok().and_then(|b| char::from_u32(b as u32))) { }
(Flags::CancelNext, _) => State { group_depth: state.group_depth, flags: Flags::Normal },
(Flags::CancelNextInGarbage, _) => State { group_depth: state.group_depth, flags: Flags::InGarbage }, fn next_state(state: State, chr: Option<char>) -> State {
(Flags::Normal, Some('{')) => State { group_depth: state.group_depth + 1, flags: Flags::Normal }, use Flags::*;
(Flags::Normal, Some('}')) => {
score += state.group_depth; match (state.flags, chr) {
State { group_depth: state.group_depth - 1, flags: Flags::Normal } (CancelNext, _) => State { score: state.score, garbage_count: state.garbage_count, group_depth: state.group_depth, flags: Normal },
}, (CancelNextInGarbage, _) => State { score: state.score, garbage_count: state.garbage_count, group_depth: state.group_depth, flags: InGarbage },
(Flags::Normal, Some('<')) => State { group_depth: state.group_depth, flags: Flags::InGarbage }, (Normal, Some('{')) => State { score: state.score, garbage_count: state.garbage_count, group_depth: state.group_depth + 1, flags: Normal },
(Flags::InGarbage, Some('>')) => State { group_depth: state.group_depth, flags: Flags::Normal }, (Normal, Some('}')) => State { score: state.score + state.group_depth, garbage_count: state.garbage_count, group_depth: state.group_depth - 1, flags: Normal },
(Flags::InGarbage, Some('!')) => State { group_depth: state.group_depth, flags: Flags::CancelNextInGarbage }, (Normal, Some('<')) => State { score: state.score, garbage_count: state.garbage_count, group_depth: state.group_depth, flags: InGarbage },
(Flags::InGarbage, Some(_)) => { (InGarbage, Some('>')) => State { score: state.score, garbage_count: state.garbage_count, group_depth: state.group_depth, flags: Normal },
garbage_count += 1; (InGarbage, Some('!')) => State { score: state.score, garbage_count: state.garbage_count, group_depth: state.group_depth, flags: CancelNextInGarbage },
State { group_depth: state.group_depth, flags: Flags::InGarbage } (InGarbage, Some(_)) => State { score: state.score, garbage_count: state.garbage_count + 1, group_depth: state.group_depth, flags: InGarbage },
}, (Normal, Some('!')) => State { score: state.score, garbage_count: state.garbage_count, group_depth: state.group_depth, flags: CancelNext },
(Flags::Normal, Some('!')) => State { group_depth: state.group_depth, flags: Flags::CancelNext }, (_, Some(_)) => state,
(_, Some(_)) => state, (_, None) => panic!("error reading/converting byte")
(_, None) => panic!("error reading/converting byte")
};
} }
(score, garbage_count)
} }
// {}, score of 1. // {}, score of 1.