1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! This is my solution for [Advent of Code - Day 25 - _Title_](https://adventofcode.com/2022/day/25)
//!
//!

use std::fs;

/// The entry point for running the solutions with the 'real' puzzle input.
///
/// - The puzzle input is expected to be at `<project_root>/res/day-25-input`
/// - It is expected this will be called by [`super::main()`] when the user elects to run day 25.
pub fn run() {
    let contents = fs::read_to_string("res/day-25-input").expect("Failed to read file");
    let decimals = parse_input(&contents);

    println!(
        "The number to enter is: {}",
        sum_and_render(&decimals),
    );
}

fn to_snafu(decimal: isize) -> String {
    fn iter(decimal: isize, acc: String) -> String {
        if decimal == 0 {
            return acc;
        }

        let (digit, carry) = match decimal % 5 {
            0 => ('0', 0),
            1 => ('1', 0),
            2 => ('2', 0),
            3 => ('=', 1),
            4 => ('-', 1),
            _ => unreachable!("n % 5")
        };

        iter(
            decimal / 5 + carry,
            format!("{digit}{acc}"),
        )
    }

    iter(decimal, "".to_string())
}

fn from_snafu(snafu: String) -> isize {
    snafu.chars().fold(
        0,
        |acc, digit|
            acc * 5 + match digit {
                '0' => 0,
                '1' => 1,
                '2' => 2,
                '-' => -1,
                '=' => -2,
                d => unreachable!("{} is not a SNAFU digit", d)
            },
    )
}

fn sum_and_render(decimals: &Vec<isize>) -> String {
    to_snafu(decimals.into_iter().sum())
}

fn parse_input(input: &String) -> Vec<isize> {
    input.lines()
         .map(|line| from_snafu(line.to_string()))
         .collect()
}

#[cfg(test)]
mod tests {
    use crate::day_25::{from_snafu, parse_input, sum_and_render, to_snafu};

    fn sample_decimals() -> Vec<isize> {
        vec![
            1747,
            906,
            198,
            11,
            201,
            31,
            1257,
            32,
            353,
            107,
            7,
            3,
            37,
        ]
    }

    #[test]
    fn can_convert_snafus() {
        let examples = vec![
            (1, "1".to_string()),
            (2, "2".to_string()),
            (3, "1=".to_string()),
            (4, "1-".to_string()),
            (5, "10".to_string()),
            (6, "11".to_string()),
            (7, "12".to_string()),
            (8, "2=".to_string()),
            (9, "2-".to_string()),
            (10, "20".to_string()),
            (15, "1=0".to_string()),
            (20, "1-0".to_string()),
            (2022, "1=11-2".to_string()),
            (12345, "1-0---0".to_string()),
            (314159265, "1121-1110-1=0".to_string()),
        ];

        for (decimal, snafu) in examples {
            assert_eq!(to_snafu(decimal), snafu);
            assert_eq!(from_snafu(snafu), decimal);
        }
    }

    #[test]
    fn can_parse() {
        let input = "1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122".to_string();

        assert_eq!(parse_input(&input), sample_decimals());
    }

    #[test]
    fn can_sum_to_snafu() {
        assert_eq!(
            sum_and_render(&sample_decimals()),
            "2=-1=0".to_string()
        )
    }
}