Skip to content

Instantly share code, notes, and snippets.

@brecert
Created December 8, 2022 16:14
Show Gist options
  • Save brecert/fec169c6843a5a7fe5645ec4a939839f to your computer and use it in GitHub Desktop.
Save brecert/fec169c6843a5a7fe5645ec4a939839f to your computer and use it in GitHub Desktop.
day 08
const file = Deno.readTextFileSync('days/06/input.txt')
const map = file.split('\n').map(line => line.split('').map(char => parseInt(char)))
const cols = map[0].length
const rows = map.length
const visible = new Set<string>()
// top to bottom
for (let x = 0; x < cols; x++) {
let prev = -1
for (let y = 0; y < rows; y++) {
let num = map[y][x]
if (num == prev || num <= prev) {
continue
}
visible.add(`${y}x${x}`)
prev = num
}
}
// bottom to top
for (let x = 0; x < cols; x++) {
let prev = -1
for (let y = rows - 1; y >= 0; y--) {
let num = map[y][x]
if (num == prev || num <= prev) {
continue
}
visible.add(`${y}x${x}`)
prev = num
}
}
// left to right
for (let y = 0; y < rows; y++) {
let prev = -1
for (let x = 0; x < cols; x++) {
let num = map[y][x]
if (num == prev || num <= prev) {
continue
}
visible.add(`${y}x${x}`)
prev = num
}
}
// right to left
for (let y = 0; y < rows; y++) {
let prev = -1
for (let x = cols - 1; x >= 0; x--) {
let num = map[y][x]
if (num == prev || num <= prev) {
continue
}
visible.add(`${y}x${x}`)
prev = num
}
}
console.log(visible.size)
const file = Deno.readTextFileSync('days/06/input.txt')
const map = file.split('\n').map(line => line.split('').map(char => parseInt(char)))
const cols = map[0].length
const rows = map.length
function calculateScore(x: number, y: number) {
let totalScore = 1;
let val = map[y][x];
function* slope(mx: number, my: number) {
for (let px = x + mx, py = y + my; px >= 0 && py >= 0 && px < cols && py < rows; px += mx, py += my) {
yield map[py][px]
}
}
function findTrees(mx: number, my: number) {
let score = 0;
for (const num of slope(mx, my)) {
score += 1
if (num >= val) {
break
}
}
totalScore *= score
}
findTrees(1, 0)
findTrees(0, 1)
findTrees(-1, 0)
findTrees(0, -1)
return totalScore
}
let highestScore = 0
for (let x = 1; x < cols - 1; x++) {
for (let y = 1; y < rows - 1; y++) {
let score = calculateScore(x, y)
if (score > highestScore) {
highestScore = score
}
}
}
console.log(highestScore)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment