Browse Source

Day 5 Swift

master
boB Rudis 3 years ago
parent
commit
c0c49089b1
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 486
      R/.Rhistory
  2. 11
      Swift/2020-code-advent.playground/Contents.swift
  3. 2
      Swift/2020-code-advent.playground/Sources/04-01.swift
  4. 77
      Swift/2020-code-advent.playground/Sources/05-01.swift

486
R/.Rhistory

@ -1,246 +1,3 @@
if (part == trigger):
ub -= amt
else:
lb += amt
return(ub if (input[-1] == ret) else lb)
def get_seat(x):
return(
(partition(x[0:6], 0, 127, "F", "F") * 8) +
partition(x[7:9], 0, 7, "L", "R")
)
res = [ get_seat(bpass) for bpass in input ]
# 05-01
res.sort()
max(res)
import math
import operator
# with open("../input/04-01.txt") as f:
with open("/tmp/test.txt") as f:
input = f.read().splitlines()
def partition(input, lb, ub, trigger, ret):
for idx, part in enumerate(input):
amt = math.floor((ub - lb)/2) + 1
if (part == trigger):
ub -= amt
else:
lb += amt
return(ub if (input[-1] == ret) else lb)
def get_seat(x):
return(
(partition(x[0:6], 0, 127, "F", "F") * 8) +
partition(x[7:9], 0, 7, "L", "R")
)
res = [ get_seat(bpass) for bpass in input ]
# 05-01
res.sort()
max(res)
# 05-02
res.insert(0, res[0])
list(map(operator.sub, res[1:], res[:-1]))
with open("../input/05-01.txt") as f:
# with open("/tmp/test.txt") as f:
input = f.read().splitlines()
def partition(input, lb, ub, trigger, ret):
for idx, part in enumerate(input):
amt = math.floor((ub - lb)/2) + 1
if (part == trigger):
ub -= amt
else:
lb += amt
return(ub if (input[-1] == ret) else lb)
def get_seat(x):
return(
(partition(x[0:6], 0, 127, "F", "F") * 8) +
partition(x[7:9], 0, 7, "L", "R")
import math
import operator
with open("../input/05-01.txt") as f:
# with open("/tmp/test.txt") as f:
input = f.read().splitlines()
def partition(input, lb, ub, trigger, ret):
for idx, part in enumerate(input):
amt = math.floor((ub - lb)/2) + 1
if (part == trigger):
ub -= amt
else:
lb += amt
return(ub if (input[-1] == ret) else lb)
def get_seat(x):
return(
(partition(x[0:6], 0, 127, "F", "F") * 8) +
partition(x[7:9], 0, 7, "L", "R")
)
res = [ get_seat(bpass) for bpass in input ]
res.sort()
max(res)
res.insert(0, res[0])
list(map(operator.sub, res[1:], res[:-1]))
res.pop(0)
import math
import operator
with open("../input/05-01.txt") as f:
# with open("/tmp/test.txt") as f:
input = f.read().splitlines()
def partition(input, lb, ub, trigger, ret):
for idx, part in enumerate(input):
amt = math.floor((ub - lb)/2) + 1
if (part == trigger):
ub -= amt
else:
lb += amt
return(ub if (input[-1] == ret) else lb)
def get_seat(x):
return(
(partition(x[0:6], 0, 127, "F", "F") * 8) +
partition(x[7:9], 0, 7, "L", "R")
)
res = [ get_seat(bpass) for bpass in input ]
# 05-01
res.sort()
max(res)
# 05-02
res.insert(0, res[0])
res
import numpy as geek
pyconfig()
import math
import numpy as geek
with open("../input/05-01.txt") as f:
# with open("/tmp/test.txt") as f:
input = f.read().splitlines()
import math
import numpy as geek
with open("../input/05-01.txt") as f:
# with open("/tmp/test.txt") as f:
input = f.read().splitlines()
def partition(input, lb, ub, trigger, ret):
for idx, part in enumerate(input):
amt = math.floor((ub - lb)/2) + 1
if (part == trigger):
ub -= amt
else:
lb += amt
return(ub if (input[-1] == ret) else lb)
def get_seat(x):
return(
(partition(x[0:6], 0, 127, "F", "F") * 8) +
partition(x[7:9], 0, 7, "L", "R")
)
res = [ get_seat(bpass) for bpass in input ]
# 05-01
res.sort()
max(res)
res.insert(0, res[0])
diff(res)
import numpy as np
np.diff(res)
import math
import numpy as np
import math
import numpy as np
with open("../input/05-01.txt") as f:
# with open("/tmp/test.txt") as f:
input = f.read().splitlines()
def partition(input, lb, ub, trigger, ret):
for idx, part in enumerate(input):
amt = math.floor((ub - lb)/2) + 1
if (part == trigger):
ub -= amt
else:
lb += amt
return(ub if (input[-1] == ret) else lb)
def get_seat(x):
return(
(partition(x[0:6], 0, 127, "F", "F") * 8) +
partition(x[7:9], 0, 7, "L", "R")
)
res = [ get_seat(bpass) for bpass in input ]
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)
reticulate::repl_python()
# --- 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")
@ -510,3 +267,246 @@ max(seats)
# 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

