diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-06 22:01:50 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-06 22:01:50 -0800 |
commit | 0cd5da6a091bb07434180da50db178e61c911eb4 (patch) | |
tree | 547624249cc615af5927f4be599eb509046db769 | |
parent | 659d106cc6e92fc8ab89044b3ca260a26a9647ca (diff) | |
download | AdventOfCode2021-0cd5da6a091bb07434180da50db178e61c911eb4.tar.gz |
solution for day 7
-rw-r--r-- | day7.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/day7.py b/day7.py new file mode 100644 index 0000000..1a2d206 --- /dev/null +++ b/day7.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +import numpy as np + +def triangle(num): + return (num * (num + 1)) / 2 + +with open("day7.txt") as data: + distances = np.array([num for num in map(int, next(data).strip().split(','))]).astype(int) + # part 1 + median = np.median(distances) + print(np.sum(np.abs(distances - median).astype(int))) + + # part 2 + minim, maxim = np.amin(distances), np.amax(distances) + least_dist = None + for i in range(minim, maxim + 1): + dist = np.sum(triangle(np.abs(distances - i)).astype(int)) + if least_dist is None or least_dist > dist: + least_dist = dist + print(least_dist) |