본문으로 바로가기

팀 내에서 Orval로 생성한 Mock을 import할떄의 Type Definition 떄문에 tsc를 도입했는데, 문제는 자꾸 MSW랑 Faker를 import하기 못하는 것.

 

Cannot find module......

 

이 떴음.

 

Typescript 4.9.3까지는 정상적으로 되는데, 새로 나온 Typescript 4.9.4만 가면 import를 못한다고 하길래 띠용했음.

 

이를 minimum playground로 yarn + msw + tsc로 해보았는데, 거기서는 또 정상적으로 되더라....

 

이걸로 헤맸는데 yarn berry가 Typescript 4.9.4 지원이 제대로 안되어있어서 생기는 문제였음.

 

 

https://github.com/yarnpkg/berry/issues/5125

 

[Bug?]: TS 4.9.4 PnP `Cannot find module` error · Issue #5125 · yarnpkg/berry

Self-service I'd be willing to implement a fix Describe the bug When installing typescript@4.9.4 with yarn@3.3.0 in VSCode, Cannot find module * or its corresponding type declarations. is displ...

github.com

여기에도 동일한 이슈 밟은 사람들이 있었다.

 

아직 patch release가 되어있지 않아 그냥 4.9.3으로 쓰기로 함. 왜 터지는지도 알았으니...



리트코드에서 Palindrome 관련 문제를 푸는데 DP로 푸는게 Best practice인거 같다.

처음에는 시뮬레이션처럼 짝수일떄/홀수일떄를 나눠서 경우를 반복문을 돌려서 풀었다.

리트코드가 시간복잡도를 널널하게 줘서 통과는 되었지만, 다음에는 DP로 풀어야겠다.

 

import sys
input = sys.stdin.readline

N = int(input())
num = list(input().split()) # 수열

dp = [[False] * N for _ in range(N)] # dp[i][j]는 num[i:j + 1]이 팰린드롬인지 여부를 저장

for i in range(N): # 구간이 1이면 무조건 팰린드롬
    for j in range(N):
        if i == j: 
            dp[i][j] = True

for i in range(N): # 구간이 2
    for j in range(N):
        # 두 수가 같으면
        if i + 1 == j and num[i] == num[j]: 
            dp[i][j] = True
            
for k in range(2, N): # 구간이 3 이상
    for i in range(N - k):
        # 처음과 끝 수가 같고 그 중간이 팰린드롬이라면
        if num[i] == num[i + k] and dp[i + 1][i + k - 1] == True: 
            dp[i][i + k] = True

M = int(input())
for _ in range(M):
    S, E = map(int, input().split())
    if dp[S - 1][E - 1]: print(1)
    else: print(0)

 

해당 로직에 대한 백준 풀이가 있어서 참고용으로 복붙...

https://velog.io/@dhelee/%EB%B0%B1%EC%A4%80-10942%EB%B2%88-%ED%8C%B0%EB%A6%B0%EB%93%9C%EB%A1%AC-Python-%EB%8B%A4%EC%9D%B4%EB%82%98%EB%AF%B9-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8DDP

 

 

Advent of code 2022 Day2

 

Rust로 풀었는데

 

'A', 'B', 'C' 와 'X', 'Y', 'Z'를 각각의 유니코드를 빼서 0,1,2로 만든 다음 규칙을 구해서 풀었다.

 

로직 자체는 어렵지 않았는데 rust에서 &str에서 byte로 깔끔하게 바꾸는 방법을 찾느라 헤멨다..

 

로직 깜빡하고 못올렸는데 내일 올려야지..

 

pub fn part_1() -> String {
    let input = std::fs::read_to_string("src/day02.txt").unwrap();

    input
        .lines()
        // map every line to the score for that round
        .map(|line| {
            // transform A B C and X Y Z to 0 1 2 respectively by using their ASCII order
            let bytes = line.as_bytes();
            let left = (bytes[0] - b'A') as i8;
            let right = (bytes[2] - b'X') as i8;

            // 0 for rock, 1 for paper, 2 for scissors
            // 0 for loss, 1 for draw, 2 for win
            let opponent_shape = left;
            let my_shape = right;
            let outcome = (my_shape - opponent_shape + 1).rem_euclid(3);

            let shape_score = my_shape + 1;
            let outcome_score = 3 * outcome;
            (shape_score + outcome_score) as u32
        })
        .sum::<u32>()
        .to_string()
}

내 풀이랑 비슷한 풀이...
https://dev.to/nickymeuleman/advent-of-code-2022-day-2-3b18

인데 나는 includes_str!로 해서 line을 string으로 받아서 그걸 풀어주는 과정이 있어서 좀 더 복잡했다. 그리고 Rust에서는 char 앞에 b를 붙여서 byte처럼 다룰 수 있다는 것을 몰라서 이리저리 구글링했었음.

 

https://doc.rust-lang.org/book/ch03-02-data-types.html

 

Data Types - The Rust Programming Language

Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data. We’ll look at two data type subsets: scalar and compound. Keep in mind that Rust is a statically typed language,

doc.rust-lang.org

분명 읽었던 내용인데 스윽 지나갔었다 ㅠ

 

 

이런 수학적 풀이 말고, Rust의 match를 통해서 깔끔하게 푼 풀이도 있었는데,

use std::fs;
use std::io;
use std::io::BufRead;
fn main() -> io::Result<()> {
    let file = fs::File::open("input.txt")?;
    let reader = io::BufReader::new(file);

    let mut score = 0;

    for line in reader.lines() {
        let line = line?;

        // Line is of form "opponent me" - split on the space
        let mut parts = line.split(" ");
        let opponent = parts.next().unwrap();
        let me = parts.next().unwrap();
        
        /*
        A/X = rock
        B/Y = paper
        C/Z = scissors
        */

        // Who won? Opponent A beats me Z, B beats me X, C beats me Y
        let win_score = match (opponent, me) {
            ("A", "Z") | ("B", "X") | ("C", "Y") => 0,
            ("A", "Y") | ("B", "Z") | ("C", "X") => 6,
            _ => 3,
        };
        score += win_score;
        // And an extra score based on what I picked
        // X = 1, Y = 2, Z = 3 - use match for that too:
        let extra_score = match me {
            "X" => 1,
            "Y" => 2,
            "Z" => 3,
            _ => 0,
        };
        println!("{} {} {} {}", opponent, me, win_score, extra_score);
        score += extra_score;
    }
    println!("{}", score);
    Ok(())
}

 

match를 이용해 깔끔하게 풀었던데 많은 공부가 된다.