Skip to content

Instantly share code, notes, and snippets.

@ConnorBaker
Last active May 25, 2020 23:46
Show Gist options
  • Select an option

  • Save ConnorBaker/d0f67d1fea6fe4f2355df1fb9d2f12d4 to your computer and use it in GitHub Desktop.

Select an option

Save ConnorBaker/d0f67d1fea6fe4f2355df1fb9d2f12d4 to your computer and use it in GitHub Desktop.
Solution to https://open.kattis.com/problems/substitution (fails second secret test case)
use std::io::{self, BufRead};
fn main() {
// Get the input as an iterator
// Transforms each line of input, a string containing a sequence of numbers
// (indexed from one) into a vector of i64, all indexed from zero
let stdin = io::stdin();
let is =
stdin
.lock()
.lines()
.map(|l| l
.unwrap() // Input is well-formed, no need for exception handling
.split_whitespace()
.map(|num_as_str| num_as_str.parse().unwrap())
.map(|num: i64| num - 1) // zero-index the numbers
.collect() // collect to a vector
);
// Skip the number of test cases
let mut it = is.into_iter().skip(1);
// Let binding allows us to skip the first line of the test case while also ensuring
// that we have more test cases to process -- nice!
while let Some(_) = it.next() {
let mut num_cycles: i32 = 0;
let mut ms: Vec<i64> = it.next().unwrap();
let cs: Vec<i64> = it.next().unwrap();
let ps: Vec<i64> = it.next().unwrap();
while !ms.eq(&cs) {
for i in 0..ms.len() {
ms[i] = ps[ms[i] as usize]
}
num_cycles += 1
}
println!("{:?}", num_cycles)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment