Platform
Resources
Pricing
Sign in
Get started
Martien van Steenbergen
Wise Fool. Visioneer.
Workspace
Fork
Public
2 collections
By
Martien van Steenbergen
Edited
Dec 1, 2023
1 fork
1 star
1
Insert cell
Insert cell
Insert cell
Insert cell
scoreTable
(
RucksackReorganization
(
datasets
,
[
reorganize
(
rearranger
)
,
reorganize
(
authenticator
)
]
)
)
Insert cell
// takes rucksacks and rearrangers and runs each rearranger on each rucksak
// returns table containing results
RucksackReorganization
=
(
rucksacks
,
rearrangers
)
=>
R
.
map
(
(
rearranger
)
=>
R
.
map
(
rearranger
)
(
rucksacks
)
)
(
rearrangers
)
Insert cell
Insert cell
Insert cell
// takes reorganizer function
// returns function that takes an input string (puzzle input) and returns its sum of priorities
reorganize
=
(
reorganizer
)
=>
(
input
)
=>
R
.
pipe
(
// tidy
R
.
split
(
"\n"
)
,
// → ["vJrwpWtwJgWrhcsFMMfFFhFp", "jqHR…" ] // see datasets[0]
R
.
filter
(
hasText
)
,
// like above, omitting blank lines
reorganizer
,
// → ["p"], ["L"], ["P"], ["v"], ["t"], ["s"]
// summarize
R
.
unnest
,
// → ["p", "L", "P", "v", "t", "s"]
R
.
map
(
priority
)
,
// → [16, 38, 42, 22, 20, 19]
R
.
sum
,
// → 157
)
(
input
)
Insert cell
Insert cell
// takes list of strings
// returns list of items that appears in both first and second half of input sting
rearranger
=
R
.
map
(
R
.
pipe
(
halve
,
intersect
,
)
)
Insert cell
R
.
map
(
reorganize
(
rearranger
)
)
(
datasets
)
Insert cell
Insert cell
// takes list of three lists
// returns list of items that appear in all three lists: A ∩ B ∩ C
authenticator
=
R
.
pipe
(
R
.
splitEvery
(
3
)
,
R
.
map
(
(
[
a
,
b
,
c
]
)
=>
R
.
intersection
(
a
,
R
.
intersection
(
b
,
c
)
)
)
// equivalent to: R.map(([a,b,c]) => R.pipe(R.intersection, R.intersection(c))(a, b))
)
Insert cell
R
.
map
(
reorganize
(
authenticator
)
)
(
datasets
)
Insert cell
Insert cell
// Splits a collection into slices or chunks of half the length.
halve
=
(
(
s
)
=>
R
.
splitEvery
(
s
.
length
/
2
)
(
s
)
)
Insert cell
halve
(
"ABCDEF"
)
Insert cell
Insert cell
intersect
=
(
[
l
,
r
]
)
=>
R
.
intersection
(
l
,
r
)
Insert cell
Insert cell
// priority :: String → Number
// takes a string
// returns ‘priority’ of its first character: [a...z] → [1...26], [A...Z] → [27...52]
priority
=
(
c
)
=>
c
.
toLowerCase
(
)
.
charCodeAt
(
0
)
-
"a"
.
charCodeAt
(
0
)
+
[
1
,
27
]
[
+
isUpperCase
(
c
)
]
Insert cell
Insert cell
// takes a String
// returns true if first charactor of String is upper case character, false otherwise, even for non alphas
isUpperCase
=
(
c
)
=>
c
.
toUpperCase
(
)
===
c
Insert cell
Insert cell
// takes a String
// returns true is String contains text (that is, length > 0, non-epmpty string)
hasText
=
x
=>
x
!==
""
Insert cell
Insert cell
scoreTable
=
(
data
)
=>
Inputs
.
table
(
data
.
map
(
(
[
test
,
real
]
,
part
)
=>
(
{
part
:
part
+
1
,
test
,
real
}
)
)
,
{
width
:
36
*
8
}
)
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.
Insert cell
R
=
require
(
"ramda"
)
Insert cell
import
{
AoCBanner
,
AoCStyle
}
from
"@martien/advent-of-code-banner"
Insert cell
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Listed in...
Advent of Code 2022
Martien van Steenbergen
Functional
Martien van Steenbergen
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
RucksackReorganization
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
datasets
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
reorganize
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rearranger
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
authenticator
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
halve
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
intersect
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
priority
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
isUpperCase
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
hasText
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
scoreTable
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
R
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML