Notebooks 2.0 is here.
Read the preview announcement
Platform
Resources
Pricing
Sign in
Get started
Visnu Pitiyanuvath
Workspace
Fork
Published
Advent of Code 2021
By
Visnu Pitiyanuvath
Edited
Dec 6, 2021
2 stars
Advent of Code 2021
Advent of Code Leaderboard 2021
Day 1: Sonar Sweep
Day 2: Dive!
Day 3: Binary Diagnostic
Day 4: Giant Squid
Day 5: Hydrothermal Venture
Day 6: Lanternfish
Day 7: The Treachery of Whales
Day 8: Seven Segment Search
Day 9: Smoke Basin
Day 10: Syntax Scoring
Day 11: Dumbo Octopus
Day 12: Passage Pathing
Day 13: Transparent Origami
Day 14: Extended Polymerization
Day 15: Chiton
Day 16: Packet Decoder
Day 17: Trick Shot
Day 18: Snailfish
Insert cell
Insert cell
Insert cell
tidy
=
data
.
flatMap
(
d
=>
d
.
split
(
""
)
.
map
(
(
digit
,
position
)
=>
(
{
digit
,
position
}
)
)
)
Insert cell
part1
=
{
let
gamma
=
""
,
epsilon
=
""
;
for
(
let
i
=
0
;
i
<
data
[
0
]
.
length
;
i
++
)
{
const
c
=
mostCommon
(
data
,
i
)
;
gamma
+=
c
;
epsilon
+=
1
-
c
;
}
return
parseInt
(
gamma
,
2
)
*
parseInt
(
epsilon
,
2
)
;
}
Insert cell
mostCommon
=
(
numbers
,
i
)
=>
{
const
counts
=
{
1
:
0
,
0
:
0
}
;
for
(
const
n
of
numbers
)
counts
[
n
[
i
]
]
++
;
return
counts
[
0
]
>
counts
[
1
]
?
0
:
1
;
}
Insert cell
function
filter
(
input
,
criteria
,
i
=
0
)
{
const
match
=
criteria
(
input
,
i
)
;
const
remaining
=
input
.
filter
(
(
n
)
=>
n
[
i
]
==
match
)
;
return
remaining
.
length
===
1
?
remaining
[
0
]
:
filter
(
remaining
,
criteria
,
i
+
1
)
;
}
Insert cell
o2
=
parseInt
(
filter
(
data
,
mostCommon
)
,
2
)
Insert cell
co2
=
parseInt
(
filter
(
data
,
(
input
,
i
)
=>
1
-
mostCommon
(
input
,
i
)
)
,
2
)
Insert cell
part2
=
o2
*
co2
Insert cell
Insert cell
data
=
(
which
===
"test"
?
test
:
input
)
.
trim
(
)
.
split
(
"\n"
)
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
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
tidy
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
part1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mostCommon
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
filter
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
o2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
co2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
part2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
which
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
data
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test
Edit
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