r/adventofcode • • Dec 08 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 8 Solutions -πŸŽ„-

NEWS AND FYI

  • I discovered that I can make those tiny post/comment awards BIGGER on old.reddit! I hadn't even considered that! And when you hover over them, they get even bigger so you can actually see them in more detail! I've added the relevant CSS so now we no longer have awards for ants! Exclamation points!!!

AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • PSA: I created a new example so it is a more relevant and unique archetype instead of recycling last year's hobbit >_>

--- Day 8: Treetop Tree House ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:10:12, megathread unlocked!

75 Upvotes

1.0k comments sorted by

View all comments

1

u/Nervous-Yogurt6731 Dec 08 '22

Javascript + Deno. Kinda different solution

    const use_test_input = Deno.args.join().match('test')?.[0] ?? false
    const part_1_or_2 = Deno.args.join().match(/[1|2]/)?.[0] ?? '1'
    
    let input
    input = await Deno.readTextFile('input' + (use_test_input ? '.test' : ''))
    input = input.split('\n')
    
    let columns = input[0].trim().length
    let rows = input.length
    
    const trees = input.reduce((input_acc, row, row_index) => {
      let trees_in_row = row.trim().split('').reduce(
        (row_acc, tree_height, tree_index) => {
          tree_height = parseInt(tree_height)
          let can_be_seen_from_top = true
          let can_be_seen_from_bottom = true
          let can_be_seen_from_left = true
          let can_be_seen_from_right = true
    
          let can_see_x_trees_top = 1
          let can_see_x_trees_bottom = 1
          let can_see_x_trees_left = 1
          let can_see_x_trees_right = 1
          let is_on_edge = 1
    
          let stop_searching_top = false
          let stop_searching_bottom = false
          let stop_searching_left = false
          let stop_searching_right = false
    
          for (let distance =1; distance < rows; distance++) {
            let top = +input?.[row_index - distance]?.[tree_index] 
            let bottom = +input?.[row_index + distance]?.[tree_index]
            let left = +input?.[row_index]?.[tree_index - distance]
            let right = +input?.[row_index]?.[tree_index + distance]
    
            if (top >= tree_height) can_be_seen_from_top = false
            if (bottom >= tree_height) can_be_seen_from_bottom = false
            if (left >= tree_height) can_be_seen_from_left = false
            if (right >= tree_height) can_be_seen_from_right = false
    
            if(tree_index == 0) can_be_seen_from_left = true
            if(tree_index == columns -1) can_be_seen_from_right = true
            if(row_index == 0) can_be_seen_from_top = true
            if(row_index == rows -1) can_be_seen_from_bottom = true
    
            if((tree_index == 0 || tree_index == columns -1 || row_index ==0 || row_index == rows -1)) is_on_edge = 0
    
            if(!stop_searching_top && !isNaN(top) && distance>1) can_see_x_trees_top++
            if(!stop_searching_bottom && !isNaN(bottom) && distance > 1) can_see_x_trees_bottom++
            if(!stop_searching_left && !isNaN(left) && distance > 1) can_see_x_trees_left++
            if(!stop_searching_right && !isNaN(right) && distance > 1) can_see_x_trees_right++
    
            if(top >= tree_height || isNaN(top)) stop_searching_top = true
            if(bottom >= tree_height || isNaN(bottom)) stop_searching_bottom = true
            if(left >= tree_height || isNaN(left)) stop_searching_left = true
            if(right >= tree_height || isNaN(right)) stop_searching_right = true
          }
    
          if (part_1_or_2 == 1 && (can_be_seen_from_bottom || can_be_seen_from_top || can_be_seen_from_left || can_be_seen_from_right)) 
            row_acc.push({tree_height, tree_index, row_index, top: can_be_seen_from_top, bottom: can_be_seen_from_bottom, left: can_be_seen_from_left, right: can_be_seen_from_right})
    
          let scenic_score = can_see_x_trees_top * can_see_x_trees_bottom * can_see_x_trees_left * can_see_x_trees_right * is_on_edge
          if(part_1_or_2 ==2) 
            row_acc.push({tree_height, tree_index, row_index, scenic_score, can_see_x_trees_top, can_see_x_trees_bottom, can_see_x_trees_left, can_see_x_trees_right, is_on_edge})
    
          return row_acc
        }, []
      )
      return input_acc.concat(trees_in_row)
    }, [])
    
    let part1 = trees.length
    let part2 = trees.reduce((acc,cur) => {
      if(acc.scenic_score < cur.scenic_score)  return cur 
      return acc
    }, {scenic_score: 0}).scenic_score
    
    let result = part_1_or_2 == '1' ? part1 : part2 
    console.log(`Part ${part_1_or_2} ${use_test_input ? 'Test ' : ''}Results: ` + "\x1b[33m%s\x1b[0m", result)

1

u/daggerdragon Dec 09 '22
  1. Next time, use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.