Table of content
What is this
The rules of the game are explained in my original post .
30th Challenge
Challenge
Today I’m solving last year’s AoC day 3 2020 using rust macros whenever possible.
In yesterday’s solution there was a small bug in my code, I will try to solve that today.
Solution
The issue was with slope number four. I repeated the challenge for just number four
println! ( "{}" , INPUT .lines ()
.enumerate ()
.filter (| x | { ( x .0 % 2 == 0 ) && ( x .1 .chars () .nth (( x .0 / 2 ) % 31 ) .unwrap () == '#' )})
.count ());
From this huntch I was able to solve the issue.
fn main () {
println! ( "product: {}" , slope_product! ( INPUT , 0 , 1 , 2 , 3 , 4 ));
}
#[macro_export]
macro_rules! slope_product {
( $input:expr , $ ( $x:expr ), * ) => {
{
let mut prod = 1 ;
$ (
println! ( "slope: {}" , slope! ( $x , $input ));
prod *= slope! ( $x , $input );
) *
prod
}
};
}
#[macro_export]
macro_rules! slope {
( $slope:expr , $input:expr ) => {
{
$input .lines ()
.enumerate ()
.filter (| x | { is_tree! ( $slope , x .0 , x .1 )})
.count ()
}
};
}
#[macro_export]
macro_rules! is_tree {
( $slope:expr , $i:expr , $s:expr ) => {
{
match $slope {
4 => {(( $i % 2 ) == 0 ) && $s .chars () .nth (( $i / 2 ) % $s .chars () .count ()) .unwrap () == '#' },
_ => $s .chars () .nth (( $i * ( 1 + $slope * 2 )) % $s .chars () .count ()) .unwrap () == '#'
}
}
};
}
const INPUT : & str = "..##.........##.........##.........##.........##.........##.......
#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
..#.##.......#.##.......#.##.......#.##.......#.##.......#.##.....
.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
.#........#.#........#.#........#.#........#.#........#.#........#
#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...
#...##....##...##....##...##....##...##....##...##....##...##....#
.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#
" ;
To see the full source see github and see the live demo in playground .