summaryrefslogtreecommitdiff
path: root/2024/day04.rs
blob: f4dc88f3f57a0309f7b6d076a47fbd4f7fe2cc3e (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
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::env;
use std::fs::File;
use std::io::Read;

fn horizontal(lx: usize, ly: usize, m: &Vec<Vec<char>>) -> usize {
    let mut count = 0;
    for y in 0..ly {
        for x in 0..lx - 3 {
            if m[y][x] == 'X' && m[y][x + 1] == 'M' && m[y][x + 2] == 'A' && m[y][x + 3] == 'S' {
                count += 1;
            }
            if m[y][x] == 'S' && m[y][x + 1] == 'A' && m[y][x + 2] == 'M' && m[y][x + 3] == 'X' {
                count += 1;
            }
        }
    }
    count
}

fn vertical(lx: usize, ly: usize, m: &Vec<Vec<char>>) -> usize {
    let mut count = 0;
    for y in 0..ly - 3 {
        for x in 0..lx {
            if m[y][x] == 'X' && m[y + 1][x] == 'M' && m[y + 2][x] == 'A' && m[y + 3][x] == 'S' {
                count += 1;
            }
            if m[y][x] == 'S' && m[y + 1][x] == 'A' && m[y + 2][x] == 'M' && m[y + 3][x] == 'X' {
                count += 1;
            }
        }
    }
    count
}

fn diag1(lx: usize, ly: usize, m: &Vec<Vec<char>>) -> usize {
    let mut count = 0;
    for y in 0..ly - 3 {
        for x in 0..lx - 3 {
            if m[y][x] == 'X'
                && m[y + 1][x + 1] == 'M'
                && m[y + 2][x + 2] == 'A'
                && m[y + 3][x + 3] == 'S'
            {
                count += 1;
            }
            if m[y][x] == 'S'
                && m[y + 1][x + 1] == 'A'
                && m[y + 2][x + 2] == 'M'
                && m[y + 3][x + 3] == 'X'
            {
                count += 1;
            }
        }
    }
    count
}
fn diag2(lx: usize, ly: usize, m: &Vec<Vec<char>>) -> usize {
    let mut count = 0;
    for y in 0..ly - 3 {
        for x in 0..lx - 3 {
            if m[y][x + 3] == 'X'
                && m[y + 1][x + 2] == 'M'
                && m[y + 2][x + 1] == 'A'
                && m[y + 3][x] == 'S'
            {
                count += 1;
            }
            if m[y][x + 3] == 'S'
                && m[y + 1][x + 2] == 'A'
                && m[y + 2][x + 1] == 'M'
                && m[y + 3][x] == 'X'
            {
                count += 1;
            }
        }
    }
    count
}

fn mas(lx: usize, ly: usize, m: &Vec<Vec<char>>) -> usize {
    let mut count = 0;
    for y in 1..ly - 1 {
        for x in 1..lx - 1 {
            if m[y][x] != 'A' {
                continue;
            }
            let a = [
                m[y - 1][x - 1],
                m[y - 1][x + 1],
                m[y + 1][x + 1],
                m[y + 1][x - 1],
            ];
            if a == ['M', 'M', 'S', 'S']
                || a == ['S', 'M', 'M', 'S']
                || a == ['S', 'S', 'M', 'M']
                || a == ['M', 'S', 'S', 'M']
            {
                count += 1;
            }
        }
    }
    count
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let filename = if args.len() == 1 {
        "in/".to_owned() + args[0].split('/').last().unwrap() + ".pzl"
    } else {
        args[1].clone()
    };
    let mut f = File::open(filename).expect("cannot open file");
    let mut content = String::new();
    f.read_to_string(&mut content).expect("cannot read file");
    let lines = content.trim_end().split("\n");

    let mut m: Vec<Vec<char>> = Vec::new();
    for line in lines {
        let mut l: Vec<char> = Vec::new();
        for c in line.bytes() {
            l.push(c as char)
        }
        m.push(l)
    }
    //println!("{:?}", m);

    let ly = m.len();
    let lx = m[0].len();

    let mut res1 = 0;
    res1 += horizontal(lx, ly, &m);
    res1 += vertical(lx, ly, &m);
    res1 += diag1(lx, ly, &m);
    res1 += diag2(lx, ly, &m);

    let res2 = mas(lx, ly, &m);

    println!("res1: {}", res1);
    println!("res2: {}", res2);
    assert_eq!(res1, 2575);
    assert_eq!(res2, 2041);
}