summaryrefslogtreecommitdiff
path: root/2023/rust/day13/src/lib.rs
blob: 03af2b825cf2533a7e6d27e5c56a30f4d7d4bf5b (plain)
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
145
146
pub fn part1(input: &str) -> u32 {
    let mut res: u32 = 0;

    let m: Vec<Vec<String>> = input
        .trim_end()
        .split("\n\n")
        .map(|x| {
            x.split('\n')
                .map(|y| y.chars().collect::<String>())
                .collect()
        })
        .collect();

    for s in m {
        let n = s.len();

        // Search by column
        //  012345678
        //      ><
        //  #.##..##.
        //  ..#.##.#.
        //  ##......#
        //  ##......#
        //  ..#.##.#.
        //  ..##..##.
        //  #.#.##.#.
        //      ><
        //  012345678
        //
        let m = s[0].len();
        let mut mc = 0;
        for i in 0..m {
            let mut j = m - 1;
            while j > i {
                let mut s1 = String::new();
                let mut s2 = String::new();
                for k in 0..n {
                    s1.push(s[k].chars().nth(i).unwrap());
                    s2.push(s[k].chars().nth(j).unwrap());
                }
                if s1 == s2 {
                    mc = std::cmp::max(mc, i);
                }
                j -= 1;
            }
        }

        let mut check = true;

        let mut i = mc;
        let mut j = mc + 1;

        while i > 0 && j < n {
            let mut s1 = String::new();
            let mut s2 = String::new();
            for k in 0..n {
                s1.push(s[k].chars().nth(i).unwrap());
                if j >= s[k].len() {
                    break;
                }
                s2.push(s[k].chars().nth(j).unwrap());
            }
            if s1 != s2 {
                check = false;
                break;
            }
            i -= 1;
            j += 1;
        }

        if check {
            res += (mc + 1) as u32;
            continue;
        }

        // Search by row
        let mut mc = 0;
        for i in 0..(n / 2) + 1 {
            for j in ((n / 2)..n).rev() {
                if s[i] == s[j] {
                    mc = std::cmp::max(mc, i);
                }
            }
        }

        let mut check = true;

        let mut i = mc;
        let mut j = mc + 1;

        while i > 0 && j < n {
            if s[i] != s[j] {
                check = false;
                break;
            }
            i -= 1;
            j += 1;
        }

        if check {
            res += (mc + 1) as u32 * 100;
            continue;
        }
    }

    res
}

// pub fn part2(input: &str) -> u32 {
//     let mut res: u32 = 0;
//
//     res
// }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn example_part1() {
        let input = include_str!("../example.txt");
        let result = part1(input);
        assert_eq!(result, 405);
    }

    #[test]
    fn input_part1() {
        let input = include_str!("../input.txt");
        let result = part1(input);
        assert_eq!(result, 2331);
    }
    //
    // #[test]
    // fn example_part2() {
    //     let input = include_str!("../example.txt");
    //     let result = part2(input);
    //     assert_eq!(result, 2286);
    // }
    //
    // #[test]
    // fn input_part2() {
    //     let input = include_str!("../input.txt");
    //     let result = part2(input);
    //     assert_eq!(result, 71585);
    // }
}