solve_day12_part1<-function(input){# Parsing ----pres_start<-which(substr(input, 2L, 2L)==":")+1Lempty<-which(substr(input, 1L, 1L)=="")n_lines<-length(input)n_empty<-length(empty)n_presents<-length(pres_start)presents<-vector("list", n_presents)# present_lookup <- c("#" = TRUE, "." = FALSE)for(iinseq_len(n_presents)){start<-pres_start[[i]]present<-input[start:(start+2L)]present<-t(simplify2array(strsplit(present, "")))present[present=="#"]<-1Lpresent[present=="."]<-0Lstorage.mode(present)<-"integer"presents[[i]]<-present}split<-empty[n_empty]trees<-input[(split+1):n_lines]base_cols<-stringi::stri_extract_first_regex(trees, r"{\d+(?=x)}")base_cols<-as.integer(base_cols)base_rows<-stringi::stri_extract_first_regex(trees, r"{(?<=x)\d+(?=:)}")base_rows<-as.integer(base_rows)pres_counts<-stringi::stri_extract_all_regex(trees, r"{(?<= )\d+}")pres_counts<-lapply(pres_counts, as.integer)# Now what ----# for tree in trees# for pres in pres counts# try placing in every possible position?# can we shortcut with not enough space?base_areas<-base_cols*base_rowspres_areas<-vapply(presents, sum, integer(1))min_areas<-lapply(pres_counts, \(x)x*pres_areas)min_areas<-vapply(min_areas, sum, numeric(1))# uhh, ahahahahasum(min_areas<base_areas)}
Run
aoc_source(day =12, part =1)input=aoc_read(day =12)aoc_run(solve_day12_part1(input))
aoc_source(day =12, part =2)input=aoc_read(day =12)aoc_run(solve_day12_part2(input))
Elapsed: 0.001 seconds
Memory: < 1 KB
— Python Day 12 Part 1 —
import redef solve_day12_part1(text): spaces = [i for i, v inenumerate(text) if v =="\n"] split = spaces[len(spaces)-1] +1 trees = text[split:] widths = [int(line[:2]) for line in trees] heights = [int(line[3:5]) for line in trees] areas = [w * h for w, h inzip(widths, heights)] counts = [re.findall("(?<= )\d+", line) for line in trees] counts = [[int(x) for x in line] for line in counts] counts = [sum(line) for line in counts] valid = [area >= (9* count) for area, count inzip(areas, counts)]sum(valid)
Run
aoc_source(day =12, part =1)input= aoc_read(day =12)result = aoc_run("solve_day12_part1(input)")