aoc-toolkit 0.1.0-alpha.4

A toolkit for solving Advent of Code problems
Documentation
use super::{LocalProgress, ProgressDay, StarSnapshot, VerifiedStar, edition_progress};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProgressDayReport {
    pub local: ProgressDay,
    pub star_one: VerifiedStar,
    pub star_two: VerifiedStar,
}

#[derive(Debug)]
pub struct EditionProgress {
    pub year: usize,
    pub days: Vec<ProgressDayReport>,
    pub synced_at_unix_seconds: Option<u64>,
}

impl EditionProgress {
    #[must_use]
    pub fn new(
        year: usize,
        registered: Option<&[(usize, usize)]>,
        local: &LocalProgress,
        snapshot: Option<&StarSnapshot>,
    ) -> Self {
        let snapshot = snapshot.filter(|snapshot| snapshot.year() == year);
        let days = edition_progress(year, registered, local)
            .into_iter()
            .map(|local| ProgressDayReport {
                local,
                star_one: snapshot.map_or(VerifiedStar::Unknown, |stars| stars.star(local.day, 1)),
                star_two: snapshot.map_or(VerifiedStar::Unknown, |stars| stars.star(local.day, 2)),
            })
            .collect();

        Self {
            year,
            days,
            synced_at_unix_seconds: snapshot.map(StarSnapshot::synced_at_unix_seconds),
        }
    }

    #[must_use]
    pub fn earned_stars(&self) -> usize {
        self.days
            .iter()
            .flat_map(|day| [day.star_one, day.star_two])
            .filter(|star| *star == VerifiedStar::Earned)
            .count()
    }

    #[must_use]
    pub fn possible_stars(&self) -> usize {
        self.days.len() * 2
    }

    #[must_use]
    pub fn completed_days(&self) -> usize {
        self.days
            .iter()
            .filter(|day| {
                day.star_one == VerifiedStar::Earned && day.star_two == VerifiedStar::Earned
            })
            .count()
    }

    /// Returns completion against all possible stars in the edition.
    #[must_use]
    pub fn completion_percent(&self) -> Option<usize> {
        let possible = self.possible_stars();
        if self.synced_at_unix_seconds.is_none() || possible == 0 {
            return None;
        }

        Some((self.earned_stars() * 100 + possible / 2) / possible)
    }

    /// Returns whether the snapshot covers every currently unlocked day.
    #[must_use]
    pub fn has_complete_star_coverage(&self) -> bool {
        self.synced_at_unix_seconds.is_some()
            && self.days.iter().all(|day| {
                day.local.availability != super::DayAvailability::Unlocked
                    || (day.star_one != VerifiedStar::Unknown
                        && day.star_two != VerifiedStar::Unknown)
            })
    }
}

#[cfg(test)]
mod tests {
    use super::{EditionProgress, LocalProgress, StarSnapshot, VerifiedStar};
    use crate::application::run::{DayProgress, PartProgress, PartStatus, ProgressReport};

    #[test]
    fn summarizes_verified_stars_on_the_edition_calendar() {
        let local = LocalProgress::default();
        let snapshot = StarSnapshot::new(2025, 1_000, vec![2, 0, 1]).unwrap();
        let report = EditionProgress::new(2025, None, &local, Some(&snapshot));

        assert_eq!(report.days.len(), 12);
        assert_eq!(report.earned_stars(), 3);
        assert_eq!(report.possible_stars(), 24);
        assert_eq!(report.completed_days(), 1);
        assert_eq!(report.days[1].star_one, VerifiedStar::Missing);
        assert_eq!(report.days[2].star_two, VerifiedStar::Missing);
        assert_eq!(report.days[3].star_one, VerifiedStar::Unknown);
        assert_eq!(report.synced_at_unix_seconds, Some(1_000));
    }

    #[test]
    fn keeps_local_success_separate_from_verified_stars() {
        let mut local = LocalProgress::default();
        local.record(&ProgressReport {
            schema_version: 1,
            days: vec![DayProgress {
                year: 2024,
                day: 1,
                parts: vec![PartProgress {
                    part: 1,
                    status: PartStatus::Succeeded,
                }],
                day_error: None,
            }],
        });

        let other_edition = StarSnapshot::new(2025, 1_000, vec![2]).unwrap();
        let report = EditionProgress::new(2024, None, &local, Some(&other_edition));

        assert_eq!(report.days.len(), 25);
        assert_eq!(report.days[0].local.part_one, Some(PartStatus::Succeeded));
        assert_eq!(report.days[0].star_one, VerifiedStar::Unknown);
        assert_eq!(report.earned_stars(), 0);
        assert_eq!(report.possible_stars(), 50);
        assert_eq!(report.synced_at_unix_seconds, None);
    }
}