diff --git a/advent_of_code/2015/6/src/main.rs b/advent_of_code/2015/6/src/main.rs index 1c88622..02cf79b 100644 --- a/advent_of_code/2015/6/src/main.rs +++ b/advent_of_code/2015/6/src/main.rs @@ -26,18 +26,18 @@ struct Instruction { second_coord: Coord, } -fn parse_input(input: &str) -> IResult<&str, Vec>> { +fn parse_input(input: &str) -> IResult<&str, Vec>> { let (input, result) = separated_list1( multispace1, tuple(( alt((tag("toggle "), tag("turn on "), tag("turn off "))), - complete::u64, + complete::i64, tag(","), - complete::u64, + complete::i64, tag(" through "), - complete::u64, + complete::i64, tag(","), - complete::u64, + complete::i64, )) .map(|(switch, x_1, _, y_1, _, x_2, _, y_2)| Instruction { switch: match switch { @@ -54,7 +54,7 @@ fn parse_input(input: &str) -> IResult<&str, Vec>> { Ok((input, result)) } -fn solve_first(input: &[Instruction]) -> usize { +fn solve_first(input: &[Instruction]) -> usize { let mut lights = vec![false; GRID_MAX * GRID_MAX]; for instruction in input.iter() { @@ -75,6 +75,25 @@ fn solve_first(input: &[Instruction]) -> usize { }) } +fn solve_second(input: &[Instruction]) -> i64 { + let mut lights = vec![0; GRID_MAX * GRID_MAX]; + + for instruction in input.iter() { + for y in instruction.first_coord.y..=instruction.second_coord.y { + for x in instruction.first_coord.x..=instruction.second_coord.x { + let pos = x as usize + y as usize * GRID_MAX; + lights[pos] = match instruction.switch { + LightSwitch::Toggle => lights[pos] + 2, + LightSwitch::Off => std::cmp::max(0, lights[pos] - 1), + LightSwitch::On => lights[pos] + 1, + } + } + } + } + + lights.into_iter().sum() +} + fn main() { println!("Hello, this is Patrick!"); @@ -84,4 +103,8 @@ fn main() { let first_result = solve_first(&light_instructions[..]); println!("The number of lit light is {}", first_result); + + let second_result = solve_second(&light_instructions[..]); + + println!("The total brightness is {}", second_result); }