Skip to content

Instantly share code, notes, and snippets.

@bschneidr
Created December 2, 2024 23:45
Show Gist options
  • Save bschneidr/3d6499791c8d46b71f717f158ef5fb52 to your computer and use it in GitHub Desktop.
Save bschneidr/3d6499791c8d46b71f717f158ef5fb52 to your computer and use it in GitHub Desktop.
import polars as pl
import pandas as pd
# Read input data as a data frame with two columns
input = pl.from_pandas(
pd.read_table("day-1/input", sep = " ", header = None,
names = ["list_1", "list_2"])
)
# Sort each column separately,
# then compute difference between each column's respective entries,
# and sum up the differences
input.with_columns(
pl.col(nm).sort() for nm in input.columns
).select(
(pl.col("list_1") - pl.col("list_2")).abs().sum().alias("list_distance")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment