r/adventofcode • • Dec 07 '22

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


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

Submissions are OPEN! Teach us, senpai!

-❄️- Submissions Megathread -❄️-


--- Day 7: No Space Left On Device ---


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:14:47, megathread unlocked!

86 Upvotes

1.3k comments sorted by

View all comments

2

u/cetttbycettt Dec 07 '22

R / Rlang / baseR

my solution: just kept track of all parent directories and updated them simultaneously

data07 <- readLines("Input/day07.txt")

size <- 0L
idx <- 1L

for (k in data07[-1]) {
  
  if (grepl("\\d", k)) size[idx] <- size[idx] + as.integer(gsub("\\D", "", k))
  else if (grepl("\\.\\.", k)) idx <- idx[-1]
  else if (grepl("cd", k)) {
    size <- c(size, 0L)
    idx <- c(length(size), idx)
  } 
}

#part1---------
sum(size[size <= 100000])

#part2-----
min(size[size >= -40000000 + size[1]])

1

u/daggerdragon Dec 08 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.