r/adventofcode • • Dec 13 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 13 Solutions -🎄-

Advent of Code 2021: Adventure Time!


--- Day 13: Transparent Origami ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:09:38, megathread unlocked!

38 Upvotes

798 comments sorted by

View all comments

1

u/GU1T Dec 13 '21

Javascript part 1 and 2

const fs = require('fs')
const day = 13

let [coordinates, folds] = fs.readFileSync(`day${day}/input.txt`, 'utf8').split("\r\n\r\n")
  .map((item, index) => {
    if (index == 0) {
      return item.split("\r\n").map(item => item.split(",").map(Number))
    } else {
      return item.split("\r\n").map(item => item.replace("fold along ", "").split("="))
    }
  })

function fold(coordinates, whatFold, whereFold) {
  [x, y] = whatFold === 'x' ? [1,0] : [0,1]
  return coordinates.map((line) => {
    if (line[y] < whereFold) {
      return line
    } 
    return whatFold === 'y' ? [line[x], Math.abs(line[y] - whereFold * 2)] :
    [Math.abs(line[y] - whereFold * 2), line[x]]
  }) 
}

let unique_records = (input) => {
  let output = new Set
  for (let item of input) {
    output.add(item[0]+"-"+item[1])  
  }
  return [...output]
}

let counter = 1
for (let [whatFold, whereFold] of folds) {
  new_coords = fold(coordinates, whatFold, whereFold)
  coordinates = unique_records(new_coords).map(item => item.split("-").map(Number))
  if (counter == 1) {
    console.log("Amount: " + coordinates.length)
  }
  counter++
} 

function displayGrid(coordinates) {
  let unique = unique_records(coordinates)
  for (i = 0; i <= 55; i++) {
    line = ""
    for (j = 0; j <=  40; j++) {
      if (unique.includes(j+"-"+i)) {
        line = line +"."
      } else {
        line = line + " "
      }
    }
    console.log(line)
  }
}

displayGrid(coordinates)

1

u/daggerdragon Dec 14 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.