|
|
@ -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 ]) |