Skip to main content

aoc_toolkit/application/progress/
report.rs

1use super::{LocalProgress, ProgressDay, StarSnapshot, VerifiedStar, edition_progress};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub struct ProgressDayReport {
5    pub local: ProgressDay,
6    pub star_one: VerifiedStar,
7    pub star_two: VerifiedStar,
8}
9
10#[derive(Debug)]
11pub struct EditionProgress {
12    pub year: usize,
13    pub days: Vec<ProgressDayReport>,
14    pub synced_at_unix_seconds: Option<u64>,
15}
16
17impl EditionProgress {
18    #[must_use]
19    pub fn new(
20        year: usize,
21        registered: Option<&[(usize, usize)]>,
22        local: &LocalProgress,
23        snapshot: Option<&StarSnapshot>,
24    ) -> Self {
25        let snapshot = snapshot.filter(|snapshot| snapshot.year() == year);
26        let days = edition_progress(year, registered, local)
27            .into_iter()
28            .map(|local| ProgressDayReport {
29                local,
30                star_one: snapshot.map_or(VerifiedStar::Unknown, |stars| stars.star(local.day, 1)),
31                star_two: snapshot.map_or(VerifiedStar::Unknown, |stars| stars.star(local.day, 2)),
32            })
33            .collect();
34
35        Self {
36            year,
37            days,
38            synced_at_unix_seconds: snapshot.map(StarSnapshot::synced_at_unix_seconds),
39        }
40    }
41
42    #[must_use]
43    pub fn earned_stars(&self) -> usize {
44        self.days
45            .iter()
46            .flat_map(|day| [day.star_one, day.star_two])
47            .filter(|star| *star == VerifiedStar::Earned)
48            .count()
49    }
50
51    #[must_use]
52    pub fn possible_stars(&self) -> usize {
53        self.days.len() * 2
54    }
55
56    #[must_use]
57    pub fn completed_days(&self) -> usize {
58        self.days
59            .iter()
60            .filter(|day| {
61                day.star_one == VerifiedStar::Earned && day.star_two == VerifiedStar::Earned
62            })
63            .count()
64    }
65
66    /// Returns completion against all possible stars in the edition.
67    #[must_use]
68    pub fn completion_percent(&self) -> Option<usize> {
69        let possible = self.possible_stars();
70        if self.synced_at_unix_seconds.is_none() || possible == 0 {
71            return None;
72        }
73
74        Some((self.earned_stars() * 100 + possible / 2) / possible)
75    }
76
77    /// Returns whether the snapshot covers every currently unlocked day.
78    #[must_use]
79    pub fn has_complete_star_coverage(&self) -> bool {
80        self.synced_at_unix_seconds.is_some()
81            && self.days.iter().all(|day| {
82                day.local.availability != super::DayAvailability::Unlocked
83                    || (day.star_one != VerifiedStar::Unknown
84                        && day.star_two != VerifiedStar::Unknown)
85            })
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::{EditionProgress, LocalProgress, StarSnapshot, VerifiedStar};
92    use crate::application::run::{DayProgress, PartProgress, PartStatus, ProgressReport};
93
94    #[test]
95    fn summarizes_verified_stars_on_the_edition_calendar() {
96        let local = LocalProgress::default();
97        let snapshot = StarSnapshot::new(2025, 1_000, vec![2, 0, 1]).unwrap();
98        let report = EditionProgress::new(2025, None, &local, Some(&snapshot));
99
100        assert_eq!(report.days.len(), 12);
101        assert_eq!(report.earned_stars(), 3);
102        assert_eq!(report.possible_stars(), 24);
103        assert_eq!(report.completed_days(), 1);
104        assert_eq!(report.days[1].star_one, VerifiedStar::Missing);
105        assert_eq!(report.days[2].star_two, VerifiedStar::Missing);
106        assert_eq!(report.days[3].star_one, VerifiedStar::Unknown);
107        assert_eq!(report.synced_at_unix_seconds, Some(1_000));
108    }
109
110    #[test]
111    fn keeps_local_success_separate_from_verified_stars() {
112        let mut local = LocalProgress::default();
113        local.record(&ProgressReport {
114            schema_version: 1,
115            days: vec![DayProgress {
116                year: 2024,
117                day: 1,
118                parts: vec![PartProgress {
119                    part: 1,
120                    status: PartStatus::Succeeded,
121                }],
122                day_error: None,
123            }],
124        });
125
126        let other_edition = StarSnapshot::new(2025, 1_000, vec![2]).unwrap();
127        let report = EditionProgress::new(2024, None, &local, Some(&other_edition));
128
129        assert_eq!(report.days.len(), 25);
130        assert_eq!(report.days[0].local.part_one, Some(PartStatus::Succeeded));
131        assert_eq!(report.days[0].star_one, VerifiedStar::Unknown);
132        assert_eq!(report.earned_stars(), 0);
133        assert_eq!(report.possible_stars(), 50);
134        assert_eq!(report.synced_at_unix_seconds, None);
135    }
136}