aboutsummaryrefslogtreecommitdiff
path: root/src/day14pt2.rs
blob: a5d5bed80475c59e99d1a030d8dc46785ea574cb (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
fn cycle(input: &mut Vec<Vec<char>>) {
    let mut sl: usize;

    // north
    for j in 0..input[0].len() {
        sl = 0;
        for i in 0..input.len() {
            if input[i][j] == 'O' {
                if sl != i {
                    input[sl][j] = 'O';
                    input[i][j] = '.';
                }
                sl += 1;
            } else if input[i][j] == '#' {
                sl = i + 1;
            }
        }
    }

    // west
    for i in 0..input.len() {
        sl = 0;
        for j in 0..input[0].len() {
            if input[i][j] == 'O' {
                if sl != j {
                    input[i][sl] = 'O';
                    input[i][j] = '.';
                }
                sl += 1;
            } else if input[i][j] == '#' {
                sl = j + 1;
            }
        }
    }

    // south
    for j in 0..input[0].len() {
        sl = input.len() - 1;
        for i in (0..input.len()).rev() {
            if input[i][j] == 'O' {
                if sl != i {
                    input[sl][j] = 'O';
                    input[i][j] = '.';
                }
                if i != 0 {
                    sl -= 1;
                }
            } else if input[i][j] == '#' {
                if i != 0 {
                    sl = i - 1;
                }
            }
        }
    }

    // east
    for i in 0..input.len() {
        sl = input[0].len() - 1;
        for j in (0..input[0].len()).rev() {
            if input[i][j] == 'O' {
                if sl != j {
                    input[i][sl] = 'O';
                    input[i][j] = '.';
                }
                if j != 0 {
                    sl -= 1;
                }
            } else if input[i][j] == '#' {
                if j != 0 {
                    sl = j - 1;
                }
            }
        }
    }
}

fn sum(input: &Vec<Vec<char>>) -> u64 {
    let mut sum: u64 = 0;

    for i in 0..input.len() {
        for j in 0..input[0].len() {
            if input[i][j] == 'O' {
                sum += (input.len() - i) as u64;
            }
        }
    }

    return sum;
}

fn cycle_len(a: &Vec<u64>) -> u64 {
    for cl in 2..(a.len() / 3) {
        let mut ind = true;
        for i in 0..cl {
            if a[i] != a[cl + i] || a[cl + i] != a[cl * 2 + i] {
                ind = false;
                break;
            }
        }
        if ind {
            return cl as u64;
        }
    }
    return 0;
}

pub fn main() {
    let txt = std::fs::read_to_string("./input/day14.txt").unwrap();
    let mut input = txt
        .lines()
        .map(|s| s.chars().collect::<Vec<char>>())
        .collect::<Vec<Vec<char>>>();


    let mut a = vec![];
    for i in 0..1000 {
        cycle(&mut input);
        if i > 500 {
            a.push(sum(&input));
        }
    }

    let cl = cycle_len(&a);
    assert_ne!(cl, 0);

    let c = 1000000000 - 501 - 1;
    println!("{}", a[(c % cl) as usize]);
}