advent-of-code/aoc2015/src/day06.rs

211 lines
5.3 KiB
Rust
Raw Normal View History

use std::fmt::Write;
2019-12-05 19:13:21 +01:00
use std::str::FromStr;
2020-12-14 20:04:47 +01:00
use anyhow::{bail, Context, Result};
2019-12-05 19:13:21 +01:00
const INPUT: &str = include_str!("../input/day06.txt");
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
2019-12-05 19:13:21 +01:00
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
2019-12-05 19:13:21 +01:00
}
fn part1(input: &str) -> Result<usize> {
let instructions = input
.lines()
.map(|line| {
line.trim_end()
.parse()
2020-12-14 20:04:47 +01:00
.context("couldn't parse instruction: {}")
2019-12-05 19:13:21 +01:00
})
.collect::<Result<Vec<Instruction>>>()?;
let mut grid: Vec<Vec<bool>> = vec![vec![false; 1000]; 1000];
for inst in instructions {
match inst.action {
Action::TurnOn => {
2019-12-05 20:23:42 +01:00
for (x, y) in inst {
grid[x][y] = true;
}
2019-12-05 19:13:21 +01:00
}
Action::TurnOff => {
2019-12-05 20:23:42 +01:00
for (x, y) in inst {
grid[x][y] = false;
}
2019-12-05 19:13:21 +01:00
}
Action::Toggle => {
2019-12-05 20:23:42 +01:00
for (x, y) in inst {
grid[x][y] = !grid[x][y];
}
2019-12-05 19:13:21 +01:00
}
}
}
Ok(grid
.iter()
.flat_map(|line| line.iter())
.filter(|v| **v)
.count())
}
fn part2(input: &str) -> Result<u64> {
let instructions = input
.lines()
.map(|line| {
line.trim_end()
.parse()
2020-12-14 20:04:47 +01:00
.context("couldn't parse instruction: {}")
2019-12-05 19:13:21 +01:00
})
.collect::<Result<Vec<Instruction>>>()?;
let mut grid: Vec<Vec<u64>> = vec![vec![0; 1000]; 1000];
for inst in instructions {
match inst.action {
Action::TurnOn => {
2019-12-05 20:23:42 +01:00
for (x, y) in inst {
grid[x][y] = grid[x][y].saturating_add(1);
}
2019-12-05 19:13:21 +01:00
}
Action::TurnOff => {
2019-12-05 20:23:42 +01:00
for (x, y) in inst {
grid[x][y] = grid[x][y].saturating_sub(1);
}
2019-12-05 19:13:21 +01:00
}
Action::Toggle => {
2019-12-05 20:23:42 +01:00
for (x, y) in inst {
grid[x][y] = grid[x][y].saturating_add(2);
}
2019-12-05 19:13:21 +01:00
}
}
}
Ok(grid.iter().flat_map(|line| line.iter()).sum())
}
enum Action {
TurnOn,
TurnOff,
Toggle,
}
struct Instruction {
action: Action,
horizontal: (usize, usize),
vertical: (usize, usize),
}
2019-12-05 20:23:42 +01:00
impl IntoIterator for Instruction {
type Item = (usize, usize);
type IntoIter = Box<dyn Iterator<Item = Self::Item>>;
fn into_iter(self) -> Self::IntoIter {
let begin_x = self.horizontal.0;
let end_x = self.horizontal.1;
let begin_y = self.vertical.0;
let end_y = self.vertical.1;
let iter = (begin_x..=end_x).flat_map(move |i| (begin_y..=end_y).map(move |j| (i, j)));
Box::new(iter)
}
}
2019-12-05 19:13:21 +01:00
impl FromStr for Instruction {
2020-12-14 20:04:47 +01:00
type Err = anyhow::Error;
2019-12-05 19:13:21 +01:00
fn from_str(s: &str) -> Result<Self> {
let mut space = s
.find(' ')
2020-12-14 20:04:47 +01:00
.with_context(|| format!("couldn't parse instruction: {}", s))?;
2019-12-05 19:13:21 +01:00
let action = if &s[..space] == "toggle" {
Action::Toggle
} else if &s[..space] == "turn" {
space = s[(space + 1)..]
.find(' ')
2020-12-14 20:04:47 +01:00
.with_context(|| format!("couldn't parse instruction: {}", s))?
2019-12-05 19:13:21 +01:00
+ space
+ 1;
if &s[..space] == "turn on" {
Action::TurnOn
} else if &s[..space] == "turn off" {
Action::TurnOff
} else {
2020-12-14 20:04:47 +01:00
bail!("couldn't identify action: {}", &s[..space]);
2019-12-05 19:13:21 +01:00
}
} else {
2020-12-14 20:04:47 +01:00
bail!("couldn't identify action: {}", s);
2019-12-05 19:13:21 +01:00
};
let s = &s[(space + 1)..];
let comma = s
.find(',')
2020-12-14 20:04:47 +01:00
.with_context(|| format!("couldn't parse instruction: {}", s))?;
2019-12-05 19:13:21 +01:00
let x = s[..comma].parse()?;
let s = &s[(comma + 1)..];
let space = s
.find(' ')
2020-12-14 20:04:47 +01:00
.with_context(|| format!("couldn't parse instruction: {}", s))?;
2019-12-05 19:13:21 +01:00
let y = s[..space].parse()?;
let s = &s[(space + 1)..];
let begin = (x, y);
let through = s
.find("through ")
2020-12-14 20:04:47 +01:00
.with_context(|| format!("couldn't parse instruction: {}", s))?;
2019-12-05 19:13:21 +01:00
let s = &s[(through + 8)..];
let comma = s
.find(',')
2020-12-14 20:04:47 +01:00
.with_context(|| format!("couldn't parse instruction: {}", s))?;
2019-12-05 19:13:21 +01:00
let x = s[..comma].parse()?;
let s = &s[(comma + 1)..];
let y = s.parse()?;
let end = (x, y);
Ok(Instruction {
action,
horizontal: (begin.0, end.0),
vertical: (begin.1, end.1),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_provided() {
assert_eq!(part1("turn on 0,0 through 999,999").unwrap(), 1_000_000);
assert_eq!(part1("toggle 0,0 through 999,0").unwrap(), 1_000);
assert_eq!(part1("turn off 499,499 through 500,500").unwrap(), 0);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 543903);
}
#[test]
fn part2_provided() {
assert_eq!(part2("turn on 0,0 through 0,0").unwrap(), 1);
assert_eq!(part2("toggle 0,0 through 999,999").unwrap(), 2_000_000);
}
#[test]
fn part2_real() {
assert_eq!(part2(INPUT).unwrap(), 14687245);
}
}