boB Rudis
3 years ago
7 changed files with 208 additions and 513 deletions
@ -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 |
import math |
||||
reticulate::repl_python() |
import numpy as np |
||||
import math |
with open("/tmp/test.txt") as f: |
||||
seats[which(diff(c(seats)) > 1)] + 1 |
# with open("../input/06-01.txt") as f: |
||||
reticulate::repl_python() |
input = f.read().splitlines() |
||||
import math |
inpout |
||||
reticulate::repl_python() |
|
||||
input <- readLines("/tmp/test.txt") |
|
||||
input |
input |
||||
split(input, findInterval(1:length(input), which(input == ""))) |
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
from itertools import groupby |
||||
map(filter, `!=` "") |
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
recs |
||||
map(filter, `!=`, "") |
recs = [ ([ split(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
library(stringi) |
recs = [ [ split(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
library(tidyverse) |
def clean(x): |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
return([chr for chr in x]) |
||||
map(filter, `!=`, "") |
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
library(magrittr) |
recs |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
map(keep, equals, "") |
recs |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
[ clean(g) for g in recs ] |
||||
map(discard, equals, "") |
recs = [ grp for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
[ clean(g) for g in recs ] |
||||
# map(discard, equals, "") %>% |
recs |
||||
map(~{ |
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
unlist(strsplit(.x, "")) |
[ [ g for g in rec] for rec in recs ] |
||||
}) |
[ [ clean(g) for g in rec] for rec in recs ] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
def clean(x): |
||||
# map(discard, equals, "") %>% |
[chr for chr in x] |
||||
map(~{ |
[ [ clean(g) for g in rec] for rec in recs ] |
||||
length(unique(unlist(strsplit(.x, "")))) |
def clean(x): |
||||
}) |
res = [chr for chr in x] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
return(length(res)) |
||||
# map(discard, equals, "") %>% |
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
map_int(~{ |
def clean(x): |
||||
length(unique(unlist(strsplit(.x, "")))) |
res = [chr for chr in x] |
||||
}) %>% |
return(len(res)) |
||||
sum() |
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
recs |
||||
map_int(~length(unique(unlist(strsplit(.x, ""))))) %>% |
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
sum() |
recs |
||||
# input <- readLines("/tmp/test.txt") |
def clean(x): |
||||
input <- readLines("../input/06-01.txt") |
res = [chr for chr in x] |
||||
# input <- readLines("/tmp/test.txt") |
return(res) |
||||
input <- readLines("../input/06-01.txt", warn = FALSE) |
return(len(res)) |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
map_int(~length(unique(unlist(strsplit(.x, ""))))) %>% |
recs |
||||
sum() |
flatten = lambda t: [item for sublist in t for item in sublist] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
map_int(~{ |
recs |
||||
table(unlist(strsplit(.x, ""))) |
def clean(x): |
||||
}) |
res = set([chr for chr in x]) |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
return(len(res)) |
||||
map_(~{ |
recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
table(unlist(strsplit(.x, ""))) |
recs |
||||
}) |
flatten = lambda x: [item for sublist in x for item in sublist] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
clean = lambda x: len(set([chr for chr in x])) |
||||
map(~{ |
recs |
||||
table(unlist(strsplit(.x, ""))) |
sum(recs) |
||||
}) |
with open("../input/06-01.txt") as f: |
||||
input <- readLines("/tmp/test.txt") |
input = f.read().splitlines() |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
flatten = lambda x: [item for sublist in x for item in sublist] |
||||
map(~{ |
clean = lambda x: len(set([chr for chr in x])) |
||||
table(unlist(strsplit(.x, ""))) |
recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
}) |
sum(recs) |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
from collections import Counter |
||||
map(discard, equals, "") %>% |
count02 = lambda x: Counter(x) |
||||
map(~{ |
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
n <- length(.x) |
with open("/tmp/test.txt") as f: |
||||
table(unlist(strsplit(.x, ""))) == n |
# with open("../input/06-01.txt") as f: |
||||
}) |
input = f.read().splitlines() |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
map(discard, equals, "") %>% |
count02 = lambda x: list(Counter(x)) |
||||
map(~{ |
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
n <- length(.x) |
count02 = lambda x: Counter(x) |
||||
sum(table(unlist(strsplit(.x, ""))) == n) |
[ 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})] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
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})] |
||||
map(discard, equals, "") %>% |
a[0].key |
||||
map_int(~{ |
a[0].values() |
||||
sum(table(unlist(strsplit(.x, ""))) == length(.x)) |
[v for v in a[0].values() if v == 1] |
||||
}) %>% |
[ grp for grp in a] |
||||
sum() |
[ [ v for v in grp.values() if v == 1 ] for grp in a ] |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
count02 = lambda x: [ answer for size, answer in (len(x), Counter(x)) ] |
||||
map_int(~length(unique(unlist(strsplit(.x, ""))))) %>% |
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
sum() |
count02 = lambda x: [ answer for size, answer in [ len(x), Counter(x) ] ] |
||||
# input <- readLines("/tmp/test.txt") |
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})] |
||||
input <- readLines("../input/06-01.txt", warn = FALSE) |
a |
||||
split(input, findInterval(1:length(input), which(input == ""))) %>% |
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ] |
||||
map(discard, equals, "") %>% |
def count02(grp): |
||||
map_int(~{ |
grp |
||||
sum(table(unlist(strsplit(.x, ""))) == length(.x)) |
[ 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 ]) |
||||
sum() |
flatten = lambda x: [item for sublist in x for item in sublist] |
||||
reticulate::repl_python() |
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 ]) |
||||
|
@ -1,3 +1,3 @@ |
|||||
{ |
{ |
||||
"activeTab": 1 |
"activeTab": 6 |
||||
} |
} |
@ -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<Iterator.Element> = [] |
||||
|
return filter { seen.insert($0).inserted } |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
extension Sequence where Element: AdditiveArithmetic { |
||||
|
func sum() -> Element { reduce(.zero, +) } |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<Workspace |
||||
|
version = "1.0"> |
||||
|
<FileRef |
||||
|
location = "self:"> |
||||
|
</FileRef> |
||||
|
</Workspace> |
Binary file not shown.
Loading…
Reference in new issue