~xenrox/aoc2020

ref: 9fdf5d6c63117a7144da7cb008919eb4cf492465 aoc2020/src/days/day03.rs -rw-r--r-- 1.0 KiB
9fdf5d6cThorben Günther Solve day 1 part 1 1 year, 5 months ago
                                                                                
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
use std::fs;

fn parse_contents(contents: String) -> Vec<Vec<char>> {
    let mut map: Vec<Vec<char>> = Vec::new();
    for line in contents.lines() {
        map.push(line.chars().collect());
    }
    map
}

fn count_trees(map: &Vec<Vec<char>>) -> i32 {
    let mut x = 0;
    let mut y = 0;
    let mut count = 0;

    let x_max = map[0].len();
    let y_max = map.len();

    while y != y_max - 1 {
        y += 1;
        x = (x + 3) % x_max;
        if map[y][x] == '#' {
            count += 1;
        }
    }
    count
}

pub fn get_solution() {
    let contents = fs::read_to_string("data/day03.dat").expect("Could not read data");
    let map = parse_contents(contents);
    let count = count_trees(&map);

    println!("Day 3, part 1: {}", count);
}

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

    #[test]
    fn part1() {
        let contents = fs::read_to_string("data/day03_test.dat").expect("Could not read data");
        let map = parse_contents(contents);
        let count = count_trees(&map);
        assert_eq!(count, 7);
    }
}