summaryrefslogtreecommitdiff
path: root/2019/src/bin/day_8.rs
blob: 0508e7c2c5a21193e5a4f86629f90355df1ad14d (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::fmt;
use std::io;
use std::io::prelude::*;
use std::process;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "Day 8: Space Image Format")]
/// Executes an Intcode program on 5 amplifiers, and finds the input that gives the max output
///
/// See https://adventofcode.com/2019/day/8 for details.
struct Opt {
    /// Rather than rendering the image, calculate and print its checksum
    #[structopt(short = "c", long = "checksum")]
    checksum_mode: bool,
    #[structopt(short = "w", long = "width")]
    width: u32,
    #[structopt(short = "h", long = "height")]
    height: u32,
}

fn main() {
    let opt = Opt::from_args();

    let image: Image = {
        let mut buffer = String::new();
        exit_on_failed_assertion(
            io::stdin().read_to_string(&mut buffer),
            "Error reading input",
        );

        Image::from_str(&buffer.trim(), opt.width, opt.height)
    };

    if opt.checksum_mode {
        println!("{}", image.checksum());
    } else {
        println!("{}", image);
    }
}

fn exit_on_failed_assertion<A, E: std::error::Error>(data: Result<A, E>, message: &str) -> A {
    match data {
        Ok(data) => data,
        Err(e) => {
            eprintln!("{}: {}", message, e);
            process::exit(1);
        }
    }
}

#[derive(Debug)]
struct Image {
    width: u32,
    height: u32,
    layers: Vec<ImageLayer>,
}

impl fmt::Display for Image {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.flatten()
            .pixels
            .chunks(self.width as usize)
            .map(|line| {
                line.iter()
                    .map(|c| write!(f, "{}", if *c == 0 { ' ' } else { '1' }))
                    .collect::<fmt::Result>()
                    .and_then(|_| writeln!(f))
            })
            .collect()
    }
}

impl Image {
    fn from_str(s: &str, width: u32, height: u32) -> Image {
        Image {
            width,
            height,
            layers: s
                .as_bytes()
                .chunks((width * height) as usize)
                .map(|chunk| ImageLayer::new(chunk))
                .collect(),
        }
    }

    fn checksum(&self) -> usize {
        self.layers
            .iter()
            .min_by_key(|layer| layer.pixel_count(0))
            .map(|layer| layer.pixel_count(1) * layer.pixel_count(2))
            .unwrap_or(0)
    }

    fn flatten(&self) -> ImageLayer {
        self.layers
            .iter()
            .fold(ImageLayer::empty(self.width, self.height), |acc, next| {
                ImageLayer {
                    pixels: acc
                        .pixels
                        .iter()
                        .zip(next.pixels.iter())
                        .map(|(a, b)| if *a == 2 { *b } else { *a })
                        .collect(),
                }
            })
    }
}

#[derive(Debug)]
struct ImageLayer {
    pixels: Vec<u8>,
}

impl ImageLayer {
    fn new(char_pixels: &[u8]) -> ImageLayer {
        ImageLayer {
            pixels: char_pixels
                .iter()
                .map(|c| c.overflowing_sub(b'0').0)
                .collect(),
        }
    }

    fn empty(width: u32, height: u32) -> ImageLayer {
        ImageLayer {
            pixels: vec![2; (width * height) as usize],
        }
    }

    fn pixel_count(&self, value: u8) -> usize {
        self.pixels.iter().filter(|p| **p == value).count()
    }
}