# 2D Array - DS : Day 1 of #100DaysOfHackerrank

Azura Sakan Taufik / September 13, 2021

5 min read

Day 1 of my own Hackerrank Challenge. I'm planning to do at least one problem each day for the 100 days, doesn't really have to be consecutive, though it'd be great If can, and write how I came up with the solution. I think this could be a great way to up-skill both my programming & technical writing skills. I will try to my best ability to solve it myself first. If can't then I will tell you guys & try to review other people's solution together.

P.S I've been using Swift for iOS development for awhile now, solving logical problems that revolves around the basic concepts of Swift is still somehwat new to me. I'm used to using Swift in an OOP environment so this will be a challenge. I'm excited to learn and discover more about this language through this journey. With that in mind, let's hop on this journey with me! ðŸ™‹

Today I solved the 2D Array - DS problem.

### Overview

Level : Easy Max Score : 15 pts Test Cases Passed : 9/9 My Score : 15 pts

### Problem

- Given an 6x6 2D Matrix, we can find 16 different hourglass pattern like this

```
a b c
d
e f g
```

### Find

Find the sum of all 16 hourglasses and return the highest sum.

### Let's Define

- Input will be a 2D, 6x6 array
- Output will be an integer

Since I'm a fresh graduate âœ¨ out of uni, and for... the last 4 semesters we're focused on the other parts of programming, like developing a game, mobile apps, websites, so I'm kind of rusty in this area. I initially tried to solve the problem by coding right away but after 15 minutes in and starting to feel like I have no way out, I resolved to writing and breaking down what I know.

I'll show you my thought process through my notes

**Step 1 : Draw the Matrix**

Since the matrix was always going to be 6 by 6, I tried drawing it fully and use the first 'hourglass' as a starter. I ended up with the right side and start with the index. But somehow it wasn't clear enough for me, so I tried doing the whole matrix with all the rows and columns index written out. I was like 30 minutes in at this point and as you can see, I was starting to feel frustrated witht that scribble on the bottom left ahaha. But, through this I learned something important.

**Step 2 : Draw the WHOLE Matrix**

I learned the only possible starting positions to achieve this hourglass shape was if we start on column 1 through 4 and row 0 through 3. This makes sense because the problem states there are 16 different hourglass shapes. With this information we confirm the right starting points. This also points out where I did I go wrong with my previous drawing. I tried starting at `0,0`

while I should have start at `0,1`

. Now that we know our starting index is, we just need to get information to complete the hourglass.

**Step 3 : Understanding the Hourglass**

So I drew another sketch that represents a single hourglass, this time, without the numbered index, i subtitued them into variables `r`

for rows and `c`

for columns. The blue circle is the index we are at. And after that it was clear what we need, and this is the code solution that I came up with.

```
var arr = [[1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0], [0, 0, 2, 4, 4, 0], [0, 0, 0, 2, 0, 0], [0, 0, 1, 2, 4, 0]]
var arrayOfSum : [Int] = []
for row in 0..<arr.count {
if (row >= 0 && row <= 3) {
for col in 0..<arr.count {
if (col > 0 && col < 5) {
print ("row \(row) column \(col)")
let a = arr[row][col-1]
let b = arr[row][col]
let c = arr[row][col+1]
let d = arr[row+1][col]
let e = arr[row+2][col-1]
let f = arr[row+2][col]
let g = arr[row+2][col+1]
let sum = a + b + c + d + e + f + g
arrayOfSum.append(sum)
}
}
}
}
print("array of sum \(arrayOfSum)")
print("max = \(String(describing: arrayOfSum.max()))")
```

### Lesson Learned

- Boy I think this took me a good 1 and a half hour, but it's okay. It's a start.
- When I submitted my code, I passed all the test cases, which was a first ðŸ¥º , I got an euphoria I never really felt before, this was the first time I truly solved a Hackerrank problem without asking Google for hints!
- Don't even try coding without truly understanding what the problem first. You'll end up wasting time going back and forth. It's okay to set out a few minutes to truly grasp what is asked until you truly understand it.
- I learned a bit more about the for loop and feel more confident in using it now.
- I refreshed my understanding of when to use let & var and their differences.

See you on Day 2!