Finished part 1 day 6 aoc 2015, brute force is fast here??
This commit is contained in:
87
advent_of_code/2015/6/src/main.rs
Normal file
87
advent_of_code/2015/6/src/main.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
character::complete::{self, multispace1},
|
||||
multi::separated_list1,
|
||||
sequence::tuple,
|
||||
IResult, Parser,
|
||||
};
|
||||
|
||||
const GRID_MAX: usize = 1000;
|
||||
|
||||
enum LightSwitch {
|
||||
On,
|
||||
Off,
|
||||
Toggle,
|
||||
}
|
||||
|
||||
struct Coord<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
}
|
||||
|
||||
struct Instruction<T> {
|
||||
switch: LightSwitch,
|
||||
first_coord: Coord<T>,
|
||||
second_coord: Coord<T>,
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> IResult<&str, Vec<Instruction<u64>>> {
|
||||
let (input, result) = separated_list1(
|
||||
multispace1,
|
||||
tuple((
|
||||
alt((tag("toggle "), tag("turn on "), tag("turn off "))),
|
||||
complete::u64,
|
||||
tag(","),
|
||||
complete::u64,
|
||||
tag(" through "),
|
||||
complete::u64,
|
||||
tag(","),
|
||||
complete::u64,
|
||||
))
|
||||
.map(|(switch, x_1, _, y_1, _, x_2, _, y_2)| Instruction {
|
||||
switch: match switch {
|
||||
"toggle " => LightSwitch::Toggle,
|
||||
"turn on " => LightSwitch::On,
|
||||
"turn off " => LightSwitch::Off,
|
||||
_ => unreachable!(),
|
||||
},
|
||||
first_coord: Coord { x: x_1, y: y_1 },
|
||||
second_coord: Coord { x: x_2, y: y_2 },
|
||||
}),
|
||||
)(input)?;
|
||||
|
||||
Ok((input, result))
|
||||
}
|
||||
|
||||
fn solve_first(input: &[Instruction<u64>]) -> usize {
|
||||
let mut lights = vec![false; 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 {
|
||||
lights[x as usize + y as usize * GRID_MAX] = match instruction.switch {
|
||||
LightSwitch::Toggle => !lights[x as usize + y as usize * GRID_MAX],
|
||||
LightSwitch::Off => false,
|
||||
LightSwitch::On => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lights.into_iter().fold(0, |acc, e| match e {
|
||||
true => acc + 1,
|
||||
false => acc,
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, this is Patrick!");
|
||||
|
||||
let input_txt = include_str!("../input.txt");
|
||||
let (_, light_instructions) = parse_input(input_txt).unwrap();
|
||||
|
||||
let first_result = solve_first(&light_instructions[..]);
|
||||
|
||||
println!("The number of lit light is {}", first_result);
|
||||
}
|
||||
Reference in New Issue
Block a user