Skip to content

Instantly share code, notes, and snippets.

@saegeullee
Last active November 15, 2024 17:18
Show Gist options
  • Select an option

  • Save saegeullee/1d9db8268f987858c595921d377203dd to your computer and use it in GitHub Desktop.

Select an option

Save saegeullee/1d9db8268f987858c595921d377203dd to your computer and use it in GitHub Desktop.
Advent of Code 2번 문제
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