diff --git a/R/.Rhistory b/R/.Rhistory index 0acfa09..d603cdb 100644 --- a/R/.Rhistory +++ b/R/.Rhistory @@ -1,512 +1,125 @@ -map_dbl(~{ -rng <- c(0, 127) -for (part in .x[1:7]) { -cat(rng[1], ", ", rng[2], "\n", sep="") -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "F") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -row <- if (.x[7] == "F") rng[1] else rng[2] -rng <- c(0, 7) -for (part in .x[8:10]) { -cat(rng[1], ", ", rng[2], "\n", sep="") -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "L") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -seat <- if (.x[10] == "R") rng[1] else rng[2] -(row * 8) + seat -}) %>% -sort() -> seats -max(seats) -seats -# input <- readLines("/tmp/test.txt") -input <- readLines("../input/05-01.txt") -strsplit(input, "") %>% -map_dbl(~{ -rng <- c(0, 127) -for (part in .x[1:7]) { -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "F") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -row <- if (.x[7] == "F") rng[1] else rng[2] -rng <- c(0, 7) -for (part in .x[8:10]) { -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "L") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -seat <- if (.x[10] == "R") rng[1] else rng[2] -(row * 8) + seat -}) %>% -sort() -> seats -max(seats) -seats[which(diff(c(seats[1], seats)) > 1)] -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -reticulate::repl_python() -input <- readLines("/tmp/test.txt") -# input <- readLines("../input/05-01.txt") -strsplit(input, "") %>% -map_dbl(~{ -rng <- c(0, 127) -for (part in .x[1:7]) { -message(str(rng)) -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "F") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -row <- if (.x[7] == "F") rng[1] else rng[2] -rng <- c(0, 7) -for (part in .x[8:10]) { -message(str(rng)) -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "L") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -seat <- if (.x[10] == "R") rng[1] else rng[2] -(row * 8) + seat -}) %>% -sort() -> seats -library(tidyverse) -input <- readLines("/tmp/test.txt") -strsplit(input, "") %>% -map_dbl(~{ -rng <- c(0, 127) -for (part in .x[1:7]) { -message(str(rng)) -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "F") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -row <- if (.x[7] == "F") rng[1] else rng[2] -rng <- c(0, 7) -for (part in .x[8:10]) { -message(str(rng)) -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "L") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -seat <- if (.x[10] == "R") rng[1] else rng[2] -(row * 8) + seat -}) %>% -sort() -> seats -reticulate::repl_python() -library(tidyverse) -# input <- readLines("/tmp/test.txt") -input <- readLines("../input/05-01.txt") -strsplit(input, "") %>% -map_dbl(~{ -rng <- c(0, 127) -for (part in .x[1:7]) { -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "F") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -row <- if (.x[7] == "F") rng[1] else rng[2] -rng <- c(0, 7) -for (part in .x[8:10]) { -amt <- floor((rng[2] - rng[1])/2) + 1 -if (part == "L") { -rng[2] <- rng[2] - amt -} else { -rng[1] <- rng[1] + amt -} -seat <- if (.x[10] == "R") rng[1] else rng[2] -(row * 8) + seat -}) %>% -sort() -> seats -max(seats) -# --- Part Two --- -# -# Ding! The "fasten seat belt" signs have turned on. Time to find your seat. -# -# It's a completely full flight, so your seat should be the only missing -# boarding pass in your list. However, there's a catch: some of the seats at -# the very front and back of the plane don't exist on this aircraft, so they'll -# be missing from your list as well. -# -# Your seat wasn't at the very front or back, though; the seats with IDs +1 -# and -1 from yours will be in your list. -# -# What is the ID of your seat? -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -reticulate::repl_python() -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -diff.default -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -letters[-1] -tail(letters, 1) -input <- readLines("../input/05-01.txt") -partition <- function(seq, lb, ub, trigger) { -for (part in seq) { -amt <- floor((ub - lb)/2) + 1 -if (part == "F") { -ub <- ub - amt -} else { -lb <- lb + amt -} -return(if (tail(seq) == trigger) lb else ub) -} -strsplit(input, "") %>% -map_dbl(~{ -return( -(partition(.x[1:7], 0, 127, "F") * 8) + -partition(.x[7:10], 0, 7, "L") -) -# rng <- c(0, 127) -# -# for (part in .x[1:7]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "F") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# row <- if (.x[7] == "F") rng[1] else rng[2] -# -# rng <- c(0, 7) -# -# for (part in .x[7:10]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "L") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# seat <- if (.x[10] == "R") rng[1] else rng[2] -# -# (row * 8) + seat -}) %>% -sort() -> seats -input <- readLines("../input/05-01.txt") -partition <- function(seq, lb, ub, trigger) { -for (part in seq) { -amt <- floor((ub - lb)/2) + 1 -if (part == "F") { -ub <- ub - amt -} else { -lb <- lb + amt -} -return(if (tail(seq, 1) == trigger) lb else ub) -} -strsplit(input, "") %>% -map_dbl(~{ -return( -(partition(.x[1:7], 0, 127, "F") * 8) + -partition(.x[7:10], 0, 7, "L") -) -# rng <- c(0, 127) -# -# for (part in .x[1:7]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "F") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# row <- if (.x[7] == "F") rng[1] else rng[2] -# -# rng <- c(0, 7) -# -# for (part in .x[7:10]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "L") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# seat <- if (.x[10] == "R") rng[1] else rng[2] -# -# (row * 8) + seat -}) %>% -sort() -> seats -max(seats) -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -seats -input <- readLines("../input/05-01.txt") -partition <- function(seq, lb, ub, trigger) { -for (part in seq) { -amt <- floor((ub - lb)/2) + 1 -if (part == "F") { -ub <- ub - amt -} else { -lb <- lb + amt -} -return(if (tail(seq, 1) == trigger) ub else lb) -} -strsplit(input, "") %>% -map_dbl(~{ -return( -(partition(.x[1:7], 0, 127, "F") * 8) + -partition(.x[7:10], 0, 7, "L") -) -# rng <- c(0, 127) -# -# for (part in .x[1:7]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "F") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# row <- if (.x[7] == "F") rng[1] else rng[2] -# -# rng <- c(0, 7) -# -# for (part in .x[7:10]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "L") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# seat <- if (.x[10] == "R") rng[1] else rng[2] -# -# (row * 8) + seat -}) %>% -sort() -> seats -max(seats) -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -input <- readLines("../input/05-01.txt") -partition <- function(seq, lb, ub, trigger) { -for (part in seq) { -amt <- floor((ub - lb)/2) + 1 -if (part == trigger) { -ub <- ub - amt -} else { -lb <- lb + amt -} -return(if (tail(seq, 1) == trigger) ub else lb) -} -strsplit(input, "") %>% -map_dbl(~{ -return( -(partition(.x[1:7], 0, 127, "F") * 8) + -partition(.x[7:10], 0, 7, "L") -) -# rng <- c(0, 127) -# -# for (part in .x[1:7]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "F") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# row <- if (.x[7] == "F") rng[1] else rng[2] -# -# rng <- c(0, 7) -# -# for (part in .x[7:10]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "L") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# seat <- if (.x[10] == "R") rng[1] else rng[2] -# -# (row * 8) + seat -}) %>% -sort() -> seats -max(seats) -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -partition <- function(seq, lb, ub, trigger) { -for (part in seq) { -amt <- floor((ub - lb)/2) + 1 -if (part == trigger) { -ub <- ub - amt -} else { -lb <- lb + amt -} -return(if (tail(seq, 1) == trigger) ub else lb) -} -strsplit(input, "") %>% -map_dbl(~{ -return( -(partition(.x[1:7], 0, 127, "F") * 8) + -partition(.x[8:10], 0, 7, "L") -) -# rng <- c(0, 127) -# -# for (part in .x[1:7]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "F") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# row <- if (.x[7] == "F") rng[1] else rng[2] -# -# rng <- c(0, 7) -# -# for (part in .x[7:10]) { -# amt <- floor((rng[2] - rng[1])/2) + 1 -# if (part == "L") { -# rng[2] <- rng[2] - amt -# } else { -# rng[1] <- rng[1] + amt -# } -# } -# -# seat <- if (.x[10] == "R") rng[1] else rng[2] -# -# (row * 8) + seat -}) %>% -sort() -> seats -max(seats) -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -library(tidyverse) -input <- readLines("../input/05-01.txt") -partition <- function(seq, lb, ub, trigger) { -for (part in seq) { -amt <- floor((ub - lb)/2) + 1 -if (part == trigger) { -ub <- ub - amt -} else { -lb <- lb + amt -} -return(if (tail(seq, 1) == trigger) ub else lb) -} -strsplit(input, "") %>% -map_dbl(~{ -(partition(.x[1:7], 0, 127, "F") * 8) + partition(.x[8:10], 0, 7, "L") -}) %>% -sort() -> seats -max(seats) -seats[which(diff(c(seats)) > 1)] + 1 -seats[which(diff(c(seats[1], seats)) > 1)] - 1 -reticulate::repl_python() import math -reticulate::repl_python() -import math -seats[which(diff(c(seats)) > 1)] + 1 -reticulate::repl_python() -import math -reticulate::repl_python() -input <- readLines("/tmp/test.txt") +import numpy as np +with open("/tmp/test.txt") as f: +# with open("../input/06-01.txt") as f: +input = f.read().splitlines() +inpout input -split(input, findInterval(1:length(input), which(input == ""))) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(filter, `!=` "") -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(filter, `!=`, "") -library(stringi) -library(tidyverse) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(filter, `!=`, "") -library(magrittr) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(keep, equals, "") -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(discard, equals, "") -split(input, findInterval(1:length(input), which(input == ""))) %>% -# map(discard, equals, "") %>% -map(~{ -unlist(strsplit(.x, "")) -}) -split(input, findInterval(1:length(input), which(input == ""))) %>% -# map(discard, equals, "") %>% -map(~{ -length(unique(unlist(strsplit(.x, "")))) -}) -split(input, findInterval(1:length(input), which(input == ""))) %>% -# map(discard, equals, "") %>% -map_int(~{ -length(unique(unlist(strsplit(.x, "")))) -}) %>% -sum() -split(input, findInterval(1:length(input), which(input == ""))) %>% -map_int(~length(unique(unlist(strsplit(.x, ""))))) %>% -sum() -# input <- readLines("/tmp/test.txt") -input <- readLines("../input/06-01.txt") -# input <- readLines("/tmp/test.txt") -input <- readLines("../input/06-01.txt", warn = FALSE) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map_int(~length(unique(unlist(strsplit(.x, ""))))) %>% -sum() -split(input, findInterval(1:length(input), which(input == ""))) %>% -map_int(~{ -table(unlist(strsplit(.x, ""))) -}) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map_(~{ -table(unlist(strsplit(.x, ""))) -}) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(~{ -table(unlist(strsplit(.x, ""))) -}) -input <- readLines("/tmp/test.txt") -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(~{ -table(unlist(strsplit(.x, ""))) -}) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(discard, equals, "") %>% -map(~{ -n <- length(.x) -table(unlist(strsplit(.x, ""))) == n -}) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(discard, equals, "") %>% -map(~{ -n <- length(.x) -sum(table(unlist(strsplit(.x, ""))) == n) -}) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(discard, equals, "") %>% -map_int(~{ -sum(table(unlist(strsplit(.x, ""))) == length(.x)) -}) %>% -sum() -split(input, findInterval(1:length(input), which(input == ""))) %>% -map_int(~length(unique(unlist(strsplit(.x, ""))))) %>% -sum() -# input <- readLines("/tmp/test.txt") -input <- readLines("../input/06-01.txt", warn = FALSE) -split(input, findInterval(1:length(input), which(input == ""))) %>% -map(discard, equals, "") %>% -map_int(~{ -sum(table(unlist(strsplit(.x, ""))) == length(.x)) -}) %>% -sum() -reticulate::repl_python() +recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] +from itertools import groupby +recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] +recs +recs = [ ([ split(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] +recs = [ [ split(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] +def clean(x): +return([chr for chr in x]) +recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] +recs +recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] +recs +[ clean(g) for g in recs ] +recs = [ grp for key, grp in groupby(input, lambda str: str != "") if key ] +[ clean(g) for g in recs ] +recs +recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] +[ [ g for g in rec] for rec in recs ] +[ [ clean(g) for g in rec] for rec in recs ] +def clean(x): +[chr for chr in x] +[ [ clean(g) for g in rec] for rec in recs ] +def clean(x): +res = [chr for chr in x] +return(length(res)) +recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] +def clean(x): +res = [chr for chr in x] +return(len(res)) +recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] +recs +recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] +recs +def clean(x): +res = [chr for chr in x] +return(res) +return(len(res)) +recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] +recs +flatten = lambda t: [item for sublist in t for item in sublist] +recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +recs +def clean(x): +res = set([chr for chr in x]) +return(len(res)) +recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +recs +flatten = lambda x: [item for sublist in x for item in sublist] +clean = lambda x: len(set([chr for chr in x])) +recs +sum(recs) +with open("../input/06-01.txt") as f: +input = f.read().splitlines() +flatten = lambda x: [item for sublist in x for item in sublist] +clean = lambda x: len(set([chr for chr in x])) +recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +sum(recs) +from collections import Counter +count02 = lambda x: Counter(x) +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +with open("/tmp/test.txt") as f: +# with open("../input/06-01.txt") as f: +input = f.read().splitlines() +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +count02 = lambda x: list(Counter(x)) +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +count02 = lambda x: Counter(x) +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +[Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 2, 'b': 1, 'c': 1}), Counter({'a': 4}), Counter({'b': 1})] +a = [Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 2, 'b': 1, 'c': 1}), Counter({'a': 4}), Counter({'b': 1})] +a[0].key +a[0].values() +[v for v in a[0].values() if v == 1] +[ grp for grp in a] +[ [ v for v in grp.values() if v == 1 ] for grp in a ] +count02 = lambda x: [ answer for size, answer in (len(x), Counter(x)) ] +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +count02 = lambda x: [ answer for size, answer in [ len(x), Counter(x) ] ] +a = [Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 2, 'b': 1, 'c': 1}), Counter({'a': 4}), Counter({'b': 1})] +a +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +def count02(grp): +grp +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +sum([ count01(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]) +flatten = lambda x: [item for sublist in x for item in sublist] +count01 = lambda x: len(set([chr for chr in x])) +# 06-01 +sum([ count01(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]) +def count02(grp): +grp +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +def count02(grp): +return(grp) +[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] +[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ] +def count02(grp): +return(len(grp)) +[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ] +def count02(grp): +grp_sz = len(grp) +return(Counter(flatten(grp))) +[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ] +def count02(grp): +return([ val for val in Counter(flatten(grp)).values() if val == len(grp)]) +[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ] +def count02(grp): +return(len([ val for val in Counter(flatten(grp)).values() if val == len(grp)])) +[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ] +count02 = lambda x: len([ val for val in Counter(flatten(x)).values() if val == len(x)]) +[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ] +# with open("/tmp/test.txt") as f: +with open("../input/06-01.txt") as f: +input = f.read().splitlines() +count01 = lambda x: len(set([chr for chr in x])) +sum([ count01(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]) +count02 = lambda x: len([ val for val in Counter(flatten(x)).values() if val == len(x)]) +sum([ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ]) diff --git a/R/.Rproj.user/CBF2E10B/pcs/files-pane.pper b/R/.Rproj.user/CBF2E10B/pcs/files-pane.pper index 792db3a..717ce4e 100644 --- a/R/.Rproj.user/CBF2E10B/pcs/files-pane.pper +++ b/R/.Rproj.user/CBF2E10B/pcs/files-pane.pper @@ -5,5 +5,5 @@ "ascending": true } ], - "path": "~/Development/2020-code-advent/R" + "path": "~/Development/2020-code-advent/js" } \ No newline at end of file diff --git a/R/.Rproj.user/CBF2E10B/pcs/source-pane.pper b/R/.Rproj.user/CBF2E10B/pcs/source-pane.pper index b074a4f..ea660b4 100644 --- a/R/.Rproj.user/CBF2E10B/pcs/source-pane.pper +++ b/R/.Rproj.user/CBF2E10B/pcs/source-pane.pper @@ -1,3 +1,3 @@ { - "activeTab": 1 + "activeTab": 6 } \ No newline at end of file diff --git a/Swift/2020-code-advent.playground/Contents.swift b/Swift/2020-code-advent.playground/Contents.swift index c2febe9..48f5662 100644 --- a/Swift/2020-code-advent.playground/Contents.swift +++ b/Swift/2020-code-advent.playground/Contents.swift @@ -15,7 +15,11 @@ //advent04.day_04_02() // //advent05.day_05_01() +// +// advent05.day_05_02() +// +advent06.day_06_01() -advent05.day_05_02() +advent06.day_06_02() diff --git a/Swift/2020-code-advent.playground/Sources/06-01.swift b/Swift/2020-code-advent.playground/Sources/06-01.swift new file mode 100644 index 0000000..6c1654f --- /dev/null +++ b/Swift/2020-code-advent.playground/Sources/06-01.swift @@ -0,0 +1,71 @@ +import Foundation + +public class advent06 { + + static func explode(_ arr : [ String ]) -> [ [Character] ] { + return([ Array("hello") ]) + } + + public static func day_06_01() -> Int { + + guard var input = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/06-01.txt", encoding: .utf8) + .components(separatedBy: "\n") else { + return(-1) + } + + input.insert("", at: 0) + + let res: [[String]] = input.reduce(into: []) { + ($1 == "") ? $0.append([$1]) : $0[$0.index(before: $0.endIndex)].append($1) + } + .filter { $0.count > 1 } + .map { $0.filter { !$0.isEmpty } } + + var holding = [[[ Character ]]]() + res.forEach { (entry : [ String ]) in holding.append( entry.compactMap { Array($0) } ) } + + return(holding.map{ $0.flatMap{$0}.unique().count }.sum()) + + } + + public static func day_06_02() -> Int { + + guard var input = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/06-01.txt", encoding: .utf8) + .components(separatedBy: "\n") else { + return(-1) + } + + input.insert("", at: 0) + + let res: [[String]] = input.reduce(into: []) { + ($1 == "") ? $0.append([$1]) : $0[$0.index(before: $0.endIndex)].append($1) + } + .filter { $0.count > 1 } + .map { $0.filter { !$0.isEmpty } } + + var holding = [[[ Character ]]]() + res.forEach { (entry : [ String ]) in holding.append( entry.compactMap { Array($0) } ) } + + return( + holding.map { (a) in + [ a.flatMap{$0}.reduce(into: [:]) { counts, val in counts[val, default: 0] += 1 }.filter{ $0.value == a.count } ] + } + .map { $0.map{ $0.keys.count } } + .reduce([], +) + .sum() + ) + + } + +} + +extension Sequence where Iterator.Element: Hashable { + func unique() -> [Iterator.Element] { + var seen: Set = [] + return filter { seen.insert($0).inserted } + } +} + +extension Sequence where Element: AdditiveArithmetic { + func sum() -> Element { reduce(.zero, +) } +} diff --git a/Swift/2020-code-advent.playground/playground.xcworkspace/contents.xcworkspacedata b/Swift/2020-code-advent.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Swift/2020-code-advent.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Swift/2020-code-advent.playground/playground.xcworkspace/xcuserdata/hrbrmstr.xcuserdatad/UserInterfaceState.xcuserstate b/Swift/2020-code-advent.playground/playground.xcworkspace/xcuserdata/hrbrmstr.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..c45f012 Binary files /dev/null and b/Swift/2020-code-advent.playground/playground.xcworkspace/xcuserdata/hrbrmstr.xcuserdatad/UserInterfaceState.xcuserstate differ