Upload day01

This commit is contained in:
Felix Albrigtsen 2022-12-01 22:28:07 +01:00
commit e302e0e016
4 changed files with 2322 additions and 0 deletions

3
day01/golf.py Normal file
View File

@ -0,0 +1,3 @@
d=sorted([sum(map(int,x))for x in[z.split()for z in open(0).read().split("\n\n")]]);print(d[-1],sum(d[-3:]))
# 108 bytes, requires piped input. `cat input.txt | python3 golf.py`

2264
day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

14
day01/input_demo.txt Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

41
day01/solution.py Normal file
View File

@ -0,0 +1,41 @@
# The data is a list of calories of each fooditem carried by the elves.
# Each line is one item, just the number of calories.
# Elves are separated by an empty line
DEBUG = False
# Read the input file into a list of elves, each of which is a list of calories
with open("input.txt", "r") as f:
# Split on empty lines. Each elf into its own string.
data = f.read()[:-1].split("\n\n")
# Split on newlines. Each elf into its own array, with each line into its own string.
data = [x.split("\n") for x in data]
# Parse data to integer. Same as above, but each line is an integer.
data = [[int(y) for y in x] for x in data]
if DEBUG: print("All data: ", data)
# Part 1
# Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
sums = [sum(x) for x in data]
if DEBUG: print("All sums: ", sums)
# Sort the sums
sums.sort()
# print("Sorted sums: ", sums)
print("======")
print("Part 1: ", sums[-1])
print("======")
# Part 2
# Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?
if DEBUG: print("Top 3: ", sums[-3:])
print("======")
print("Part 2: ", sum(sums[-3:]))
print("======")