Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kkulebaev committed Dec 11, 2022
1 parent afa3aa8 commit 61ec090
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/day8/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
type TreeHeight = number
type TreeRow = TreeHeight[]
export type TreePlace = TreeRow[]

export interface VisibleScore {
isVisible: boolean
score: number
}
37 changes: 20 additions & 17 deletions src/day8/day8.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TreePlace } from './constants'
import { TreePlace, VisibleScore } from './constants'

export function getCountVisibleTrees(data: TreePlace) {
let counter = 0
Expand All @@ -9,12 +9,10 @@ export function getCountVisibleTrees(data: TreePlace) {
for (let j = 0; j < row.length; j++) {
const height = row[j]

if (i === 0 || i === data.length - 1) {
counter++
continue
}
const outsideEl =
i === 0 || i === data.length - 1 || j === 0 || j === row.length - 1

if (j === 0 || j === row.length - 1) {
if (outsideEl) {
counter++
continue
}
Expand Down Expand Up @@ -57,11 +55,6 @@ export function getCountVisibleTrees(data: TreePlace) {
return counter
}

interface VisibleScore {
isVisible: boolean
score: number
}

function checkTopVisible(
height: number,
rowPos: number,
Expand All @@ -70,13 +63,16 @@ function checkTopVisible(
): VisibleScore {
let score = 0
let isVisible = true
let i = rowPos - 1

for (let i = rowPos - 1; i >= 0; i--) {
while (i >= 0) {
score++

if (height <= treePlace[i][colPos]) {
return { isVisible: false, score }
}

i--
}

return { isVisible, score }
Expand All @@ -90,13 +86,16 @@ function checkLeftVisible(
): VisibleScore {
let score = 0
let isVisible = true
let i = colPos - 1

for (let i = colPos - 1; i >= 0; i--) {
while (i >= 0) {
score++

if (height <= treePlace[rowPos][i]) {
return { isVisible: false, score }
}

i--
}

return { isVisible, score }
Expand All @@ -110,15 +109,17 @@ function checkDownVisible(
): VisibleScore {
let score = 0
let isVisible = true

let i = rowPos + 1
const colCount = treePlace.length

for (let i = rowPos + 1; i < colCount; i++) {
while (i < colCount) {
score++

if (height <= treePlace[i][colPos]) {
return { isVisible: false, score }
}

i++
}

return { isVisible, score }
Expand All @@ -132,15 +133,17 @@ function checkRightVisible(
): VisibleScore {
let score = 0
let isVisible = true

let i = colPos + 1
const rowCount = treePlace[0].length

for (let i = colPos + 1; i < rowCount; i++) {
while (i < rowCount) {
score++

if (height <= treePlace[rowPos][i]) {
return { isVisible: false, score }
}

i++
}

return { isVisible, score }
Expand Down

0 comments on commit 61ec090

Please sign in to comment.