Platform
Resources
Pricing
Sign in
Contact us
Ramona
Designer at Observable! ✨
Workspace
Fork
Public
Advent of Code 2022
By
Ramona
Edited
Dec 7, 2022
1
Insert cell
Insert cell
rawData
=
FileAttachment
(
"aocdata-2"
)
.
text
(
)
Insert cell
Insert cell
scoreTable
=
[
// You played...
// R P S
[
1
+
3
,
2
+
6
,
3
+
0
]
,
// They played rock
[
1
+
0
,
2
+
3
,
3
+
6
]
,
// They played paper
[
1
+
6
,
2
+
0
,
3
+
3
]
// They played scissors
]
Insert cell
modifiedData
=
rawData
.
replaceAll
(
/A|B|C|X|Y|Z/g
,
(
d
)
=>
{
if
(
d
==
"A"
||
d
==
"X"
)
{
return
0
;
}
else
if
(
d
==
"B"
||
d
==
"Y"
)
{
return
1
;
}
else
if
(
d
==
"C"
||
d
==
"Z"
)
{
return
2
;
}
}
)
Insert cell
plays
=
modifiedData
.
split
(
"\n"
)
.
map
(
(
d
)
=>
d
.
split
(
" "
)
)
.
slice
(
0
,
-
1
)
Insert cell
scores
=
plays
.
map
(
(
d
,
i
)
=>
scoreTable
[
plays
[
i
]
[
0
]
]
[
plays
[
i
]
[
1
]
]
)
Insert cell
totalScore
=
scores
.
reduce
(
(
accumulator
,
currentValue
)
=>
accumulator
+
currentValue
)
Insert cell
Insert cell
scoreTablePart2
=
[
// Play bonuses: rock = 1, Paper = 2, Scissors = 3
// You should...
// win draw lose
[
6
+
2
,
3
+
1
,
0
+
3
]
,
// They played rock (win: paper, draw: rock, lose: scissors)
[
6
+
3
,
3
+
2
,
0
+
1
]
,
// They played paper (win: scissors, draw: paper, lose: rock)
[
6
+
1
,
3
+
3
,
0
+
2
]
// They played scissors (win: rock, draw: scissors, lose: paper)
]
Insert cell
modifiedDataPart2
=
rawData
.
replaceAll
(
/A|B|C|X|Y|Z/g
,
(
d
)
=>
{
if
(
d
==
"A"
||
d
==
"Z"
)
{
// Z should win, which is the first column above
return
0
;
}
else
if
(
d
==
"B"
||
d
==
"Y"
)
{
return
1
;
}
else
if
(
d
==
"C"
||
d
==
"X"
)
{
// X should lose, which is the third column above
return
2
;
}
}
)
Insert cell
playsPart2
=
modifiedDataPart2
.
split
(
"\n"
)
.
map
(
(
d
)
=>
d
.
split
(
" "
)
)
.
slice
(
0
,
-
1
)
Insert cell
scoresPart2
=
playsPart2
.
map
(
(
d
,
i
)
=>
scoreTablePart2
[
playsPart2
[
i
]
[
0
]
]
[
playsPart2
[
i
]
[
1
]
]
)
Insert cell
totalScorePart2
=
scoresPart2
.
reduce
(
(
accumulator
,
currentValue
)
=>
accumulator
+
currentValue
)
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
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rawData
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
modifiedData
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
plays
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
scores
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
totalScore
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
scoreTablePart2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
modifiedDataPart2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
playsPart2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
scoresPart2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
totalScorePart2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML