day8: added
This commit is contained in:
parent
503bbd176a
commit
10a38b6f3b
|
@ -1,54 +1,64 @@
|
|||
#![feature(byte_slice_trim_ascii)]
|
||||
#![feature(slice_split_once)]
|
||||
#![feature(test)]
|
||||
|
||||
use std::{cmp::Ordering, collections::HashMap};
|
||||
use std::collections::HashMap;
|
||||
|
||||
extern crate test;
|
||||
|
||||
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
||||
const INPUTS: [&[u8]; 2] = [
|
||||
// RL
|
||||
//
|
||||
// AAA = (BBB, CCC)
|
||||
// BBB = (DDD, EEE)
|
||||
// CCC = (ZZZ, GGG)
|
||||
// DDD = (DDD, DDD)
|
||||
// EEE = (EEE, EEE)
|
||||
// GGG = (GGG, GGG)
|
||||
// ZZZ = (ZZZ, ZZZ)
|
||||
&[
|
||||
82, 76, 10, 10, 65, 65, 65, 32, 61, 32, 40, 66, 66, 66, 44, 32, 67, 67, 67, 41, 10, 66, 66,
|
||||
66, 32, 61, 32, 40, 68, 68, 68, 44, 32, 69, 69, 69, 41, 10, 67, 67, 67, 32, 61, 32, 40, 90,
|
||||
90, 90, 44, 32, 71, 71, 71, 41, 10, 68, 68, 68, 32, 61, 32, 40, 68, 68, 68, 44, 32, 68, 68,
|
||||
68, 41, 10, 69, 69, 69, 32, 61, 32, 40, 69, 69, 69, 44, 32, 69, 69, 69, 41, 10, 71, 71, 71,
|
||||
32, 61, 32, 40, 71, 71, 71, 44, 32, 71, 71, 71, 41, 10, 90, 90, 90, 32, 61, 32, 40, 90, 90,
|
||||
90, 44, 32, 90, 90, 90, 41,
|
||||
],
|
||||
include_bytes!("./input.txt"),
|
||||
];
|
||||
|
||||
fn process(data: &str) -> usize {
|
||||
let mut answer = 0;
|
||||
|
||||
let mut data = data.split("\n\n");
|
||||
|
||||
let seq: Vec<char> = data.next().map(|x| x.chars().collect()).unwrap();
|
||||
fn process(data: &[u8]) -> usize {
|
||||
let (seq, remain) = data.split_once(|&x| x == b'\n').unwrap();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for line in data.next().unwrap().lines() {
|
||||
let (start, remain) = line.split_once(" = ").unwrap();
|
||||
|
||||
let (l, r) = remain.split_once(',').unwrap();
|
||||
let l: String = l.chars().filter(|x| x.is_ascii_alphabetic()).collect();
|
||||
let r: String = r.chars().filter(|x| x.is_ascii_alphabetic()).collect();
|
||||
for line in remain.split(|&x| x == b'\n').skip(1) {
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let (start, remain) = line.split_at(3);
|
||||
let (l, r) = (&remain[4..7], &remain[9..12]);
|
||||
|
||||
map.insert(start, (l, r));
|
||||
}
|
||||
|
||||
let mut i = 0;
|
||||
let mut pos = "AAA";
|
||||
let mut pos: &[u8] = &[b'A', b'A', b'A'];
|
||||
|
||||
loop {
|
||||
let step = seq[i % seq.len()];
|
||||
if pos == "ZZZ" {
|
||||
return answer;
|
||||
for (i, ins) in seq.iter().cycle().enumerate() {
|
||||
if pos == [b'Z', b'Z', b'Z'] {
|
||||
return i;
|
||||
}
|
||||
|
||||
let (l, r) = map.get(&pos).unwrap();
|
||||
|
||||
println!("pos = {} l = {} r = {}", pos, l, r);
|
||||
match step {
|
||||
'R' => pos = r,
|
||||
'L' => pos = l,
|
||||
_ => unreachable!(),
|
||||
match ins {
|
||||
b'L' => pos = l,
|
||||
b'R' => pos = r,
|
||||
|
||||
_ => (),
|
||||
}
|
||||
|
||||
answer += 1;
|
||||
|
||||
i += 1;
|
||||
}
|
||||
answer
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -1,70 +1,82 @@
|
|||
#![feature(byte_slice_trim_ascii)]
|
||||
#![feature(slice_split_once)]
|
||||
#![feature(test)]
|
||||
|
||||
use std::{cmp::Ordering, collections::HashMap};
|
||||
use std::collections::HashMap;
|
||||
|
||||
extern crate test;
|
||||
|
||||
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
||||
const INPUTS: [&[u8]; 2] = [
|
||||
include_bytes!("./sample.txt"),
|
||||
include_bytes!("./input.txt"),
|
||||
];
|
||||
|
||||
fn process(data: &str) -> usize {
|
||||
let mut answer = 0;
|
||||
|
||||
let mut data = data.split("\n\n");
|
||||
|
||||
let seq: Vec<char> = data.next().map(|x| x.chars().collect()).unwrap();
|
||||
fn process(data: &[u8]) -> usize {
|
||||
let (seq, remain) = data.split_once(|&x| x == b'\n').unwrap();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for line in data.next().unwrap().lines() {
|
||||
let (start, remain) = line.split_once(" = ").unwrap();
|
||||
|
||||
let (l, r) = remain.split_once(',').unwrap();
|
||||
let l: String = l.chars().filter(|x| x.is_ascii_alphabetic()).collect();
|
||||
let r: String = r.chars().filter(|x| x.is_ascii_alphabetic()).collect();
|
||||
for line in remain.split(|&x| x == b'\n').skip(1) {
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let (start, remain) = line.split_at(3);
|
||||
let (l, r) = (&remain[4..7], &remain[9..12]);
|
||||
|
||||
map.insert(start, (l, r));
|
||||
}
|
||||
|
||||
let mut i = 0;
|
||||
let mut pos = "AAA";
|
||||
let mut a_set = vec![];
|
||||
|
||||
let mut stack = vec![];
|
||||
for &k in map
|
||||
.keys()
|
||||
.filter(|x| x.last().map_or(false, |&b| b == b'A'))
|
||||
{
|
||||
a_set.push(k);
|
||||
}
|
||||
|
||||
for k in map.keys() {
|
||||
if k.ends_with('A') {
|
||||
stack.push((k.clone(), seq[0]));
|
||||
let mut tmp = Vec::with_capacity(a_set.len());
|
||||
|
||||
'outer: for mut node in a_set {
|
||||
for (i, ins) in seq.iter().cycle().enumerate() {
|
||||
if node.last().map_or(false, |&b| b == b'Z') {
|
||||
tmp.push(i);
|
||||
continue 'outer;
|
||||
}
|
||||
|
||||
let (l, r) = map.get(node).unwrap();
|
||||
|
||||
match ins {
|
||||
b'L' => node = l,
|
||||
b'R' => node = r,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{:?}", stack);
|
||||
lcm(&tmp)
|
||||
}
|
||||
|
||||
fn lcm(a: &[usize]) -> usize {
|
||||
let mut answer = a[0];
|
||||
|
||||
for &num in a.iter().skip(1) {
|
||||
answer = (num * answer) / (gcd(num, answer));
|
||||
}
|
||||
|
||||
// loop {
|
||||
// let step = seq[i % seq.len()];
|
||||
// if pos == "ZZZ" {
|
||||
// return answer;
|
||||
// }
|
||||
//
|
||||
// let (l, r) = map.get(&pos).unwrap();
|
||||
//
|
||||
// println!("pos = {} l = {} r = {}", pos, l, r);
|
||||
// match step {
|
||||
// 'R' => pos = r,
|
||||
// 'L' => pos = l,
|
||||
// _ => unreachable!(),
|
||||
// }
|
||||
//
|
||||
// answer += 1;
|
||||
//
|
||||
// i += 1;
|
||||
// }
|
||||
answer
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn gcd(a: usize, b: usize) -> usize {
|
||||
if b == 0 {
|
||||
return a;
|
||||
}
|
||||
gcd(b, a % b)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for input in INPUTS.iter() {
|
||||
println!("total = {}", process(input));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
RL
|
||||
LR
|
||||
|
||||
AAA = (BBB, CCC)
|
||||
BBB = (DDD, EEE)
|
||||
CCC = (ZZZ, GGG)
|
||||
DDD = (DDD, DDD)
|
||||
EEE = (EEE, EEE)
|
||||
GGG = (GGG, GGG)
|
||||
ZZZ = (ZZZ, ZZZ)
|
||||
11A = (11B, XXX)
|
||||
11B = (XXX, 11Z)
|
||||
11Z = (11B, XXX)
|
||||
22A = (22B, XXX)
|
||||
22B = (22C, 22C)
|
||||
22C = (22Z, 22Z)
|
||||
22Z = (22B, 22B)
|
||||
XXX = (XXX, XXX)
|
Loading…
Reference in New Issue
Block a user