Browse Source

Day 6 R and python

master
boB Rudis 3 years ago
parent
commit
2fca75dda2
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 234
      R/.Rhistory
  2. 2
      R/.Rproj.user/CBF2E10B/pcs/source-pane.pper
  3. 9
      R/.Rproj.user/CBF2E10B/sources/prop/INDEX
  4. 4
      R/.Rproj.user/shared/notebooks/paths
  5. 2
      R/05.R
  6. 5
      R/05.py
  7. 116
      R/06.R
  8. 20
      R/06.py
  9. 2179
      input/06-01.txt

234
R/.Rhistory

@ -1,120 +1,3 @@
# highest seat ID on a boarding pass?
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)
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)
# --- Day 5: Binary Boarding ---
#
# You board your plane only to discover a new problem: you dropped your boarding
# pass! You aren't sure which seat is yours, and all of the flight attendants
# are busy with the flood of people that suddenly made it through passport
# control.
#
# You write a quick program to use your phone's camera to scan all of the nearby
# boarding passes (your puzzle input); perhaps you can find your seat through
# process of elimination.
#
# Instead of zones or groups, this airline uses binary space partitioning to
# seat people. A seat might be specified like FBFBBFFRLR, where F means "front",
# B means "back", L means "left", and R means "right".
#
# The first 7 characters will either be F or B; these specify exactly one of
# the 128 rows on the plane (numbered 0 through 127). Each letter tells you
# which half of a region the given seat is in. Start with the whole list of
# rows; the first letter indicates whether the seat is in the front
# (0 through 63) or the back (64 through 127). The next letter indicates which
# half of that region the seat is in, and so on until you're left with
# exactly one row.
#
# For example, consider just the first seven characters of FBFBBFFRLR:
#
# Start by considering the whole range, rows 0 through 127.
# F means to take the lower half, keeping rows 0 through 63.
# B means to take the upper half, keeping rows 32 through 63.
# F means to take the lower half, keeping rows 32 through 47.
# B means to take the upper half, keeping rows 40 through 47.
# B keeps rows 44 through 47.
# F keeps rows 44 through 45.
# The final F keeps the lower of the two, row 44.
#
# The last three characters will be either L or R; these specify exactly one
# of the 8 columns of seats on the plane (numbered 0 through 7). The same
# process as above proceeds again, this time with only three steps. L means
# to keep the lower half, while R means to keep the upper half.
#
# For example, consider just the last 3 characters of FBFBBFFRLR:
#
# Start by considering the whole range, columns 0 through 7.
# R means to take the upper half, keeping columns 4 through 7.
# L means to take the lower half, keeping columns 4 through 5.
# The final R keeps the upper of the two, column 5.
#
# So, decoding FBFBBFFRLR reveals that it is the seat at row 44, column 5.
#
# Every seat also has a unique seat ID: multiply the row by 8, then add the
# column. In this example, the seat has ID 44 * 8 + 5 = 357.
#
# Here are some other boarding passes:
#
# BFFFBBFRRR: row 70, column 7, seat ID 567.
# FFFBBBFRRR: row 14, column 7, seat ID 119.
# BBFFBBFRLL: row 102, column 4, seat ID 820.
# As a sanity check, look through your list of boarding passes. What is the
# highest seat ID on a boarding pass?
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)
seats
reticulate::repl_python()
input <- readLines("/tmp/test.txt")
strsplit(input, "") %>%
map_dbl(~{
rng <- c(0, 127)
for (part in .x[1:7]) {
@ -510,3 +393,120 @@ partition(.x[8:10], 0, 7, "L")
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")
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()

2
R/.Rproj.user/CBF2E10B/pcs/source-pane.pper

@ -1,3 +1,3 @@
{
"activeTab": 3
"activeTab": 1
}

9
R/.Rproj.user/CBF2E10B/sources/prop/INDEX

@ -1,7 +1,16 @@
~%2FDevelopment%2F2020-code-advent%2FR%2F01.R="6CE8BD46"
~%2FDevelopment%2F2020-code-advent%2FR%2F01.py="E3582989"
~%2FDevelopment%2F2020-code-advent%2FR%2F02.R="5E60AF29"
~%2FDevelopment%2F2020-code-advent%2FR%2F04.R="17C8E91A"
~%2FDevelopment%2F2020-code-advent%2FR%2F04.py="374D3B07"
~%2FDevelopment%2F2020-code-advent%2FR%2F05.R="3147CB9C"
~%2FDevelopment%2F2020-code-advent%2FR%2F05.py="C22F3766"
~%2FDevelopment%2F2020-code-advent%2FR%2F06.R="2E0DCDD0"
~%2FDevelopment%2F2020-code-advent%2FR%2F06.py="A53BEE38"
~%2FDevelopment%2F2020-code-advent%2FREADME.md="87542FC2"
~%2FDevelopment%2F2020-code-advent%2FSwift%2F2020-code-advent.playground%2FSources%2F05-01.swift="A5C6F3F2"
~%2FDevelopment%2F2020-code-advent%2Finput%2F01-01.txt="22EEE1B0"
~%2FDevelopment%2F2020-code-advent%2Finput%2F06-01.txt="488B7FA9"
~%2FDevelopment%2F2020-code-advent%2Fjs%2F01.js="BCBA9F87"
~%2FDevelopment%2F2020-code-advent%2Fjs%2F02.js="823FD9BF"
~%2FDevelopment%2F2020-code-advent%2Fjs%2F05.js="28BC66DE"

4
R/.Rproj.user/shared/notebooks/paths

@ -8,11 +8,15 @@
/Users/hrbrmstr/Development/2020-code-advent/R/04.py="8F22DFA5"
/Users/hrbrmstr/Development/2020-code-advent/R/05.R="A949D345"
/Users/hrbrmstr/Development/2020-code-advent/R/05.py="89270E3A"
/Users/hrbrmstr/Development/2020-code-advent/R/06.R="E6CEEABC"
/Users/hrbrmstr/Development/2020-code-advent/R/06.py="6D546EAC"
/Users/hrbrmstr/Development/2020-code-advent/README.md="74DC8DCF"
/Users/hrbrmstr/Development/2020-code-advent/Swift/2020-code-advent.playground/Sources/05-01.swift="8E24EF14"
/Users/hrbrmstr/Development/2020-code-advent/input/01-01.txt="53BE9636"
/Users/hrbrmstr/Development/2020-code-advent/input/02-01.txt="C32036DD"
/Users/hrbrmstr/Development/2020-code-advent/input/03-01.txt="3353A6D2"
/Users/hrbrmstr/Development/2020-code-advent/input/04-01.txt="C5B6BCAC"
/Users/hrbrmstr/Development/2020-code-advent/input/06-01.txt="F27C3C93"
/Users/hrbrmstr/Development/2020-code-advent/js/01.js="77D3BDB6"
/Users/hrbrmstr/Development/2020-code-advent/js/02.js="BC8B36A1"
/Users/hrbrmstr/Development/2020-code-advent/js/03.js="E24752E6"

2
R/05.R

@ -100,4 +100,4 @@ max(seats)
#
# What is the ID of your seat?
seats[which(diff(c(seats[1], seats)) > 1)] - 1
seats[which(diff(c(seats)) > 1)] + 1

5
R/05.py

@ -32,10 +32,7 @@ max(res)
# 05-02
res.insert(0, res[0])
idx = np.where(np.diff(res) > 1)
res.pop(0)
res[idx[0][0]] + 1
res[idx[0][0]] - 1

116
R/06.R

@ -0,0 +1,116 @@
# --- Day 6: Custom Customs ---
#
# As your flight approaches the regional airport where you'll switch to a much
# larger plane, customs declaration forms are distributed to the passengers.
#
# The form asks a series of 26 yes-or-no questions marked a through z. All you
# need to do is identify the questions for which anyone in your group answers
# "yes". Since your group is just you, this doesn't take very long.
#
# However, the person sitting next to you seems to be experiencing a language
# barrier and asks if you can help. For each of the people in their group, you
# write down the questions for which they answer "yes", one per line. For example:
#
# abcx
# abcy
# abcz
#
# In this group, there are 6 questions to which anyone answered
# "yes": a, b, c, x, y, and z. (Duplicate answers to the same question don't
# count extra; each question counts at most once.)
#
# Another group asks for your help, then another, and eventually you've collected
# answers from every group on the plane (your puzzle input). Each group's answers
# are separated by a blank line, and within each group, each person's answers are
# on a single line. For example:
#
# abc
#
# a
# b
# c
#
# ab
# ac
#
# a
# a
# a
# a
#
# b
#
# This list represents answers from five groups:
#
# The first group contains one person who answered "yes" to 3 questions: a, b, and c.
# The second group contains three people; combined, they answered "yes" to 3 questions: a, b, and c.
# The third group contains two people; combined, they answered "yes" to 3 questions: a, b, and c.
# The fourth group contains four people; combined, they answered "yes" to only 1 question, a.
# The last group contains one person who answered "yes" to only 1 question, b.
#
# In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11.
#
# For each group, count the number of questions to which anyone answered "yes".
# What is the sum of those counts?
library(stringi)
library(magrittr)
library(tidyverse)
# input <- readLines("/tmp/test.txt")
input <- readLines("../input/06-01.txt", warn = FALSE)
# 04-01 -------------------------------------------------------------------
split(input, findInterval(1:length(input), which(input == ""))) %>%
map_int(~length(unique(unlist(strsplit(.x, ""))))) %>%
sum()
# --- Part Two ---
#
# As you finish the last group's customs declaration, you notice that you misread
# one word in the instructions:
#
# You don't need to identify the questions to which anyone answered "yes"; you
# need to identify the questions to which everyone answered "yes"!
#
# Using the same example as above:
#
# abc
#
# a
# b
# c
#
# ab
# ac
#
# a
# a
# a
# a
#
# b
# This list represents answers from five groups:
#
# In the first group, everyone (all 1 person) answered "yes" to 3 questions: a, b, and c.
# In the second group, there is no question to which everyone answered "yes".
# In the third group, everyone answered yes to only 1 question, a. Since some
# people did not answer "yes" to b or c, they don't count.
# In the fourth group, everyone answered yes to only 1 question, a.
# In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b.
#
# In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6.
#
# For each group, count the number of questions to which everyone answered "yes".
# What is the sum of those counts?
#
# 04-02
split(input, findInterval(1:length(input), which(input == ""))) %>%
map(discard, equals, "") %>%
map_int(~{
sum(table(unlist(strsplit(.x, ""))) == length(.x))
}) %>%
sum()

20
R/06.py

@ -0,0 +1,20 @@
from itertools import groupby
from collections import Counter
with open("../input/06-01.txt") as f:
input = f.read().splitlines()
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 ])
# 06-02
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 ])

2179
input/06-01.txt

File diff suppressed because it is too large
Loading…
Cancel
Save