summaryrefslogtreecommitdiffstats
path: root/src/bin/2022/day25.rs
blob: 31493653b2c160c0d64def6bef7fc24b1f045cc4 (plain) (blame)
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
#![allow(dead_code)]
#![allow(unused_variables)]

use advent_of_code::prelude::*;

fn from_snafu(line: String) -> i64 {
    let mut total = 0;
    for c in line.chars() {
        let digit = match c {
            '2' => 2,
            '1' => 1,
            '0' => 0,
            '-' => -1,
            '=' => -2,
            _ => panic!("unknown char {}", c),
        };
        total *= 5;
        total += digit;
    }
    total
}

fn to_snafu(mut n: i64) -> String {
    let mut s = String::new();
    while n > 0 {
        let rem = n % 5;
        let c = match rem {
            0 => '0',
            1 => '1',
            2 => '2',
            3 => '=',
            4 => '-',
            _ => unreachable!(),
        };
        s.insert(0, c);
        n = (n + 2) / 5;
    }
    s
}

pub fn parse(fh: File) -> Result<impl Iterator<Item = String>> {
    Ok(parse::raw_lines(fh))
}

pub fn part1(lines: impl Iterator<Item = String>) -> Result<String> {
    Ok(to_snafu(lines.map(from_snafu).sum()))
}

pub fn part2(_: impl Iterator<Item = String>) -> Result<i64> {
    todo!()
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(parse::data(2022, 25).unwrap()).unwrap()).unwrap(),
        "2-20=01--0=0=0=2-120"
    );
    // assert_eq!(
    //     part2(parse(parse::data(2022, 25).unwrap()).unwrap()).unwrap(),
    //     0
    // );
}