Probably a Fire Hazard

--- Day 6: Probably a Fire Hazard ---

Because your neighbors keep defeating you in the holiday house decorating contest year after year, you've decided to deploy one million lights in a 1000x1000 grid.

Furthermore, because you've been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration.

Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square. The lights all start turned off.

To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order.

For example:

• turn on 0,0 through 999,999 would turn on (or leave on) every light.
• toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off.
• turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights.

After following the instructions, how many lights are lit?

--- Part Two ---

You just finish implementing your winning light pattern when you realize you mistranslated Santa's message from Ancient Nordic Elvish.

The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero.

The phrase turn on actually means that you should increase the brightness of those lights by 1.

The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero.

The phrase toggle actually means that you should increase the brightness of those lights by 2.

What is the total brightness of all lights combined after following Santa's instructions?

For example:

• turn on 0,0 through 0,0 would increase the total brightness by 1.
• toggle 0,0 through 999,999 would increase the total brightness by 2000000.
Probably a Fire Hazard
use rustgym_util::*;
use std::fmt::Write;
use std::io::*;

let mut grid1: Vec<Vec<i32>> = vec![vec![0; 1000]; 1000];
let mut grid2: Vec<Vec<i32>> = vec![vec![0; 1000]; 1000];
for line in it {
let words: Vec<String> = line.split_whitespace().map(|s| s.to_string()).collect();
let data = if words == "on" || words == "off" {
(2, 4, if words == "on" { 1 } else { 0 })
} else {
(1, 3, 2)
};
let cord1: Vec<usize> = words[data.0]
.split(',')
.map(|s| s.parse::<usize>().unwrap())
.collect();
let cord2: Vec<usize> = words[data.1]
.split(',')
.map(|s| s.parse::<usize>().unwrap())
.collect();
light1(&mut grid1, data.2, &cord1, &cord2);
light2(&mut grid2, data.2, &cord1, &cord2);
}
let res1: i32 = grid1.iter().map(|r| r.iter().sum::<i32>()).sum();
let res2: i32 = grid2.iter().map(|r| r.iter().sum::<i32>()).sum();
writeln!(writer, "{}", res1).unwrap();
writeln!(writer, "{}", res2).unwrap();
}

fn light1(grid: &mut Vec<Vec<i32>>, cmd: i32, cord1: &[usize], cord2: &[usize]) {
let r1 = cord1.min(cord2);
let c1 = cord1.min(cord2);
let r2 = cord1.max(cord2);
let c2 = cord1.max(cord2);
for i in r1..=r2 {
for j in c1..=c2 {
if cmd == 2 {
grid[i][j] = 1 - grid[i][j];
} else {
grid[i][j] = cmd;
}
}
}
}

fn light2(grid: &mut Vec<Vec<i32>>, cmd: i32, cord1: &[usize], cord2: &[usize]) {
let r1 = cord1.min(cord2);
let c1 = cord1.min(cord2);
let r2 = cord1.max(cord2);
let c2 = cord1.max(cord2);
for i in r1..=r2 {
for j in c1..=c2 {
if cmd == 0 {
grid[i][j] -= 1;
grid[i][j] = grid[i][j].max(0);
} else {
grid[i][j] += cmd;
}
}
}
}