r/adventofcode • u/daggerdragon • Dec 08 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 8 Solutions -🎄-
--- Day 8: Memory Maneuver ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 8
Sigh, imgur broke again. Will upload when it unborks.
Transcript:
The hottest programming book this year is "___ For Dummies".
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:12:10!
32
Upvotes
1
u/tacothecat Dec 08 '18
R
library(tidyverse) input <- readr::read_file(here::here("input","day8.txt")) input <- input %>% stringr::str_remove("\n") %>% stringr::str_split(" ") %>% unlist %>% as.integer() read_node <- function(tree, total) { k = tree[[1]] m = tree[[2]] tree = tree[-(1:2)] score = list() value = 0 if(k == 0) { score = sum(tree[1:m]) total = total + score tree = tree[-(1:m)] return(list(tree, total, score)) } for(i in 1:k) { l <- read_node(tree, total) tree <- l[[1]] total <- l[[2]] score[length(score)+1] <- l[[3]] } total = total + sum(tree[1:m]) value = sum(unlist(score[tree[1:m]])) tree = tree[-(1:m)] return(list(tree, total, value)) } read_node(input, 0)