Compare commits

...

2 Commits

3 changed files with 21 additions and 38 deletions

View File

@@ -7,7 +7,7 @@ pub fn solve() {
adapters.sort(); adapters.sort();
let diffs = count_diffs(&adapters); let diffs = count_diffs(&adapters);
println!("{:?}", diffs); // println!("{:?}", diffs);
let part1 = (diffs[0] + 1) * (diffs[2] + 1); let part1 = (diffs[0] + 1) * (diffs[2] + 1);
println!("Day 10 Part 1: {}", part1); println!("Day 10 Part 1: {}", part1);

View File

@@ -2,18 +2,17 @@ use super::util;
use crate::day11::GridState::Floor; use crate::day11::GridState::Floor;
use crate::day11::GridState::FilledSeat; use crate::day11::GridState::FilledSeat;
use crate::day11::GridState::EmptySeat; use crate::day11::GridState::EmptySeat;
use usize_cast::{IntoUsize, FromUsize, IntoIsize, FromIsize};
pub fn solve() { pub fn solve() {
let lines = util::read_file("input/day11.txt"); let lines = util::read_file("input/day11.txt");
let seats = Seats::parse(&lines); let seats = Seats::parse(&lines, 1);
let visible_chairs = seats.calculate_all_visible_chairs();
let part1 = solve_part1(&seats); let part1 = solve_part1(&seats);
println!("Day 11 Part 1: {}", part1); println!("Day 11 Part 1: {}", part1);
let visible_chairs = seats.calculate_all_visible_chairs();
let part2 = solve_part2(&seats, &visible_chairs); let part2 = solve_part2(&seats, &visible_chairs);
println!("Day 11 Part 2: {}", part2); println!("Day 11 Part 2: {}", part2);
@@ -26,8 +25,8 @@ fn solve_part1(seats: &Seats) -> usize {
loop { loop {
let mut changes = false; let mut changes = false;
for y in 0..seats.height { for y in 1..seats.height-1 {
for x in 0..seats.width { for x in 1..seats.width-1 {
let seat = last_seats.get(&x, &y); let seat = last_seats.get(&x, &y);
if seat != &Floor { if seat != &Floor {
let occupied = last_seats.count_occupied_neighbors(&x, &y); let occupied = last_seats.count_occupied_neighbors(&x, &y);
@@ -67,8 +66,8 @@ fn solve_part2(seats: &Seats, visible_chairs: &Vec<Option<Vec<(usize, usize)>>>)
changes = true; changes = true;
next_seats.set(&x, &y, FilledSeat); next_seats.set(&x, &y, FilledSeat);
} else if seat == &FilledSeat && occupied >= 5 { } else if seat == &FilledSeat && occupied >= 5 {
next_seats.set(&x, &y, EmptySeat);
changes = true; changes = true;
next_seats.set(&x, &y, EmptySeat);
} }
} }
} }
@@ -95,22 +94,20 @@ impl Seats {
const DIRECTIONS: [(i32, i32); 8] = [(-1, 1), (-1, 0), (-1, -1), (0, -1), (0, 1), (1, 1), (1, 0), (1, -1)]; const DIRECTIONS: [(i32, i32); 8] = [(-1, 1), (-1, 0), (-1, -1), (0, -1), (0, 1), (1, 1), (1, 0), (1, -1)];
fn parse(lines: &Vec<String>) -> Seats { fn parse(lines: &Vec<String>, padding: usize) -> Seats {
let height = lines.len(); let height = lines.len() + padding * 2;
let width = lines[0].len(); let width = lines[0].len() + padding * 2;
let mut grid = vec!(GridState::Floor; width * height); let mut grid = vec!(GridState::Floor; width * height);
for y in 0..lines.len() { for y in 0..lines.len() {
let line = &lines[y]; let line = &lines[y];
let mut line_chars = line.chars(); let mut line_chars = line.chars();
for x in 0..lines[0].len() { for x in 0..lines[0].len() {
grid[x + y * width] = GridState::parse(&line_chars.next().unwrap()) grid[(x + padding) + (y + padding) * width] = GridState::parse(&line_chars.next().unwrap())
} }
} }
let mut seats = Seats { grid, width, height }; return Seats { grid, width, height };
seats.calculate_all_visible_chairs();
return seats;
} }
fn calculate_all_visible_chairs(&self) -> Vec<Option<Vec<(usize, usize)>>> { fn calculate_all_visible_chairs(&self) -> Vec<Option<Vec<(usize, usize)>>> {
@@ -141,10 +138,9 @@ impl Seats {
fn raycast(&self, origin_x: &usize, origin_y: &usize, direction: &(i32, i32)) -> Option<(usize, usize)> { fn raycast(&self, origin_x: &usize, origin_y: &usize, direction: &(i32, i32)) -> Option<(usize, usize)> {
let mut hit = false;
let mut x = *origin_x as i32; let mut x = *origin_x as i32;
let mut y = *origin_y as i32; let mut y = *origin_y as i32;
while !hit { loop {
x = x + direction.0; x = x + direction.0;
@@ -158,7 +154,6 @@ impl Seats {
return Option::Some((x as usize, y as usize)); return Option::Some((x as usize, y as usize));
} }
} }
return Option::None;
} }
fn is_valid(&self, x: &i32, y: &i32) -> bool { fn is_valid(&self, x: &i32, y: &i32) -> bool {
@@ -175,20 +170,15 @@ impl Seats {
fn count_occupied_neighbors(&self, x: &usize, y: &usize) -> u8 { fn count_occupied_neighbors(&self, x: &usize, y: &usize) -> u8 {
let x_upper = x == &(self.width - &ONE);
let x_lower = x == &0;
let y_upper = y == &(self.height - &ONE);
let y_lower = y == &0;
return return
if !x_lower && !y_lower && self.is_occupied(&(x - &ONE), &(y - &ONE)) { 1 } else { 0 } + if self.is_occupied(&(x - &ONE), &(y - &ONE)) { 1 } else { 0 } +
if !x_lower && self.is_occupied(&(x - &ONE), &y) { 1 } else { 0 } + if self.is_occupied(&(x - &ONE), &y) { 1 } else { 0 } +
if !x_lower && !y_upper && self.is_occupied(&(x - &ONE), &(y + ONE)) { 1 } else { 0 } + if self.is_occupied(&(x - &ONE), &(y + ONE)) { 1 } else { 0 } +
if !y_lower && self.is_occupied(&x, &(y - ONE)) { 1 } else { 0 } + if self.is_occupied(&x, &(y - ONE)) { 1 } else { 0 } +
if !y_upper && self.is_occupied(&x, &(y + ONE)) { 1 } else { 0 } + if self.is_occupied(&x, &(y + ONE)) { 1 } else { 0 } +
if !x_upper && !y_lower && self.is_occupied(&(x + ONE), &(y - ONE)) { 1 } else { 0 } + if self.is_occupied(&(x + ONE), &(y - ONE)) { 1 } else { 0 } +
if !x_upper && self.is_occupied(&(x + ONE), &y) { 1 } else { 0 } + if self.is_occupied(&(x + ONE), &y) { 1 } else { 0 } +
if !x_upper && !y_upper && self.is_occupied(&(x + ONE), &(y + ONE)) { 1 } else { 0 } if self.is_occupied(&(x + ONE), &(y + ONE)) { 1 } else { 0 }
} }
@@ -205,6 +195,7 @@ impl Seats {
self.grid[x + y * self.width] = state; self.grid[x + y * self.width] = state;
} }
#[allow(dead_code)]
fn print(&self) { fn print(&self) {
for y in 0..self.height { for y in 0..self.height {

View File

@@ -11,11 +11,3 @@ pub fn read_file(filename: &str) -> Vec<String> {
res res
} }
pub fn add_to_usize(u: usize, i: i32) -> usize {
if i.is_negative() {
u - i.wrapping_abs() as u32 as usize
} else {
u + i as usize
}
}