Finished day 6 aoc 2015
This commit is contained in:
@@ -26,18 +26,18 @@ struct Instruction<T> {
|
|||||||
second_coord: Coord<T>,
|
second_coord: Coord<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input(input: &str) -> IResult<&str, Vec<Instruction<u64>>> {
|
fn parse_input(input: &str) -> IResult<&str, Vec<Instruction<i64>>> {
|
||||||
let (input, result) = separated_list1(
|
let (input, result) = separated_list1(
|
||||||
multispace1,
|
multispace1,
|
||||||
tuple((
|
tuple((
|
||||||
alt((tag("toggle "), tag("turn on "), tag("turn off "))),
|
alt((tag("toggle "), tag("turn on "), tag("turn off "))),
|
||||||
complete::u64,
|
complete::i64,
|
||||||
tag(","),
|
tag(","),
|
||||||
complete::u64,
|
complete::i64,
|
||||||
tag(" through "),
|
tag(" through "),
|
||||||
complete::u64,
|
complete::i64,
|
||||||
tag(","),
|
tag(","),
|
||||||
complete::u64,
|
complete::i64,
|
||||||
))
|
))
|
||||||
.map(|(switch, x_1, _, y_1, _, x_2, _, y_2)| Instruction {
|
.map(|(switch, x_1, _, y_1, _, x_2, _, y_2)| Instruction {
|
||||||
switch: match switch {
|
switch: match switch {
|
||||||
@@ -54,7 +54,7 @@ fn parse_input(input: &str) -> IResult<&str, Vec<Instruction<u64>>> {
|
|||||||
Ok((input, result))
|
Ok((input, result))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn solve_first(input: &[Instruction<u64>]) -> usize {
|
fn solve_first(input: &[Instruction<i64>]) -> usize {
|
||||||
let mut lights = vec![false; GRID_MAX * GRID_MAX];
|
let mut lights = vec![false; GRID_MAX * GRID_MAX];
|
||||||
|
|
||||||
for instruction in input.iter() {
|
for instruction in input.iter() {
|
||||||
@@ -75,6 +75,25 @@ fn solve_first(input: &[Instruction<u64>]) -> usize {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn solve_second(input: &[Instruction<i64>]) -> 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() {
|
fn main() {
|
||||||
println!("Hello, this is Patrick!");
|
println!("Hello, this is Patrick!");
|
||||||
|
|
||||||
@@ -84,4 +103,8 @@ fn main() {
|
|||||||
let first_result = solve_first(&light_instructions[..]);
|
let first_result = solve_first(&light_instructions[..]);
|
||||||
|
|
||||||
println!("The number of lit light is {}", first_result);
|
println!("The number of lit light is {}", first_result);
|
||||||
|
|
||||||
|
let second_result = solve_second(&light_instructions[..]);
|
||||||
|
|
||||||
|
println!("The total brightness is {}", second_result);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user