# 알고리즘(23.11.12)

### Task 1

숫자로 구성 된 배열이 주어지고, 해당 배열 숫자들의 합을 리턴한다.

이 때 배열에서 숫자 요소를 변경할 수 있는데, 만약 요소의 값이 11일 때 11의 첫번째 1과 두번째 1을 더해 1 + 1 = 2 로 변경할 수 있다.

이렇게 변경할 수 있는 요소의 수는 2개까지 가능하며 한 요소에 대한 변경 횟수는 무한하다.

```javascript

function replaceSumOfDigits(number = 0) {
  return number.toString().split('').reduce((acc, curr) => {
      const result = Number(acc) + Number(curr);

      if (result >= 10) {
        return replaceSumOfDigits(result);
      }

      return result;
  }, 0)
}

function solution(arr) {
    const reverseArr = arr.sort((a, b) => b - a);

    for (let i = 0; i < 2; i += 1) {
        const item = reverseArr[i]

        if (item >= 10) {
            const replaceDigit = replaceSumOfDigits(item);
            reverseArr[i] = replaceDigit;
        }
    }

    return reverseArr.reduce((acc, curr) => acc + curr, 0);
}
```

### Task 2

4명으로 구성된 그룹이 앉을 좌석을 예약하려 한다.

한 그룹은 최소 2명씩 같이 붙어 있어야 한다.

좌석은 A1, B4와 같은 좌석 번호를 갖고 rows는 좌석의 줄 수, seat는 예약된 좌석 문자열이다.

좌석이 ABC | DEFG | HJK 로 구성되어 있을 때 몇 그룹이 앉을 수 있는지 리턴한다.

```javascript
function solution(rows, seat) {
    const reservedSeats = {};
    const emptySeats = {};
    const seatArr = seat.split(' ');

    for (let i = 1; i <= rows; i += 1) {
        emptySeats[i] = "BCDEFGHJ"
    }

    if (seat !== '') {
        for (let i = 0; i < seatArr.length; i += 1) {
        const seatStr = seatArr[i].split('');
        const col = seatStr.pop();
        const row = seatStr.join('');
        
        if (col === 'A' || col === 'K') {
            continue;
        }

        emptySeats[row] = emptySeats[row].replace(col, '');
        }
    }

    let result = 0;

    for (const row in emptySeats) {
        const hasBCDE = emptySeats[row].includes('BCDE');
        const hasDEFG = emptySeats[row].includes('DEFG');
        const hasFGHJ = emptySeats[row].includes('FGHJ');

        if (hasBCDE) {
            result += 1;
        }

        if (hasFGHJ) {
            result += 1;
        }

        if (!hasBCDE && !hasFGHJ && hasDEFG) {
            result += 1;
        }
    }

    return result;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jihees-organization.gitbook.io/928m.archive/readme/23.11.12.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
