summaryrefslogtreecommitdiff
path: root/d08/src/main.rs
blob: cbdd1fd45eb71763cc5e295599ef54a0bf19cb83 (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
use std::fs;

fn solve_puzzle(lines: &Vec<&str>) -> [[bool; 6]; 50] {
    let mut pixels = [[false; 6]; 50];

    for line in lines {
        let words: Vec<&str> = line.split_ascii_whitespace().collect();
        if words[0] == "rect" {
            // Read operation parameters
            let wxh: Vec<&str> = words[1].split('x').collect();
            let w: usize = wxh[0].parse().unwrap();
            let h: usize = wxh[1].parse().unwrap();

            // Apply operation to state
            for x in 0..w {
                for y in 0..h {
                    pixels[x][y] = true;
                }
            }
        } else if words[0] == "rotate" && words[1] == "row" {
            // Read operation parameters
            let which: Vec<&str> = words[2].split('=').collect();
            let y: usize = which[1].parse().unwrap();
            let n: usize = words[4].parse().unwrap();

            // Apply operation to state
            let mut old = Vec::new();
            for x in 0..50 {
                old.push(pixels[x][y]);
            }
            for x in 0..50 {
                pixels[x][y] = old[(x + 50 - n) % 50];
            }
        } else if words[0] == "rotate" && words[1] == "column" {
            // Read operation parameters
            let which: Vec<&str> = words[2].split('=').collect();
            let x: usize = which[1].parse().unwrap();
            let n: usize = words[4].parse().unwrap();

            // Apply operation to state
            let old = pixels[x];
            for y in 0..6 {
                pixels[x][y] = old[(y + 6 - n) % 6];
            }
        }
    }

    pixels
}

fn main() {
    let input = fs::read_to_string("input.txt").unwrap();
    let lines = input.lines().collect();

    let pixels = solve_puzzle(&lines);

    // Part 2 must be read by eye, gives "UPOJFLBCEZ" for me
    println!("Part 2 solution:");
    let mut total = 0;
    for y in 0..6 {
        print!("\t");
        for x in 0..50 {
            print!(
                "{}",
                if pixels[x][y] {
                    total += 1;
                    '#'
                } else {
                    ' '
                }
            );
        }
        print!("\n");
    }

    // Part 1 gives 116 for me
    println!("Part 1 solution: {}", total);
}