Noëlle пре 3 година
родитељ
комит
9782541fea
No known key found for this signature in database
2 измењених фајлова са 51 додато и 0 уклоњено
  1. 17
    0
      aoc7-1.py
  2. 34
    0
      aoc7-2.py

+ 17
- 0
aoc7-1.py Прегледај датотеку

@@ -0,0 +1,17 @@
import statistics

def main():
"""
Find the position that requires the fewest total moves for each crab sub to get there.
This yields to an insight: given a list of numbers, the value that requires the
fewest total "moves" to get there is the median.
"""
with open("aoc7-1.txt", "r") as file:
positions = [int(el) for el in file.read().strip().split(",")]
med = statistics.median(positions)
moves = [abs(pos-med) for pos in positions]
print(sum(moves))


if __name__ == "__main__":
main()

+ 34
- 0
aoc7-2.py Прегледај датотеку

@@ -0,0 +1,34 @@
import statistics

def main():
with open("aoc7-1.txt", "r") as file:
positions = [int(el) for el in file.read().strip().split(",")]
# Gonna brute-force this and see what happens.
# I bet I'm right and am just doing math wrong at one point.
# By definition, the position we want will be between the lowest
# and highest values in the list.
# positions = [16,1,2,0,4,2,7,1,2,14]
# mn, mx = min(positions), max(positions) # the outer bounds
# movegroups = []
# for pos in positions:
# movegroup = []
# for i in range(mn, mx+1):
# movegroup.append(sum(range(abs(pos-i)+1)))
# movegroups.append(movegroup)
# movetotals = []
# for i in range((mx-mn)+1):
# mt = []
# for move in movegroups:
# mt.append(move[i])
# movetotals.append(sum(mt))
# print(min(movetotals), mn + movetotals.index(min(movetotals)))
# print(statistics.mean(positions))
# I WAS RIGHT
# Leaving the above in for posterity
med = int(statistics.mean(positions))
moves = [sum(range(abs(pos-med)+1)) for pos in positions]
print(sum(moves))


if __name__ == "__main__":
main()

Loading…
Откажи
Сачувај