day06 task 1
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
mod tasks;
|
||||
|
||||
fn main() {
|
||||
tasks::day04::run();
|
||||
tasks::day06::run();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use itertools::Itertools;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn run() {
|
||||
let range = 109165..=576723;
|
||||
task1(&range);
|
||||
|
||||
32
src/tasks/day06.rs
Normal file
32
src/tasks/day06.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub fn run() {
|
||||
let input = std::fs::read_to_string("input/day06.txt").unwrap();
|
||||
let orbits: Vec<_> = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut s = line.split(")");
|
||||
(s.next().unwrap(), s.next().unwrap())
|
||||
})
|
||||
.collect();
|
||||
task1(&orbits);
|
||||
}
|
||||
|
||||
fn task1(orbit_list: &Vec<(&str, &str)>) {
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back((0, "COM")); // com is where everything floats around
|
||||
let sum: i32 = (0..)
|
||||
.scan((orbit_list, queue), |(all, queue), _| {
|
||||
if let Some((depth, next)) = queue.pop_front() {
|
||||
all.iter()
|
||||
.filter(|(a, _)| *a == next)
|
||||
.for_each(|(_, b)| queue.push_back((depth + 1, b)));
|
||||
Some(depth)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
println!("Task 1: there are {} direct and indirect orbits", sum);
|
||||
//405096 too high
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
pub mod day01;
|
||||
pub mod day03;
|
||||
pub mod day04;
|
||||
pub mod day05;
|
||||
pub mod day06;
|
||||
|
||||
Reference in New Issue
Block a user