Skip to content

Instantly share code, notes, and snippets.

@raeq
Created January 18, 2022 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raeq/9d26969aaa8c20964b5aa375f8b5dcaf to your computer and use it in GitHub Desktop.
Save raeq/9d26969aaa8c20964b5aa375f8b5dcaf to your computer and use it in GitHub Desktop.
What is median...
from functools import lru_cache
from math import floor, ceil
from statistics import mean, median
def sum_c(pop: list[int], centroid: int) -> int:
@lru_cache
def gauss_sum(n: int) -> int:
return (n * (n + 1)) // 2
return int(sum([gauss_sum(abs(x - centroid)) for x in pop]))
def sum_d(pop: list[int], centroid: int) -> int:
return int(sum([abs(x - centroid) for x in pop]))
def get_data(fn) -> list:
print(fn)
with open(fn) as f:
file_contents = f.read().rstrip()
return list(map(int, file_contents.split(',')))
if __name__ == '__main__':
file_data = get_data("day07.txt")
print(f"Day 7 Part 1: {sum_d(file_data, (median(file_data)))}")
# floor or ceil of mean?
m = mean(file_data)
fl = sum_c(file_data, floor(m))
cl = sum_c(file_data, ceil(m))
print(f"Day 7 Part 2: {fl if fl < cl else cl}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment