This commit is contained in:
2024-10-28 14:21:53 +01:00
parent 6c6b50dee0
commit a4ef4dfacf
5 changed files with 438 additions and 1 deletions

20
y2015/src/bin/d6.rs Normal file
View File

@@ -0,0 +1,20 @@
use std::fs;
use y2015::days::d6;
fn main() {
part1();
part2();
}
fn part1() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/6_input.txt")).unwrap();
println!("{}", d6::process_part1(&content));
}
fn part2() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/6_input.txt")).unwrap();
println!("{}", d6::process_part2(&content));
}

View File

@@ -67,7 +67,6 @@ pub fn process_part2(input: &str) -> i32 {
}
for (_, count) in pairs.into_iter() {
if count >= 2 {
println!("{}", String::from_utf8(line.to_vec()).unwrap());
return 1;
}
}

117
y2015/src/days/d6.rs Normal file
View File

@@ -0,0 +1,117 @@
pub fn process_part1(input: &str) -> u32 {
let mut grid = vec![vec![0; 1000]; 1000];
input.lines().for_each(|line| {
let words = line.split_whitespace().collect::<Vec<&str>>();
match words[0] {
"toggle" => {
let from = parse_coord(words[1]);
let to = parse_coord(words[3]);
toggle(&mut grid, from, to);
}
"turn" => {
let from = parse_coord(words[2]);
let to = parse_coord(words[4]);
match words[1] {
"off" => turn_off(&mut grid, from, to),
"on" => turn_on(&mut grid, from, to),
_ => panic!("Should not happen"),
};
}
_ => panic!("Should not happen"),
}
});
grid.iter().map(|line| line.iter().sum::<u32>()).sum()
}
fn parse_coord(coord: &str) -> (u32, u32) {
let (x, y) = coord.split_once(",").unwrap();
(x.parse().unwrap(), y.parse().unwrap())
}
fn toggle(grid: &mut [Vec<u32>], from: (u32, u32), to: (u32, u32)) {
for x in from.0..=to.0 {
let line = grid.get_mut(x as usize).unwrap();
for y in from.1..=to.1 {
if line[y as usize] == 1 {
line[y as usize] = 0;
} else {
line[y as usize] = 1;
}
}
}
}
fn turn_on(grid: &mut [Vec<u32>], from: (u32, u32), to: (u32, u32)) {
for x in from.0..=to.0 {
let line = grid.get_mut(x as usize).unwrap();
for y in from.1..=to.1 {
line[y as usize] = 1;
}
}
}
fn turn_off(grid: &mut [Vec<u32>], from: (u32, u32), to: (u32, u32)) {
for x in from.0..=to.0 {
let line = grid.get_mut(x as usize).unwrap();
for y in from.1..=to.1 {
line[y as usize] = 0;
}
}
}
fn adjust(grid: &mut [Vec<u32>], value: i32, from: (u32, u32), to: (u32, u32)) {
for x in from.0..=to.0 {
let line = grid.get_mut(x as usize).unwrap();
for y in from.1..=to.1 {
if value > 0 {
line[y as usize] = line[y as usize].saturating_add(value as u32);
} else {
line[y as usize] = line[y as usize].saturating_sub(value.unsigned_abs());
}
}
}
}
pub fn process_part2(input: &str) -> u32 {
let mut grid = vec![vec![0; 1000]; 1000];
input.lines().for_each(|line| {
let words = line.split_whitespace().collect::<Vec<&str>>();
match words[0] {
"toggle" => {
let from = parse_coord(words[1]);
let to = parse_coord(words[3]);
adjust(&mut grid, 2, from, to);
}
"turn" => {
let from = parse_coord(words[2]);
let to = parse_coord(words[4]);
match words[1] {
"off" => adjust(&mut grid, -1, from, to),
"on" => adjust(&mut grid, 1, from, to),
_ => panic!("Should not happen"),
};
}
_ => panic!("Should not happen"),
}
});
grid.iter().map(|line| line.iter().sum::<u32>()).sum()
}
#[cfg(test)]
mod tests_6 {
use super::*;
const INPUT1: &str = "turn on 0,0 through 999,999";
const INPUT2: &str = "toggle 0,0 through 999,0";
const INPUT3: &str = "turn off 499,499 through 500,500";
#[test]
fn it_works() {
let result = process_part1(INPUT1);
assert_eq!(result, 1_000_000);
let result = process_part1(format!("{INPUT1}\n{INPUT2}").as_str());
assert_eq!(result, 999_000);
let result = process_part1(format!("{INPUT1}\n{INPUT2}\n{INPUT3}").as_str());
assert_eq!(result, 998_996);
}
}

View File

@@ -3,3 +3,4 @@ pub mod d2;
pub mod d3;
pub mod d4;
pub mod d5;
pub mod d6;