summaryrefslogtreecommitdiff
path: root/2017/src/bin/day_14.rs
blob: 778a57f51d058d7072ae194556529f652ea2849a (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
extern crate advent_of_code_2017;
use advent_of_code_2017::*;

fn main() {
    let args = AdventArgs::init();
    let input = args.input[0].clone();

    let mut used = 0;
    let mut grid: Vec<Vec<bool>> = vec!(vec!(false; 128); 128);
    for i in 0..128 {
        let to_hash = format!("{}-{}", input, i);
        let hash = knot_hash(&to_hash);
        for (x1,c) in hash.chars().enumerate() {
            let parsed = u32::from_str_radix(&c.to_string(), 16).unwrap();
            used += parsed.count_ones();
            for (x2,b) in format!("{:04b}",parsed).chars().enumerate() {
                grid[i][4*x1+x2] = b == '1';
            }
        }
    }

    if args.part == 1 {
        println!("{} is used", used);
    } else {
        let mut group_count = 0;
        for start_y in 0..128 {
            for start_x in 0..128 {
                if grid[start_y][start_x] {
                    group_count += 1;
                    clear_group(&mut grid, Point{
                        x: start_x as i32,
                        y: start_y as i32
                    });
                }
            }
        }
        println!("{} groups", group_count);
        
    }
}

fn clear_group(grid: &mut Vec<Vec<bool>>, point: Point) {
    if point.x >= 0 && point.x < 128 && point.y >= 0 && point.y < 128 {
        if grid[point.y as usize][point.x as usize] {
            grid[point.y as usize][point.x as usize] = false;
            clear_group(grid, point.up());
            clear_group(grid, point.down());
            clear_group(grid, point.left());
            clear_group(grid, point.right());
        }
    }
}