summaryrefslogtreecommitdiff
path: root/2018/src/bin/day_10.rs
blob: 082be7c409b685cd69298336dcfafd3765b6e0ff (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
extern crate advent_of_code_2018;
use advent_of_code_2018::*;

use std::str::FromStr;

use std::error::Error;
use std::path::PathBuf;

use std::i32;

// cargo watch -cs "cargo run --release --bin day_10"



#[derive(Debug, Clone)]
struct Star {
    x: i32,
    y: i32,
    dx: i32,
    dy: i32
}

impl FromStr for Star {
    type Err = Box<Error>;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let split = s
            .split(|c: char| c != '-' && !c.is_numeric())
            .filter(|s| !s.is_empty())
            .map(|s| s.parse())
            .collect::<Result<Vec<i32>, _>>()?;
        
        Ok(Star {
            x: split[0],
            y: split[1],
            dx: split[2],
            dy: split[3]
        })
    }
}

fn main() -> Result<(), Box<Error>> {
    let input = read_file(&PathBuf::from("inputs/10.txt"))?;
    let mut stars = input.iter().map(|line| line.parse::<Star>().unwrap()).collect::<Vec<_>>();

    //debug!(input);
    //debug!(stars);

    let screen_width = 80;
    let screen_height = 80;

    for seconds in 0..1000000 {
        let min_x = stars.iter().map(|s| s.x).min().unwrap();
        let max_x = stars.iter().map(|s| s.x).max().unwrap();
        let min_y = stars.iter().map(|s| s.y).min().unwrap();
        let max_y = stars.iter().map(|s| s.y).max().unwrap();

        if max_x - min_x < screen_width && max_y - min_y < screen_height {
            debug!(seconds);
            for y in min_y..max_y+1 {
                for x in min_x..max_x+1 {
                    let star_at_point = stars.iter().any(|s| s.x == x && s.y == y);
                    if star_at_point {
                        print!("#");
                    } else {
                        print!(" ");
                    }
                }
                println!();
            }
            println!();
            println!();
        }

        for star in &mut stars {
            star.x += star.dx;
            star.y += star.dy;
        }
    }

    Ok(())
}