Platform
Resources
Pricing
Sign in
Get started
Philippe Rigovanov
math & informatics teacher
Workspace
Fork
Public
By
Philippe Rigovanov
Edited
Dec 3, 2023
Fork of
🕯 Advent of Code – 2023-01 – Trebuchet?!
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
test1
=
`1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet`
Insert cell
test
(
trebuchet1
)
(
[
{
input
:
test1
,
value
:
142
}
,
{
input
,
value
:
55447
}
]
)
Insert cell
trebuchet1
=
trebuchet
(
/^[^\d]*(\d).*(\d)|^[^\d]*(\d).*(\d)?/
)
// see https://regex101.com
Insert cell
trebuchet
=
R
.
curry
(
(
re
)
=>
R
.
pipe
(
R
.
trim
,
// trim any blank lines, esp. at start and end or trip over them
R
.
split
(
"\n"
)
,
// split into separate lines
R
.
map
(
// for each lines
R
.
pipe
(
R
.
match
(
re
)
,
// match the RE
R
.
map
(
(
x
)
=>
word2number
[
x
]
)
,
// map strings to numbers
convert
,
// convert number pairs to calibration value
)
)
,
R
.
sum
,
// calculate total sum of calibration values
)
)
Insert cell
Insert cell
trebuchet2
=
trebuchet
(
re
)
Insert cell
test2
=
`two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
`
Insert cell
test
(
trebuchet2
)
(
[
{
input
:
test2
,
value
:
281
}
,
{
input
:
"2"
,
value
:
22
}
,
{
input
:
"seven"
,
value
:
77
}
,
{
input
,
value
:
54706
}
]
)
Insert cell
// generate object with digit as key as text or number and its corresponding value
// so it can be used as ‘associative array’
word2number
=
R
.
pipe
(
R
.
split
(
"|"
)
,
R
.
addIndex
(
R
.
map
)
(
(
key
,
i
)
=>
(
{
[
key
]
:
i
+
1
,
[
i
+
1
]
:
i
+
1
}
)
)
,
R
.
mergeAll
,
// merge into single object
)
(
one2nine
)
Insert cell
one2nine
=
"one|two|three|four|five|six|seven|eight|nine"
Insert cell
digit
=
`\\d|${
one2nine
}`
// RE for digit as number or text
Insert cell
// either two rigits in a row
// or just a single digit
re
=
new
RegExp
(
`(${
digit
}).*(${
digit
}).*|(${
digit
})`
)
Insert cell
value
=
(
a
,
b
)
=>
10
*
a
+
b
Insert cell
// first value is matching string (so can be skipped), followed by match groups
convert
=
(
[
,
deca
,
uni
,
single
]
)
=>
single
?
value
(
single
,
single
)
:
value
(
deca
,
uni
)
Insert cell
test
=
(
ƒ
)
=>
R
.
map
(
(
{
input
,
value
}
)
=>
ƒ
(
input
)
===
value
)
Insert cell
Insert cell
Insert cell
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
Compare fork
Fork
View
Export
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
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
trebuchet1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
trebuchet
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
trebuchet2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
word2number
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
one2nine
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
digit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
re
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
value
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
convert
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
input
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
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
R
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML