Golfing Advent of Code 2020, Day 3

J, 30 26 23 22 bytes

0{0{[:+/|.^:(<@%&{.~$)

Try it online!

Note: This solution works for any grid

J supports multi-dimensional rotation, which reduces the problem almost to a single summation. To clarify what's happening, assume the board looks like this:

 0  1  2  3  4
 5  6  7  8  9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
35 36 37 38 39

Now we rotate the columns left and the rows up |. by 1 3, iteratively, 3 times:

┌──────────────┬──────────────┬──────────────┐
│ 0  1  2  3  4│ 8  9  5  6  7│11 12 13 14 10│
│ 5  6  7  8  9│13 14 10 11 12│16 17 18 19 15│
│10 11 12 13 14│18 19 15 16 17│21 22 23 24 20│
│15 16 17 18 19│23 24 20 21 22│26 27 28 29 25│
│20 21 22 23 24│28 29 25 26 27│31 32 33 34 30│
│25 26 27 28 29│33 34 30 31 32│36 37 38 39 35│
│30 31 32 33 34│38 39 35 36 37│ 1  2  3  4  0│
│35 36 37 38 39│ 3  4  0  1  2│ 6  7  8  9  5│
└──────────────┴──────────────┴──────────────┘

We are bringing the sled's "new position" to the top-left corner every time, rather than moving a pointer within a fixed grid and adjusting indexes with modular arithmetic.

Once we have every iteration like this, we need only "sum the planes" element-wise [:+/, and then our answer will the number in the top left corner of the resulting matrix 0{0{.

The only piece left is to determine how many times we need to iterate. This amounts to dividing the first element of our "step" vector (the down part) into the height of the input matrix: %&{.~$.

old approach, 26 bytes

1#.]{~[:;/$@]|"1#\@]*($~#)

Try it online!


05AB1E, 9 bytes

Takes y, grid and x as seperate inputs where grid is a 2d-list of 0/1.

ιнε³N*è}O

Try it online! Convert a grid into the required format with this Retina script.

ι          # un-interleave the grid with step y
 н         # take the first element grid[0::y]
  ε    }   # map over each row
   ³N*     #   multiply x with 0-based iteration index N
      è    #   index into row (modular)
        O  # sum the values

Scala, 47 44 bytes

-3 bytes thanks to user!

When replacing the ./# grid with a 0/1 one:

(x,y,z)=>1 to 10/y map(i=>z(i*y)(i*x%11))sum

Try it online!


77..62 60 bytes

using a ./# grid:

(x,y,z)=>1 to 10/y map(i=>if(z(i*y)(i*x%11)==35)1 else 0)sum

Try it online!

Explanation:

(x, y, z) =>                  // x: steps to the right, y: steps to the bottoms, z: grid as a String sequence
  (1 to 10 / y)               // range r := [1;10/y]
    .map (                    // override every entry in r
      i => if (               // every entry i in r if
        z(i * y)(i * x % 11)  // the character from the grid that is at the position (i * y, i * x),
                              // because i * x can get bigger than the number of columns modulo (%) it by 11
                              // so the correct position is: (i * y, i * x % 11)
          == 35               // is equal to 35 (ASCII numeric value for '#')
      ) 1 else 0              // with 1 else 0
    ) sum                     // sum over all entries

Example: (5, 3, grid):

  val grid = Seq(
    "..##.......",
    "#...#...#..",
    ".#....#..#.",
    "..#.#...#.#",
    ".#...##..#.",
    "..#.##.....",
    ".#.#.#....#",
    ".#........#",
    "#.##...#...",
    "#...##....#",
    ".#..#...#.#"
  )
   Steps                   | Sequence
   ------------------------|---------
   init                    | [1, 2, 3]
   i = 1; z(3)(5)  == '.'  | [0, 2, 3]
   i = 2; z(6)(10) == '#'  | [0, 1, 3]
   i = 3; z(9)(4)  == '#'  | [0, 1, 1]
   sum == 2

Tags:

Grid

Code Golf