Skip to content

Instantly share code, notes, and snippets.

@TimothyLoyer
Created December 1, 2022 15:01
Show Gist options
  • Save TimothyLoyer/4d48ea149fcf01fc058e9e957d2721d1 to your computer and use it in GitHub Desktop.
Save TimothyLoyer/4d48ea149fcf01fc058e9e957d2721d1 to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 1
"""Advent of Code 2022: Day 1"""
def get_input(path):
with open(path) as i:
return i.read()
def get_input_lines(path):
i = get_input(path)
return i.splitlines()
def clean_lines(lines):
def clean(l):
if l == "":
return None
return int(l)
return [clean(l) for l in lines]
def find_highest_total_calories(path):
highest = 0
total = 0
input_calories = clean_lines(get_input_lines(path))
for calories in input_calories:
if calories is None:
highest = max(highest, total)
total = 0
else:
total += calories
return max(highest, total)
def get_totals_from_input(path):
i = get_input(path)
elf_cal_feeds = i.split("\n\n")
return [sum([int(c) for c in f.split()]) for f in elf_cal_feeds]
def find_n_highest_totals(path, n):
cal_totals = get_totals_from_input(path)
cal_totals.sort(reverse=True)
return cal_totals[:n]
def answer_part_1():
"""Find the Elf carrying the most Calories.
How many total Calories is that Elf carrying?
The Elves take turns writing down the number of Calories contained by the
various meals, snacks, rations, etc. that they've brought with them,
one item per line. Each Elf separates their own inventory from
the previous Elf's inventory (if any) by a blank line.
"""
print("Part 1:", find_highest_total_calories("input"))
def answer_part_2():
"""Find the top three Elves carrying the most Calories.
How many Calories are those Elves carrying in total?
"""
top_3_total_cal = sum(find_n_highest_totals("input", 3))
print("Part 2:", top_3_total_cal)
print("Advent of Code 2022 Solutions: Day 1")
answer_part_1()
answer_part_2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment