add day 6 part 1

This commit is contained in:
timeshifter
2026-06-07 20:51:06 +02:00
parent 07ac33c05a
commit f3cc4c7246
5 changed files with 437 additions and 0 deletions
+121
View File
@@ -0,0 +1,121 @@
use std::{str::FromStr, vec};
#[cfg(debug_assertions)]
const FILENAME: &str = "example.txt";
#[cfg(not(debug_assertions))]
const FILENAME: &str = "input.txt";
type Coordinate = (usize, usize);
type Rectangle = (Coordinate, Coordinate);
#[derive(Debug)]
enum Switch {
On(Rectangle),
Off(Rectangle),
Toggle(Rectangle),
}
#[derive(Debug)]
enum ParseLineError {}
impl std::fmt::Display for ParseLineError {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
impl std::error::Error for ParseLineError {}
impl FromStr for Switch {
type Err = ParseLineError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut split = s.split(" through ");
let pre = split.next().unwrap();
let post = split.next().unwrap();
let mut pre_nums = pre.split(' ').next_back().unwrap().split(',');
let x0 = pre_nums.next().unwrap().parse::<usize>().unwrap();
let y0 = pre_nums.next().unwrap().parse::<usize>().unwrap();
let mut post = post.split(',');
let x1 = post.next().unwrap().parse::<usize>().unwrap();
let y1 = post.next().unwrap().parse::<usize>().unwrap();
let rect: Rectangle = ((x0, y0), (x1, y1));
let binding = pre.chars().rev().collect::<String>();
let pre_actual = binding
.split_once(' ')
.iter()
.next_back()
.unwrap()
.1
.chars()
.rev()
.collect::<String>();
let instance = match pre_actual.as_str() {
"turn on" => Switch::On(rect),
"turn off" => Switch::Off(rect),
"toggle" => Switch::Toggle(rect),
_ => unreachable!(),
};
Ok(instance)
}
}
fn main() {
let data: Vec<Switch> = std::fs::read_to_string(FILENAME)
.unwrap()
.lines()
.map(|l| l.parse().unwrap())
.collect();
part_1(&data);
}
#[allow(clippy::needless_range_loop)]
fn part_1(data: &[Switch]) {
let mut grid = vec![vec![0; 1000]; 1000];
for task in data {
match task {
Switch::On(rect) => {
for x in rect.0.0..=rect.1.0 {
for y in rect.0.1..=rect.1.1 {
grid[x][y] = 1;
}
}
}
Switch::Off(rect) => {
for x in rect.0.0..=rect.1.0 {
for y in rect.0.1..=rect.1.1 {
grid[x][y] = 0;
}
}
}
Switch::Toggle(rect) => {
for x in rect.0.0..=rect.1.0 {
for y in rect.0.1..=rect.1.1 {
grid[x][y] ^= 1;
}
}
}
}
}
let mut sum = 0;
for x in 0..1000 {
for y in 0..1000 {
if grid[x][y] == 1 {
sum += 1;
}
}
}
println!("{sum}");
}