Platform
Resources
Pricing
Sign in
Get started
Maya Gans
Workspace
Fork
Published
Advent of Code 2021
By
Maya Gans
Edited
Dec 8, 2021
Advent of Code 2021
1: Slider
2: Coordinates
3: Binaries
7: Triangular Numbers
8: Numbers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
binary
.
map
(
input
=>
{
return
input
.
split
(
''
)
.
map
(
l
=>
l
.
split
(
' '
)
)
}
)
Insert cell
Insert cell
totals
=
{
let
totals
=
[
]
for
(
let
i
=
0
;
i
<
binary
[
0
]
.
length
;
i
++
)
{
let
temp
=
[
]
for
(
let
j
=
0
;
j
<
binary
.
length
;
j
++
)
{
temp
.
push
(
binary
[
j
]
[
i
]
)
}
let
counts
=
{
}
for
(
var
k
=
0
;
k
<
binary
.
length
;
k
++
)
{
counts
[
temp
[
k
]
]
=
1
+
(
counts
[
temp
[
k
]
]
||
0
)
;
}
totals
.
push
(
counts
)
}
return
totals
}
Insert cell
gamma
=
totals
.
map
(
(
x
)
=>
{
return
x
[
"0"
]
>
x
[
"1"
]
?
"0"
:
"1"
}
)
.
join
(
""
)
Insert cell
epsilon
=
totals
.
map
(
(
x
)
=>
{
return
x
[
"0"
]
>
x
[
"1"
]
?
"1"
:
"0"
}
)
.
join
(
""
)
// epsilon = (2 ** data[0].length) - gamma - 1
// epsilon = 2^12-1-gamma
Insert cell
digit
=
(
binary
)
=>
{
return
parseInt
(
binary
,
2
)
}
Insert cell
digit
(
epsilon
)
*
digit
(
gamma
)
Insert cell
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
=
digit
(
filter
(
binary
,
mostCommon
)
)
Insert cell
co2
=
digit
(
filter
(
binary
,
(
input
,
i
)
=>
1
-
mostCommon
(
input
,
i
)
)
)
Insert cell
o2
*
co2
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
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
binary
Edit
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
totals
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gamma
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
epsilon
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
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
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
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML