From ae592b71af670e3596b3d80a866ca49a1cee4c25 Mon Sep 17 00:00:00 2001 From: Logan G Date: Sat, 3 Dec 2022 15:39:19 -0700 Subject: [PATCH] Since everyone wanted me to use lists for D1, here you go Also, zombie, your PR kinda sucked so I deleted it and made my own --- day1/day1-alt.py | 22 +++++++++++++++++++ day1/day1.py | 23 ++++++++++---------- day1/input2 | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 day1/day1-alt.py create mode 100644 day1/input2 diff --git a/day1/day1-alt.py b/day1/day1-alt.py new file mode 100644 index 0000000..774cdde --- /dev/null +++ b/day1/day1-alt.py @@ -0,0 +1,22 @@ +if __name__ == "__main__": + elves = [] + + with open('input', 'r') as f: + sum_ = 0 + for line in f: + if line.strip() == '': + elves.append(sum_) + sum_ = 0 + else: + sum_ += int(line.strip()) + + elves.append(sum_) + + elves.sort(reverse=True) + + largest = elves[0] + print(f'Max Value: {largest}') + + top3 = elves[0] + elves[1] + elves[2] + + print(f'Top 3: {top3}') diff --git a/day1/day1.py b/day1/day1.py index 4d36409..f62de35 100644 --- a/day1/day1.py +++ b/day1/day1.py @@ -1,23 +1,24 @@ if __name__ == "__main__": - elves = {} + elves = [] with open('input', 'r') as f: - elf = 0 - elves[0] = 0 + sum_ = 0 for line in f: if line.strip() == '': - elf += 1 - elves[elf] = 0 + elves.append(sum_) + sum_ = 0 else: - elves[elf] += int(line.strip()) + sum_ += int(line.strip()) - largest = max(elves, key=elves.get) - print(f'Index: {largest}\nValue: {elves[largest]}') + elves.append(sum_) + + largest = max(elves) + print(f'Max Value: {largest}') top3 = 0 for i in range(3): - largest = max(elves, key=elves.get) - top3 += elves[largest] - del elves[largest] + largest = max(elves) + top3 += largest + elves.remove(largest) print(f'Top 3: {top3}') diff --git a/day1/input2 b/day1/input2 new file mode 100644 index 0000000..7b975a0 --- /dev/null +++ b/day1/input2 @@ -0,0 +1,55 @@ +5324 +5176 +2197 +2701 +6185 +3901 +5392 +2065 +6467 +6085 +5062 +1841 +1197 +1318 + +4901 +3443 +1403 +5570 +4336 +5672 +2532 +5627 +6038 +1099 +4305 +2317 +1382 +3226 +4427 + +2612 +15638 + +4118 +4687 +2243 +3532 +2089 +3937 +1146 +5069 +5728 +2962 +3099 +5882 +5448 +6064 + +2642 +7996 +5334 +10384 +1106 +2742