mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 13:20:32 +01:00
Brute force day 22 part 1
This commit is contained in:
@@ -1,9 +1,104 @@
|
||||
use std::io::Read;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
pub fn part1(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::character::complete::newline;
|
||||
use nom::combinator::map;
|
||||
use nom::multi::separated_list1;
|
||||
use nom::sequence::preceded;
|
||||
use nom::sequence::separated_pair;
|
||||
use nom::sequence::tuple;
|
||||
use nom::IResult;
|
||||
|
||||
use crate::common::read_input;
|
||||
|
||||
type CRange = RangeInclusive<i32>;
|
||||
|
||||
fn parse_range(input: &[u8]) -> IResult<&[u8], CRange> {
|
||||
use nom::character::complete::i32;
|
||||
|
||||
map(separated_pair(i32, tag(".."), i32), |(first, last)| {
|
||||
first..=last
|
||||
})(input)
|
||||
}
|
||||
|
||||
fn parse_input<'a>(input: &[u8]) -> IResult<&[u8], Vec<(bool, CRange, CRange, CRange)>> {
|
||||
let parse_state = alt((map(tag("on x="), |_| true), map(tag("off x="), |_| false)));
|
||||
let parse_line = tuple((
|
||||
parse_state,
|
||||
parse_range,
|
||||
preceded(tag(",y="), parse_range),
|
||||
preceded(tag(",z="), parse_range),
|
||||
));
|
||||
|
||||
separated_list1(newline, parse_line)(input)
|
||||
}
|
||||
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
const MAX_ABS_VAL: i32 = 50;
|
||||
const SIDE_LEN: usize = 2 * (MAX_ABS_VAL as usize) + 1;
|
||||
|
||||
let mut state = [[0u128; SIDE_LEN]; SIDE_LEN];
|
||||
|
||||
let valid_range = -MAX_ABS_VAL..=MAX_ABS_VAL;
|
||||
|
||||
let ranges = read_input(input, parse_input);
|
||||
|
||||
for (toggle, xr, yr, zr) in ranges {
|
||||
for z in zr {
|
||||
if !valid_range.contains(&z) {
|
||||
continue;
|
||||
}
|
||||
for y in yr.clone() {
|
||||
if !valid_range.contains(&y) {
|
||||
continue;
|
||||
}
|
||||
let row = &mut state[(z + MAX_ABS_VAL) as usize][(y + MAX_ABS_VAL) as usize];
|
||||
|
||||
for x in xr.clone() {
|
||||
if !valid_range.contains(&x) {
|
||||
continue;
|
||||
}
|
||||
let bit = 1 << (x + MAX_ABS_VAL);
|
||||
|
||||
if toggle {
|
||||
*row |= bit;
|
||||
} else {
|
||||
*row &= !bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|val| val.count_ones())
|
||||
.sum::<u32>()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn part2(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test_implementation;
|
||||
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("samples/22.txt");
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
test_implementation(part1, SAMPLE, 590784);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part2() {
|
||||
// test_implementation(part2, SAMPLE, 230)
|
||||
}
|
||||
}
|
||||
|
||||
22
2021/src/samples/22.txt
Normal file
22
2021/src/samples/22.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
on x=-20..26,y=-36..17,z=-47..7
|
||||
on x=-20..33,y=-21..23,z=-26..28
|
||||
on x=-22..28,y=-29..23,z=-38..16
|
||||
on x=-46..7,y=-6..46,z=-50..-1
|
||||
on x=-49..1,y=-3..46,z=-24..28
|
||||
on x=2..47,y=-22..22,z=-23..27
|
||||
on x=-27..23,y=-28..26,z=-21..29
|
||||
on x=-39..5,y=-6..47,z=-3..44
|
||||
on x=-30..21,y=-8..43,z=-13..34
|
||||
on x=-22..26,y=-27..20,z=-29..19
|
||||
off x=-48..-32,y=26..41,z=-47..-37
|
||||
on x=-12..35,y=6..50,z=-50..-2
|
||||
off x=-48..-32,y=-32..-16,z=-15..-5
|
||||
on x=-18..26,y=-33..15,z=-7..46
|
||||
off x=-40..-22,y=-38..-28,z=23..41
|
||||
on x=-16..35,y=-41..10,z=-47..6
|
||||
off x=-32..-23,y=11..30,z=-14..3
|
||||
on x=-49..-5,y=-3..45,z=-29..18
|
||||
off x=18..30,y=-20..-8,z=-3..13
|
||||
on x=-41..9,y=-7..43,z=-33..15
|
||||
on x=-54112..-39298,y=-85059..-49293,z=-27449..7877
|
||||
on x=967..23432,y=45373..81175,z=27513..53682
|
||||
Reference in New Issue
Block a user