~xenrox/aoc2020

9fdf5d6c63117a7144da7cb008919eb4cf492465 — Thorben Günther 1 year, 7 months ago e1b0047
Solve day 1 part 1
5 files changed, 384 insertions(+), 0 deletions(-)

A data/day03.dat
A data/day03_test.dat
M src/days.rs
A src/days/day03.rs
M src/main.rs
A data/day03.dat => data/day03.dat +323 -0
@@ 0,0 1,323 @@
...#...#..#....#..#...#..##..#.
.#..#.....#.#............###...
.#...###....#.............##..#
...##...##....#.....##..#.##...
.....###.#.###..##.#.##.......#
#...##.....#..........#..#.#.#.
......##.......##..#....#.#....
....#.###.##..#.#..##.##....#.#
.......#.......###.#.#.##.....#
.........#.#....#..........#.#.
.#...##.....##.........#..#....
.##....#.#.#...##......#.......
##.#.#..#....#....#....#...#.#.
##....#.#..##......#....##...#.
....#..#..##..#.###.......#.#..
.....##....###...........#.#.##
#.....##.........#....##......#
........###.#..#....#....#.....
...#.......#.##..#.###......#..
...............#..#....#.##....
..#..###..#.#..#.........##..#.
####..#..####..................
#...####...#.......#.#.#...#...
......###.....#......#..#..#...
#...#.....##.....#.#..##...#.#.
#...........##.......#.........
.#..#.........#.#..##....#.....
........##...#................#
........#.###.#.###.#.#.##..##.
.#....##.....#...##.#..#.#.....
..#..#.....###....##.#....#.#.#
#......##.##...##..#.........#.
#..#..#.....#.....#.........#..
#....#.#...###.........#...#...
.#.#.....##......#.#......#....
..##......##...#.#.#.#.........
..#......#.....##.###.#.#..#...
....#..#.......#..#..#.....#...
.#.#.....#...#..........#......
#.#..#...........#.#.##.#...#.#
..#.#....###...#...#.....#.#...
....##.#.###....####.......#...
.....##....#.......#..#..#....#
...##..#.#.#.#......#......#...
...##...#....#...#......###...#
........#..#.#.....#.###.......
..#..##.#....#.#.........#...#.
.....#.####....#.##.........#..
......#...#...#.....#......###.
.##.....#....#..#.#....#.....#.
...........#...#....##..#...#..
.....#....#.....#...##..#...#.#
.#...#.........#.......#...#..#
...#..#...#........#......#....
..#..#####.#.....#.#....#...#.#
...#.......#.#....#...##..#..#.
####..#.#.###.#.#..............
.##........#...#.#....#..#.....
..#..............#.#..##...#.##
.###.#.....#.#.....##.#......##
....###.....#...#...#.#..#.....
....###.#.##.......#....#...#..
#..#...#......##..#.....#.#...#
....#.#.........#..............
#.##.##...#..#.#.#.....#...#.##
#...#...#......#...........##..
#.#.#......#............#.#....
.#.#..######...#.#.........#.##
..#.#..#...#......#............
....#.....#......##..#.....#...
.##............#....##..#......
.#.#.#...#.##.............###.#
#.#...#...#.....#....#.#.#.....
........#..#......##.##.#.....#
.....#.....#.#####...#....#....
.#...#......#.........#.#......
...#...#..##.....##....#..#....
....#....##..#.........#.......
..#........##..#.#........#....
...#...##...........#...#....#.
.....##.........#..#....#..#.#.
#..#....##..#...##.....#..##.#.
..#.#.#.#...#...#.....#.#....#.
.......#.###...#.#.......#.#...
....#..#..#.###.#.....###..#.#.
.#..##......#..#..#....#.####..
..##...........#...#.........#.
......#..#...#..........#......
....#..........#......##...#...
....#..#.##........#.#...##.#..
#.##......#........##.#...#...#
#..#....#.....###........##....
...........##.....##..#....#.##
..#....#..#..#......#.#.....#..
#....#.##....#.....##.......#..
.#.....#.#..............#.##..#
.#..#..#...#...#....#.#.....#..
...###...##.#...#..#........#..
#...#.##.#.....#.#....#..#.....
#.....###.#.......#.#..#.#..##.
....#..#..##.......###.#...#...
.#...####...............#.....#
.#.##.#.....#.....#.#......##.#
#...........#.##....###.##....#
...............#..........#....
.....#..#.##.###.#.............
...##.............#.....#.#..#.
....#.#...#.#..#..#..#....#....
..#.......#..........#...#...#.
...............#.#.#...###....#
....#...#.##....#..##....#.....
........#.#.##.........##.##.##
#.....###.......#.#....#..#..##
.#..#...#......#.#..##.......#.
#.....#.#........#.##..#..#....
.###..##.#.......#......###....
.#...###.....#.....#....###...#
........##.##......#.#....#...#
.#....#..#.........#..##...##..
.......#.......##.#..#..##.....
#..##..##......#.#......#.##...
..#..###..#...#....#..#...#....
#.............#.####.........##
..#..................#...#..#..
..#......#........##.......#.#.
.#.#.#.#..###.....#....#.#.....
...#.##.###.......#....#.......
................##...#.....#...
..#.###.#...#.####....#..#..#..
..#....###....##..#.#.........#
.#..#.#.....#........#....##...
.....#..#......#..#..##.#.#....
.#..#.........##....##......#..
.....#.#...#...#.#...#.#...#.#.
..#..#...#...#...##.#..###.....
..#..##......#..##.#...##......
.......#..##....##.#......#..#.
..#......#.#.....#.##....##....
..#....#......#......##........
....##.#.#....#.......#.##.....
#.....#...###....#....#...#....
............#.#..#...#...#..#..
..##.............##....#.......
.#.......#.##.#......#....##...
...##............#....#..#...#.
.##.####.....#.#..###.#....#.##
....##.#........#..#...#.......
...#...###.##...........##..#..
..##..##....#...#..#..........#
..#.........#.#...##..........#
.......##....#.#...##.....#..#.
.............#.....#.#.......#.
#.......#..##..##...##.#.......
..............#.....#.#..#...##
........##..#.....#...#...#.#..
###.#.................#........
...#........#...#.#######..#..#
...#.##...##.#.#..######...#...
#.......#..#....#..#.##.....#..
#..#....##....#.##.......#....#
#...#..#.#.#...#..#.##..#......
....#..##....#..#.#...........#
.##..#.#.............###.......
#....##......#..#..#.....###...
..#..........#...###.#.........
.####......#....#......#.#....#
..#....#.#.#......#....#.......
.....#.....#....#....#####....#
.##..........#...#.###....#....
....##.....##......#...#.#.....
.#...#...#..#.#.#...#####......
...#.##..####.##.##.......##...
............#.......#..........
.#..##.#..#####........#..#...#
#......##..##..##.........##...
....#....#.............#.#....#
###..#.....#.....#.#...#..#.###
#...#.......##......#....#.#.#.
...#......#..#...#....#...###.#
....#....##.......#....#......#
............#......##.##.....#.
...#.........#......#....##..##
.....##....##...#..###...#..#..
.......##.#..........#.##.##...
....##...........#.#..#..#.##.#
#...#..##.##.#....#....#.#.....
...##.#.....#..#..#..###....##.
#.##.#..#..#.#.............#...
..#.#.............###.....#....
...#..#....#..#.....#.#..#..#..
...#.....##.#...........#..##.#
.........#.#.##..#..#.#...#....
...#..##..#...#...###.##.#..#..
.#..##...##......##..##........
......##....##.#.##.#.#........
...#..................#.....#..
.##................#.#..#..###.
.##.##.....#................#..
.....#.#..........#...#..#.#..#
.............#......#..#.#..#..
...#...##..#........#....#.....
#......#........##.##...##.....
##..#..##....#...#............#
..##..##.##....##..##........#.
...#....#.#.#.#....#.#...##....
....#...##..##.#.##...#..#...#.
#..#....##.#.....#.......#...##
##.#....#.............#..#.....
.##..#..#.#.....#.......#.#..#.
.......#..#...##...#...###..#..
..........#...#.#..##.....#...#
..#....#...........#####....#..
#....#..#.......##.............
.........##..#####.......##....
#..#..........#.....###...#..#.
.#.#.#..#...#.......##...#####.
.....#....#.###...#.......#....
#.#.....##...###....###....#...
.#.....#..#.#.#........#...#...
.##.#.#.#......#....###....#...
.#..##..####......###......#...
......#.#.#.#.#...#...####.##..
.#........##..#.....#....#....#
.....###......##..#....#.......
#.#.##...#.#......###..........
........#.#...#..#......#....#.
..##...##.........#.......#.#..
..#.##....#...##.....#.###.....
.........#..#.#....#....#.#.##.
#.........#......#..#.......#..
...#...##.......#.........#....
............#......#...........
##.....#.....#.#...#.....#.....
..#.#...#..#...#.#...........#.
#.#.#..#..#...##.#...#.#.....#.
.#..###.#..##.#.....#.....#....
##....##....#.......##..##.....
.#..#...........###..........#.
.#..#..#..........###..#.......
#..###......#............##...#
#......#........#..#..#..#.#...
.......#.###...#.##............
.##....#.......#.#...##.....#.#
....#..#.#.......#.#...........
##....#.###.#....#.#..##.#....#
..#..#..#....#...#........##...
...#...##....#..#.#...#..#.....
......#..#......#....#.......#.
#.#..............#...###...#..#
...#....#..#..........#.#...#..
#.....##..##.....#........#....
.#...##..#.#..............#....
##.#....#..##...#..#.####.#..#.
.....#.......#.#.#.#..#.....###
...#.##....#.#........##.......
#...#.#...#.#..###..##.##...#.#
###..............#.#.###.......
...###..#.#..#....##...###.#...
......##...........#...#..#...#
.#..#.........##.......#..#...#
.#.......###......##...#...#...
.#......##...#........#.......#
.#..#.....#.........#.#........
#...#.#.....#...#..##.........#
......##.#......##.#..##.#.....
...............#.#..#....#....#
#....#..#..#..#.#.....##...##..
#.#......#.###......#..#...####
.#.#..#...#...#.#..#.##.##.#.#.
.....#.#...###...#.#.....##....
...#..#.#..........##.#....#.#.
...#..#.#.##.....###.##.#....#.
..........#..###......#..#.#...
###.....#..###..#...#..###.#...
..#..#.....##.#.#..###.......#.
....#....##........##..........
.......#..........#...#......#.
.#........#.#.#.#.#.......#....
.#..#.......##..##....#.#...#..
.#.#.#.......#..#..............
#.#....#.#...#.#.#.....#.#...##
.....#..........##..#.......#..
.##......#.#....#.#.......#....
..#.##....#.##.#...#...........
...##......##..##.............#
..........##.#.#..#..........#.
.##....#..#..#.#....##.#...#.#.
...........#....#.....#.#..#...
.#.....#....##..#.........#....
.....#.....#...#....#...#.###.#
..#....#....#.....#...#......#.
.....##..#.............#...#...
........#..#.......#.#.......#.
#...###..#.##.#...###...##..##.
....##..#.......#...#.#........
.#...#.#.##....####........#..#
.#...#.#.####.##.#.............
#..##...#....#...#.#.#.#.##..#.
.#.......#........#.....###....
#.#.....#....#..#....#..#....#.
...#..#...#.....#.........##...
.#....#......###...#....#.#.#..
#.#........#......#...#....##..
.....#..#......#..#..#......#..
.#.....#..#.##.#.#.#...#......#
##........#..#.#..#...#.####...
..........##....#.#..#.#....#..
#.##..#..#....#..#....##..#.#.#
..#......#.......#...##..#.....
##...#.........#......#......#.
.#.....................#..#.##.
.#.......#........#.#.#..##.#..
..#..........#........#..##.#..
.#...#...#.........##.#.#.#....
....#....#.###.#....###....#.##
....##......##........##.#.##..
....#.#......#.##.#...#.##.....
....#....#..#.#..###.#.#.......
....#......#..#.#.......#..##..
.....#..#.#.##.##..##.....#.#..
...#....................##.....
#.....#...##...#.#.............
..#.#...#.#.#.....##..#....#...

A data/day03_test.dat => data/day03_test.dat +11 -0
@@ 0,0 1,11 @@
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#

M src/days.rs => src/days.rs +1 -0
@@ 1,2 1,3 @@
pub mod day01;
pub mod day02;
pub mod day03;

A src/days/day03.rs => src/days/day03.rs +48 -0
@@ 0,0 1,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);
    }
}

M src/main.rs => src/main.rs +1 -0
@@ 3,4 3,5 @@ mod days;
fn main() {
    days::day01::get_solution();
    days::day02::get_solution();
    days::day03::get_solution();
}