[2018][rust][14.2]

sorbet
Alpha Chen 6 years ago
parent ad57cc604e
commit c911c4d72a

@ -4,40 +4,63 @@ use advent_of_code::main;
main!(); main!();
fn solve(_input: &str) -> Result<String, Box<Error>> { fn solve(input: &str) -> Result<String, Box<Error>> {
let elves = vec![0, 1]; let elves = vec![0, 1];
let recipes = vec![3, 7]; let recipes = "37".into();
let mut scoreboard = Scoreboard { elves, recipes }; let scoreboard = Scoreboard { elves, recipes };
let needle = input;
// let recipes = scoreboard.find(|x| x.len() > n + 9).unwrap();
// let score: String = recipes[n..n + 10].iter().map(|&x| x.to_string()).collect();
let n = scoreboard
.scan(0, |n, recipes| {
let seen = *n;
*n = recipes
.len()
.checked_sub(needle.len() - 1)
.unwrap_or_else(|| 0);
Some((seen, recipes[seen..recipes.len()].to_string()))
})
// .inspect(|x| println!("{:?}", x))
.flat_map(|(n, x)| x.find(needle).map(|i| n + i))
.next()
.unwrap();
let n = 919_901; Ok(n.to_string())
let recipes = scoreboard.find(|x| x.len() > n + 9).unwrap(); }
let score: String = recipes[n..n + 10].iter().map(|&x| x.to_string()).collect();
Ok(score) #[test]
fn test_solve() {
// Part Two
assert_eq!(&solve("51589").unwrap(), "9");
assert_eq!(&solve("01245").unwrap(), "5");
assert_eq!(&solve("92510").unwrap(), "18");
assert_eq!(&solve("59414").unwrap(), "2018");
} }
#[derive(Debug)] #[derive(Debug)]
struct Scoreboard { struct Scoreboard {
elves: Vec<usize>, elves: Vec<usize>,
recipes: Vec<usize>, recipes: String,
} }
impl Iterator for Scoreboard { impl Iterator for Scoreboard {
type Item = Vec<usize>; type Item = String;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.elves self.recipes.push_str(
&self
.elves
.iter() .iter()
.map(|&x| self.recipes[x]) .map(|&x| self.recipes[x..=x].parse::<usize>().unwrap())
.sum::<usize>() .sum::<usize>()
.to_string() .to_string(),
.chars() );
.map(|x| x.to_digit(10).unwrap() as usize)
.for_each(|x| self.recipes.push(x));
self.elves = self self.elves = self
.elves .elves
.iter() .iter()
.map(|&x| (1 + self.recipes[x] + x) % self.recipes.len()) .map(|&x| (1 + self.recipes[x..=x].parse::<usize>().unwrap() + x) % self.recipes.len())
.collect(); .collect();
Some(self.recipes.clone()) Some(self.recipes.clone())
@ -47,9 +70,9 @@ impl Iterator for Scoreboard {
#[test] #[test]
fn test_scoreboard() { fn test_scoreboard() {
let elves = vec![0, 1]; let elves = vec![0, 1];
let recipes = vec![3, 7]; let recipes = "37".into();
let mut scoreboard = Scoreboard { elves, recipes }; let mut scoreboard = Scoreboard { elves, recipes };
let recipes = scoreboard.next().unwrap(); let recipes = scoreboard.next().unwrap();
assert_eq!(recipes, vec![3, 7, 1, 0]); assert_eq!(recipes, "3710");
} }

Loading…
Cancel
Save