Last active
November 15, 2024 17:18
-
-
Save saegeullee/1d9db8268f987858c595921d377203dd to your computer and use it in GitHub Desktop.
Advent of Code 2번 문제
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function part1(input: string):number { | |
| const lines:string[] = input.split('\n'); | |
| let result:number = 0; | |
| for(const line of lines) { | |
| if(line === "") continue; | |
| const infos = line.split(' '); | |
| const min:number = parseInt(infos[0].split('-')[0]); | |
| const max:number = parseInt(infos[0].split('-')[1]); | |
| const target:string = infos[1][0]; | |
| const pw:string = infos[2]; | |
| let cnt:number = 0; | |
| for(let i = 0; i < pw.length; i++) { | |
| if(pw[i] === target) cnt++; | |
| } | |
| if(cnt >= min && cnt <= max) result++; | |
| } | |
| return result; | |
| } | |
| function part2(input: string):number { | |
| const lines:string[] = input.split('\n'); | |
| let result:number = 0; | |
| for(const line of lines) { | |
| if(line === "") continue; | |
| const infos = line.split(' '); | |
| const firstIndex:number = parseInt(infos[0].split('-')[0]); | |
| const secondIndex:number = parseInt(infos[0].split('-')[1]); | |
| const target:string = infos[1][0]; | |
| const pw:string = infos[2]; | |
| const first:string = pw[firstIndex-1]; | |
| const second:string = pw[secondIndex-1]; | |
| let cnt = 0; | |
| if(first === target) cnt++; | |
| if(second === target) cnt++; | |
| if(cnt === 1) result++; | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment