boB Rudis
3 years ago
commit
290f3b1131
24 changed files with 689 additions and 0 deletions
@ -0,0 +1,45 @@ |
|||
library(rvest) |
|||
library(tidyverse) |
|||
library(httr) |
|||
httr::GET("https://adventofcode.com/2020/day/1") |
|||
httr::GET("https://adventofcode.com/2020/day/1") %>% |
|||
httr::content() |
|||
httr::GET("https://adventofcode.com/2020/day/1") %>% |
|||
httr::content() %>% |
|||
as.character() |
|||
scan(here::here("../input/01-01.txt"), what = "integer") |
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = "integer" |
|||
) |
|||
?scan |
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) |
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) -> input |
|||
combn(input) |
|||
combn(input, 2) |
|||
x <- combn(input, 2) |
|||
str(x, 1) |
|||
?combn |
|||
which(combn(input, 2, sum) == 2020) |
|||
sums <- combn(input, 2, sum) |
|||
sums |
|||
pairs <- combn(input, 2) |
|||
dim(pairs) |
|||
pairs[,which(sums == 2020)] |
|||
`*`(pairs[,which(sums == 2020)]) |
|||
magrittr::multiply_by(pairs[,which(sums == 2020)]) |
|||
reduce(pairs[,which(sums == 2020)], `*`) |
|||
Reduct(pairs[,which(sums == 2020)], `*`) |
|||
Reduce(pairs[,which(sums == 2020)], `*`) |
|||
Reduce(`*`, pairs[,which(sums == 2020)]) |
|||
tri <- combn(input, 3) |
|||
str(tri) |
|||
sums <- combn(input, 3, sum) |
|||
Reduce(`*`, tri[,which(sums == 2020)]) |
|||
combn |
@ -0,0 +1,9 @@ |
|||
{ |
|||
"sortOrder": [ |
|||
{ |
|||
"columnIndex": 2, |
|||
"ascending": true |
|||
} |
|||
], |
|||
"path": "~/Development/2020-code-advent/input" |
|||
} |
@ -0,0 +1,3 @@ |
|||
{ |
|||
"activeTab": 0 |
|||
} |
@ -0,0 +1,14 @@ |
|||
{ |
|||
"left": { |
|||
"splitterpos": 554, |
|||
"topwindowstate": "MAXIMIZE", |
|||
"panelheight": 1349, |
|||
"windowheight": 1387 |
|||
}, |
|||
"right": { |
|||
"splitterpos": 832, |
|||
"topwindowstate": "MINIMIZE", |
|||
"panelheight": 1349, |
|||
"windowheight": 1387 |
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
{ |
|||
"TabSet1": -1, |
|||
"TabSet2": 0, |
|||
"TabZoom": {} |
|||
} |
@ -0,0 +1,5 @@ |
|||
|
|||
|
|||
|
|||
|
|||
|
@ -0,0 +1 @@ |
|||
{"active_set":"","sets":[]} |
@ -0,0 +1,27 @@ |
|||
{ |
|||
"id": "91603C46", |
|||
"path": "~/Development/2020-code-advent/R/01.R", |
|||
"project_path": "01.R", |
|||
"type": "r_source", |
|||
"hash": "565949706", |
|||
"contents": "", |
|||
"dirty": false, |
|||
"created": 1606834493630.0, |
|||
"source_on_save": false, |
|||
"relative_order": 1, |
|||
"properties": { |
|||
"tempName": "Untitled1", |
|||
"source_window_id": "", |
|||
"Source": "Source", |
|||
"cursorPosition": "3,0", |
|||
"scrollLine": "1" |
|||
}, |
|||
"folds": "", |
|||
"lastKnownWriteTime": 1606835285, |
|||
"encoding": "UTF-8", |
|||
"collab_server": "", |
|||
"source_window": "", |
|||
"last_content_update": 1606835285557, |
|||
"read_only": false, |
|||
"read_only_alternatives": [] |
|||
} |
@ -0,0 +1,59 @@ |
|||
# https://adventofcode.com/2020 |
|||
|
|||
# Day 01 - 01 ------------------------------------------------------------- |
|||
|
|||
# Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. |
|||
# |
|||
# Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. |
|||
# |
|||
# For example, suppose your expense report contained the following: |
|||
# |
|||
# 1721 |
|||
# 979 |
|||
# 366 |
|||
# 299 |
|||
# 675 |
|||
# 1456 |
|||
# In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. |
|||
# |
|||
# Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? |
|||
|
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) -> input |
|||
|
|||
pairs <- combn(input, 2) |
|||
|
|||
sums <- combn(input, 2, sum) |
|||
|
|||
Reduce(`*`, pairs[,which(sums == 2020)]) |
|||
|
|||
# Day 01 - 02 ------------------------------------------------------------- |
|||
|
|||
# Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. |
|||
# |
|||
# Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. |
|||
# |
|||
# For example, suppose your expense report contained the following: |
|||
# |
|||
# 1721 |
|||
# 979 |
|||
# 366 |
|||
# 299 |
|||
# 675 |
|||
# 1456 |
|||
# In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. |
|||
# |
|||
# Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? |
|||
|
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) -> input |
|||
|
|||
tri <- combn(input, 3) |
|||
|
|||
sums <- combn(input, 3, sum) |
|||
|
|||
Reduce(`*`, tri[,which(sums == 2020)]) |
@ -0,0 +1,7 @@ |
|||
{ |
|||
"tempName": "Untitled1", |
|||
"source_window_id": "", |
|||
"Source": "Source", |
|||
"cursorPosition": "199,4", |
|||
"scrollLine": "141" |
|||
} |
@ -0,0 +1,7 @@ |
|||
{ |
|||
"tempName": "Untitled1", |
|||
"source_window_id": "", |
|||
"Source": "Source", |
|||
"cursorPosition": "3,0", |
|||
"scrollLine": "1" |
|||
} |
@ -0,0 +1,2 @@ |
|||
~%2FDevelopment%2F2020-code-advent%2FR%2F01.R="6CE8BD46" |
|||
~%2FDevelopment%2F2020-code-advent%2Finput%2F01-01.txt="22EEE1B0" |
@ -0,0 +1,2 @@ |
|||
/Users/hrbrmstr/Development/2020-code-advent/R/01.R="AE02BC3A" |
|||
/Users/hrbrmstr/Development/2020-code-advent/input/01-01.txt="53BE9636" |
@ -0,0 +1,59 @@ |
|||
# https://adventofcode.com/2020 |
|||
|
|||
# Day 01 - 01 ------------------------------------------------------------- |
|||
|
|||
# Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. |
|||
# |
|||
# Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. |
|||
# |
|||
# For example, suppose your expense report contained the following: |
|||
# |
|||
# 1721 |
|||
# 979 |
|||
# 366 |
|||
# 299 |
|||
# 675 |
|||
# 1456 |
|||
# In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. |
|||
# |
|||
# Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? |
|||
|
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) -> input |
|||
|
|||
pairs <- combn(input, 2) |
|||
|
|||
sums <- combn(input, 2, sum) |
|||
|
|||
Reduce(`*`, pairs[,which(sums == 2020)]) |
|||
|
|||
# Day 01 - 02 ------------------------------------------------------------- |
|||
|
|||
# Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. |
|||
# |
|||
# Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. |
|||
# |
|||
# For example, suppose your expense report contained the following: |
|||
# |
|||
# 1721 |
|||
# 979 |
|||
# 366 |
|||
# 299 |
|||
# 675 |
|||
# 1456 |
|||
# In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. |
|||
# |
|||
# Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? |
|||
|
|||
scan( |
|||
file = here::here("../input/01-01.txt"), |
|||
what = integer() |
|||
) -> input |
|||
|
|||
tri <- combn(input, 3) |
|||
|
|||
sums <- combn(input, 3, sum) |
|||
|
|||
Reduce(`*`, tri[,which(sums == 2020)]) |
@ -0,0 +1,15 @@ |
|||
Version: 1.0 |
|||
|
|||
RestoreWorkspace: Default |
|||
SaveWorkspace: Default |
|||
AlwaysSaveHistory: Default |
|||
|
|||
EnableCodeIndexing: Yes |
|||
UseSpacesForTab: Yes |
|||
NumSpacesForTab: 2 |
|||
Encoding: UTF-8 |
|||
|
|||
RnwWeave: Sweave |
|||
LaTeX: pdfLaTeX |
|||
|
|||
StripTrailingWhitespace: Yes |
@ -0,0 +1,3 @@ |
|||
# 2020 Advent of Code |
|||
|
|||
Solutions in R, Swift, and mebbe others |
@ -0,0 +1,3 @@ |
|||
print(advent.day_01_01()) |
|||
|
|||
print(advent.day_01_02()) |
@ -0,0 +1,203 @@ |
|||
import Foundation |
|||
|
|||
public class advent { |
|||
|
|||
public static func day_01_01() -> Int { |
|||
|
|||
guard let lines = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/01-01.txt", encoding: .utf8) else { |
|||
return(-1) |
|||
} |
|||
|
|||
let scanner = Scanner(string: lines) |
|||
var input = [Int]() |
|||
var value: Int = 0 |
|||
while (scanner.scanInt(&value)) { |
|||
input.append(value) |
|||
} |
|||
|
|||
let pairs = input.combinations(taking: 2) |
|||
|
|||
let sums = pairs.map { $0[0] + $0[1] } |
|||
|
|||
let idx = sums.firstIndex(of: 2020)! |
|||
|
|||
return(pairs[idx][0] * pairs[idx][1]) |
|||
|
|||
} |
|||
|
|||
|
|||
public static func day_01_02() -> Int { |
|||
|
|||
guard let lines = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/01-01.txt", encoding: .utf8) else { |
|||
return(-1) |
|||
} |
|||
|
|||
let scanner = Scanner(string: lines) |
|||
var input = [Int]() |
|||
var value: Int = 0 |
|||
while (scanner.scanInt(&value)) { |
|||
input.append(value) |
|||
} |
|||
|
|||
let tri = input.combinations(taking: 3) |
|||
|
|||
let sums = tri.map { $0[0] + $0[1] + $0[2] } |
|||
|
|||
let idx = sums.firstIndex(of: 2020)! |
|||
|
|||
return(tri[idx][0] * tri[idx][1] * tri[idx][2]) |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
// https://github.com/almata/Combinatorics/blob/master/Combinatorics/Combinatorics.swift |
|||
/// Combinatorics contains static functions to generate k-permutations and k-combinations |
|||
/// (in both cases either with or without repetition) of the n elements in an array. |
|||
public class Combinatorics { |
|||
|
|||
// MARK: - Permutations |
|||
|
|||
/// Given an array of elements and how many of them we are taking, returns an array with all possible permutations |
|||
/// without repetition. Please note that as repetition is not allowed, taking must always be less or equal to |
|||
/// `elements.count`. |
|||
/// Almost by convention, if `taking` is 0, the function will return [[]] (an array with only one possible permutation |
|||
/// - a permutation with no elements). In a different scenario, if `taking` is bigger than `elements.count` the function |
|||
/// will return [] (an empty array, so including no permutation at all). |
|||
/// |
|||
/// - Parameters: |
|||
/// - elements: Array to be permutating. |
|||
/// - taking: Picking item count from array. |
|||
/// - Returns: Returns permutations of elements without repetition. |
|||
public static func permutationsWithoutRepetitionFrom<T>(_ elements: [T], taking: Int) -> [[T]] { |
|||
guard elements.count >= taking else { return [] } |
|||
guard elements.count >= taking && taking > 0 else { return [[]] } |
|||
|
|||
if taking == 1 { |
|||
return elements.map {[$0]} |
|||
} |
|||
|
|||
var permutations = [[T]]() |
|||
for (index, element) in elements.enumerated() { |
|||
var reducedElements = elements |
|||
reducedElements.remove(at: index) |
|||
permutations += permutationsWithoutRepetitionFrom(reducedElements, taking: taking - 1).map {[element] + $0} |
|||
} |
|||
|
|||
return permutations |
|||
} |
|||
|
|||
/// Given an array of elements and how many of them we are taking, returns an array with all possible permutations |
|||
/// with repetition. Please note that as repetition is allowed, taking does not need to be less or equal to |
|||
/// `elements.count`. |
|||
/// Almost by convention, if `taking` is 0, the function will return [[]] (an array with only one possible permutation |
|||
/// - a permutation with no elements). In a different scenario, if `taking` is bigger than 0 but elements contains no |
|||
/// elements, the function will return [] (an empty array, so including no permutation at all). |
|||
/// |
|||
/// - Parameters: |
|||
/// - elements: Array to be permutating. |
|||
/// - taking: Picking item count from array. |
|||
/// - Returns: Returns permutations of elements with repetition. |
|||
public static func permutationsWithRepetitionFrom<T>(_ elements: [T], taking: Int) -> [[T]] { |
|||
guard elements.count >= 0 && taking > 0 else { return [[]] } |
|||
|
|||
if taking == 1 { |
|||
return elements.map {[$0]} |
|||
} |
|||
|
|||
var permutations = [[T]]() |
|||
for element in elements { |
|||
permutations += permutationsWithRepetitionFrom(elements, taking: taking - 1).map {[element] + $0} |
|||
} |
|||
|
|||
return permutations |
|||
} |
|||
|
|||
// MARK: - Combinations |
|||
|
|||
/// Given an array of elements and how many of them we are taking, returns an array with all possible combinations |
|||
/// without repetition. Please note that as repetition is not allowed, taking must always be less or equal to |
|||
/// `elements.count`. |
|||
/// Almost by convention, if `taking` is 0, the function will return [[]] (an array with only one possible combination |
|||
/// - a combination with no elements). In a different scenario, if taking is bigger than `elements.count` the function |
|||
/// will return [] (an empty array, so including no combination at all). |
|||
/// |
|||
/// - Parameters: |
|||
/// - elements: Array to be combinating. |
|||
/// - taking: Picking item count from array |
|||
/// - Returns: Returns combinations of elements without repetition. |
|||
public static func combinationsWithoutRepetitionFrom<T>(_ elements: [T], taking: Int) -> [[T]] { |
|||
guard elements.count >= taking else { return [] } |
|||
guard elements.count > 0 && taking > 0 else { return [[]] } |
|||
|
|||
if taking == 1 { |
|||
return elements.map {[$0]} |
|||
} |
|||
|
|||
var combinations = [[T]]() |
|||
for (index, element) in elements.enumerated() { |
|||
var reducedElements = elements |
|||
reducedElements.removeFirst(index + 1) |
|||
combinations += combinationsWithoutRepetitionFrom(reducedElements, taking: taking - 1).map {[element] + $0} |
|||
} |
|||
|
|||
return combinations |
|||
} |
|||
|
|||
/// Given an array of elements and how many of them we are taking, returns an array with all possible combinations |
|||
/// with repetition. Please note that as repetition is allowed, taking does not need to be less or equal to |
|||
/// `elements.count`. |
|||
/// Almost by convention, if `taking` is 0, the function will return [[]] (an array with only one possible |
|||
/// combination - a combination with no elements). In a different scenario, if `taking` is bigger than 0 but |
|||
/// elements contains no elements, the function will return [] (an empty array, so including no combination at all). |
|||
/// |
|||
/// - Parameters: |
|||
/// - elements: Array to be combinating. |
|||
/// - taking: Picking item count from array |
|||
/// - Returns: Returns combinations of elements with repetition. |
|||
public static func combinationsWithRepetitionFrom<T>(_ elements: [T], taking: Int) -> [[T]] { |
|||
guard elements.count >= 0 && taking > 0 else { return [[]] } |
|||
|
|||
if taking == 1 { |
|||
return elements.map {[$0]} |
|||
} |
|||
|
|||
var combinations = [[T]]() |
|||
var reducedElements = elements |
|||
for element in elements { |
|||
combinations += combinationsWithRepetitionFrom(reducedElements, taking: taking - 1).map {[element] + $0} |
|||
reducedElements.removeFirst() |
|||
} |
|||
|
|||
return combinations |
|||
} |
|||
} |
|||
|
|||
extension Array { |
|||
|
|||
/// Permutates array. |
|||
/// |
|||
/// - Parameters: |
|||
/// - taking: Picking item count. |
|||
/// - withRepetition: Could select an item more than one. |
|||
/// - Returns: Returns permutations. |
|||
public func permutations(taking: Int? = nil, withRepetition: Bool = false) -> [Array] { |
|||
if withRepetition { |
|||
return Combinatorics.permutationsWithRepetitionFrom(self, taking: taking ?? self.count) |
|||
} |
|||
return Combinatorics.permutationsWithoutRepetitionFrom(self, taking: taking ?? self.count) |
|||
} |
|||
|
|||
/// Combinates array. |
|||
/// |
|||
/// - Parameters: |
|||
/// - taking: Picking item count. |
|||
/// - withRepetition: Could select an item more than one. |
|||
/// - Returns: Returns combinations. |
|||
public func combinations(taking: Int, withRepetition: Bool = false) -> [Array] { |
|||
if withRepetition { |
|||
return Combinatorics.combinationsWithRepetitionFrom(self, taking: taking) |
|||
} |
|||
return Combinatorics.combinationsWithoutRepetitionFrom(self, taking: taking) |
|||
} |
|||
} |
@ -0,0 +1,4 @@ |
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
|||
<playground version='5.0' target-platform='macos' buildActiveScheme='true'> |
|||
<timeline fileName='timeline.xctimeline'/> |
|||
</playground> |
@ -0,0 +1 @@ |
|||
xcuserdata |
@ -0,0 +1,7 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<Workspace |
|||
version = "1.0"> |
|||
<FileRef |
|||
location = "group:/Users/hrbrmstr/Development/2020-code-advent/Swift/2020-code-advent.playground"> |
|||
</FileRef> |
|||
</Workspace> |
@ -0,0 +1,8 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
|||
<plist version="1.0"> |
|||
<dict> |
|||
<key>IDEDidComputeMac32BitWarning</key> |
|||
<true/> |
|||
</dict> |
|||
</plist> |
@ -0,0 +1,200 @@ |
|||
1440 |
|||
1511 |
|||
1731 |
|||
1400 |
|||
1542 |
|||
1571 |
|||
1768 |
|||
1730 |
|||
1959 |
|||
1342 |
|||
1744 |
|||
872 |
|||
1237 |
|||
1846 |
|||
1597 |
|||
1583 |
|||
1711 |
|||
1499 |
|||
1679 |
|||
1895 |
|||
1875 |
|||
1928 |
|||
1728 |
|||
1673 |
|||
481 |
|||
1934 |
|||
673 |
|||
1704 |
|||
1916 |
|||
1958 |
|||
1821 |
|||
1649 |
|||
1640 |
|||
1802 |
|||
1732 |
|||
121 |
|||
1924 |
|||
1438 |
|||
1748 |
|||
1046 |
|||
1905 |
|||
1566 |
|||
1152 |
|||
1964 |
|||
1518 |
|||
1603 |
|||
1414 |
|||
1785 |
|||
1993 |
|||
1594 |
|||
1761 |
|||
1455 |
|||
1738 |
|||
1699 |
|||
1507 |
|||
1483 |
|||
1450 |
|||
1653 |
|||
1644 |
|||
19 |
|||
1340 |
|||
1227 |
|||
1353 |
|||
2009 |
|||
1188 |
|||
1228 |
|||
1898 |
|||
1941 |
|||
1515 |
|||
1766 |
|||
1351 |
|||
1980 |
|||
1378 |
|||
1702 |
|||
1620 |
|||
1729 |
|||
1279 |
|||
1384 |
|||
1894 |
|||
1770 |
|||
1853 |
|||
1161 |
|||
1970 |
|||
1986 |
|||
1669 |
|||
1938 |
|||
1602 |
|||
1190 |
|||
1822 |
|||
425 |
|||
1750 |
|||
1632 |
|||
1613 |
|||
1805 |
|||
1718 |
|||
1990 |
|||
1762 |
|||
1242 |
|||
1485 |
|||
1598 |
|||
1893 |
|||
1995 |
|||
1823 |
|||
1786 |
|||
1506 |
|||
1464 |
|||
1467 |
|||
1639 |
|||
1674 |
|||
1903 |
|||
1961 |
|||
1478 |
|||
1847 |
|||
1760 |
|||
1997 |
|||
2010 |
|||
899 |
|||
2000 |
|||
1488 |
|||
1243 |
|||
1891 |
|||
1504 |
|||
1693 |
|||
1176 |
|||
1391 |
|||
1563 |
|||
692 |
|||
1497 |
|||
1428 |
|||
1745 |
|||
1368 |
|||
1723 |
|||
1989 |
|||
1930 |
|||
1171 |
|||
1840 |
|||
1372 |
|||
1987 |
|||
1952 |
|||
1842 |
|||
1967 |
|||
1759 |
|||
1929 |
|||
1945 |
|||
1919 |
|||
1333 |
|||
1692 |
|||
1811 |
|||
1221 |
|||
1520 |
|||
1920 |
|||
1093 |
|||
1618 |
|||
1795 |
|||
1686 |
|||
1369 |
|||
1820 |
|||
1857 |
|||
1356 |
|||
1562 |
|||
2004 |
|||
1519 |
|||
1628 |
|||
1831 |
|||
1687 |
|||
1792 |
|||
1948 |
|||
927 |
|||
1789 |
|||
1546 |
|||
1338 |
|||
1614 |
|||
1472 |
|||
1494 |
|||
1979 |
|||
1936 |
|||
1577 |
|||
1147 |
|||
1446 |
|||
1683 |
|||
1375 |
|||
856 |
|||
1787 |
|||
1517 |
|||
1724 |
|||
1334 |
|||
1642 |
|||
1496 |
|||
1668 |
|||
1725 |
|||
1800 |
|||
1708 |
|||
1814 |
|||
1585 |
|||
1827 |
|||
1801 |
|||
1208 |
|||
1839 |
|||
1596 |
|||
1925 |
Loading…
Reference in new issue