Skip to content

Instantly share code, notes, and snippets.

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

  • Save saegeullee/203b90bf326dee17152ffbfd485b895f to your computer and use it in GitHub Desktop.

Select an option

Save saegeullee/203b90bf326dee17152ffbfd485b895f to your computer and use it in GitHub Desktop.
Advent of Code 3번 문제
function part1(input:string):number {
const lines:string[] = input.split('\n');
lines.shift();
lines.pop();
const x:number = lines[0].length;
const y:number = lines.length;
const array:string[][] = [];
for(const line of lines) {
const arr = [];
for(let i = 0; i < line.length; i++) {
arr.push(line[i]);
}
array.push(arr);
}
let xmove:number = 0;
let ymove:number = 0;
const tree: string = '#';
let cnt:number = 0;
while (ymove < y - 1) {
xmove += 3;
if(xmove > x - 1) {
xmove = xmove - x;
}
ymove += 1;
if(array[ymove][xmove] === tree) {
cnt += 1;
}
}
return cnt;
}
function part2(input:string):number {
const lines:string[] = input.split('\n');
lines.shift();
lines.pop();
const x:number = lines[0].length;
const y:number = lines.length;
const array:string[][] = [];
for(const line of lines) {
const arr = [];
for(let i = 0; i < line.length; i++) {
arr.push(line[i]);
}
array.push(arr);
}
const tree: string = '#';
type Slope = [number, number];
const slopes:Slope[] = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]];
let result:number = 1;
for(const slope of slopes) {
let xmove:number = 0;
let ymove:number = 0;
let cnt:number = 0;
while (ymove < y - 1) {
xmove += slope[0];
if(xmove > x - 1) {
xmove = xmove - x;
}
ymove += slope[1];
if(array[ymove][xmove] === tree) {
cnt += 1;
}
}
result = result * cnt;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment