mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 13:20:32 +01:00
Make day 12 even more rusty.
This commit is contained in:
@@ -22,21 +22,13 @@ fn char_bool(c: char) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_state(state: &State) -> String {
|
fn print_state(state: &State) -> String {
|
||||||
let mut buf = String::with_capacity(state.len());
|
state.iter().map(|(_, x)| if *x { '#' } else { '.' }).collect()
|
||||||
for &(_, b) in state {
|
|
||||||
if b {
|
|
||||||
buf.push('#');
|
|
||||||
} else {
|
|
||||||
buf.push('.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buf
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn state_from_string(representation: &str, offset: i64) -> State {
|
fn state_from_string(representation: &str, offset: i64) -> State {
|
||||||
Vec::from_iter(representation.chars()
|
representation.chars().enumerate()
|
||||||
.enumerate()
|
.map(|(i, c)| (i as i64 + offset, char_bool(c)))
|
||||||
.map(|(i, c)| (i as i64 + offset, char_bool(c))))
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Day12 {
|
impl Day12 {
|
||||||
@@ -47,11 +39,9 @@ impl Day12 {
|
|||||||
fn read_input(&mut self, input: &mut Read) -> State {
|
fn read_input(&mut self, input: &mut Read) -> State {
|
||||||
let state;
|
let state;
|
||||||
let mut reader = BufReader::new(input);
|
let mut reader = BufReader::new(input);
|
||||||
{
|
let mut line = String::new();
|
||||||
let mut line = String::new();
|
reader.read_line(&mut line).unwrap();
|
||||||
reader.read_line(&mut line).unwrap();
|
state = state_from_string(&line["initial state:".len()..line.len()].trim(), 0);
|
||||||
state = state_from_string(&line["initial state:".len()..line.len()].trim(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
let line = line.unwrap();
|
let line = line.unwrap();
|
||||||
@@ -62,10 +52,7 @@ impl Day12 {
|
|||||||
|
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
for c in line.chars().take(5) {
|
for c in line.chars().take(5) {
|
||||||
index <<= 1;
|
index = (index << 1) | char_bool(c) as usize;
|
||||||
if char_bool(c) {
|
|
||||||
index |= 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.productions[index] = char_bool(line.chars().last().unwrap());
|
self.productions[index] = char_bool(line.chars().last().unwrap());
|
||||||
|
|||||||
Reference in New Issue
Block a user