11
Swift/2020-code-advent.playground/Contents.swift

@ -9,6 +9,13 @@
//print(advent03.day_03_01())
//
//print(advent03.day_03_02())
//
//advent04.day_04_01()
//
//advent04.day_04_02()
//
//advent05.day_05_01()
advent05.day_05_02()
advent04.day_04_01()
advent04.day_04_02()

2
Swift/2020-code-advent.playground/Sources/04-01.swift

@ -6,7 +6,7 @@ public class advent04 {
let reqKeys: Set = [ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ]
guard var input = try? String(contentsOfFile: "/tmp/test.txt", encoding: .utf8)
guard var input = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/04-01.txt", encoding: .utf8)
.components(separatedBy: "\n") else {
return(-1)
}

77
Swift/2020-code-advent.playground/Sources/05-01.swift

@ -0,0 +1,77 @@
import Foundation
public class advent05 {
static func partition(seq: String, lb: Double, ub: Double, trigger: String) -> Double {
var lb = lb
var ub = ub
for part in seq {
let amt = floor(Double((ub - lb)/2)) + 1
if (String(part) == trigger) {
ub -= amt
} else {
lb += amt
}
}
return((String(seq.last!) == trigger) ? ub : lb)
}
public static func day_05_01() -> Double {
guard let input = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/05-01.txt", encoding: .utf8)
.components(separatedBy: "\n") else {
return(-1)
}
let res = input.filter { line in line != "" }.map { (line) -> Double in
let endIndex = line.index(line.startIndex, offsetBy: 7)
let rowPart = line[line.startIndex..<endIndex]
let seatPart = line[endIndex..<line.endIndex]
return((partition(seq: String(rowPart), lb: 0, ub: 127, trigger: "F") * 8) + partition(seq: String(seatPart), lb: 0, ub: 7, trigger: "L"))
}
return(res.max()!)
}
public static func day_05_02() -> Int {
guard let input = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/05-01.txt", encoding: .utf8)
.components(separatedBy: "\n") else {
return(-1)
}
var res = input.filter { line in line != "" }.map { (line) -> Int in
let endIndex = line.index(line.startIndex, offsetBy: 7)
let rowPart = line[line.startIndex..<endIndex]
let seatPart = line[endIndex..<line.endIndex]
return(Int((partition(seq: String(rowPart), lb: 0, ub: 127, trigger: "F") * 8)) + Int(partition(seq: String(seatPart), lb: 0, ub: 7, trigger: "L")))
}
res.sort()
var prev = res[0]
let diff : [Int] = res.map { val in
defer { prev = val }
return( val - prev )
}
return(res[diff.firstIndex { (val) -> Bool in val > 1 }!] - 1)
}
}
Loading…
Cancel
Save