operating on numeric ids instead of strings
This commit is contained in:
parent
9091376a90
commit
8b7587890d
@ -1,14 +1,13 @@
|
|||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use std::collections::HashMap;
|
|
||||||
use num::integer::lcm;
|
use num::integer::lcm;
|
||||||
|
|
||||||
fn get_steps_to(directions: &str, nodes: &HashMap<&str, (&str, &str)>,
|
fn get_steps_to<F: Fn(u16) -> bool>(directions: &str, nodes: &Vec<(u16, u16)>,
|
||||||
start: &str, stop: fn(&str) -> bool) -> u64 {
|
start: u16, stop: F) -> u64 {
|
||||||
let mut current_node = start;
|
let mut current_node = start;
|
||||||
let mut counter: u64 = 0;
|
let mut counter: u64 = 0;
|
||||||
'outer: loop {
|
'outer: loop {
|
||||||
for c in directions.bytes() {
|
for c in directions.bytes() {
|
||||||
let (left, right) = nodes[current_node];
|
let (left, right) = nodes[current_node as usize];
|
||||||
match c {
|
match c {
|
||||||
b'L' => current_node = left,
|
b'L' => current_node = left,
|
||||||
b'R' => current_node = right,
|
b'R' => current_node = right,
|
||||||
@ -23,22 +22,36 @@ fn get_steps_to(directions: &str, nodes: &HashMap<&str, (&str, &str)>,
|
|||||||
counter
|
counter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn str_to_id(str: &str) -> u16 {
|
||||||
|
let mut value: u16 = 0;
|
||||||
|
for c in str.bytes() {
|
||||||
|
value = value * 26 + (c - b'A') as u16;
|
||||||
|
}
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let lines_str = read_to_string("input.txt").unwrap();
|
let lines_str = read_to_string("input.txt").unwrap();
|
||||||
let mut lines_iter = lines_str.lines();
|
let mut lines_iter = lines_str.lines();
|
||||||
let directions = lines_iter.next().unwrap();
|
let directions = lines_iter.next().unwrap();
|
||||||
lines_iter.next();
|
lines_iter.next();
|
||||||
let nodes = lines_iter.map(|line| {
|
let mut nodes: Vec<(u16, u16)> = vec![(0, 0); 17576];
|
||||||
|
let mut ids_ending_with_a: Vec<u16> = Vec::new();
|
||||||
|
for line in lines_iter {
|
||||||
let mut ids = line.split([' ', '(', ')', '=', ',']).filter(|&str| !str.is_empty());
|
let mut ids = line.split([' ', '(', ')', '=', ',']).filter(|&str| !str.is_empty());
|
||||||
(ids.next().unwrap(), (ids.next().unwrap(), ids.next().unwrap()))
|
let node_id = str_to_id(ids.next().unwrap());
|
||||||
}).collect::<HashMap<_, _>>();
|
nodes[node_id as usize] = (str_to_id(ids.next().unwrap()), str_to_id(ids.next().unwrap()));
|
||||||
let counter1 = get_steps_to(directions, &nodes, "AAA", |str| str == "ZZZ");
|
if node_id % 26 == 0 {
|
||||||
let counter2 = nodes.iter()
|
ids_ending_with_a.push(node_id);
|
||||||
.filter_map(|(&id, _)|
|
}
|
||||||
Some(id).filter(|&id| id.as_bytes()[2 as usize] == b'A'))
|
}
|
||||||
.map(|node| get_steps_to(
|
let id_aaa = str_to_id("AAA");
|
||||||
directions, &nodes, node, |str| str.as_bytes()[2 as usize] == b'Z'))
|
let id_zzz = str_to_id("ZZZ");
|
||||||
.fold(1 as u64, |acc, counter| lcm(acc, counter));
|
let counter1 = get_steps_to(directions, &nodes, id_aaa, |id| id == id_zzz);
|
||||||
println!("{}", counter1);
|
let counter2 = ids_ending_with_a.iter()
|
||||||
println!("{}", counter2);
|
.map(|&node| get_steps_to(
|
||||||
|
directions, &nodes, node, |id| id % 26 == 25))
|
||||||
|
.fold(1 as u64, |acc, counter| lcm(acc, counter));
|
||||||
|
println!("Counter1: {}", counter1);
|
||||||
|
println!("Counter2: {}", counter2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user