2022 day 1 (eh idk if i wanna do it this year)
This commit is contained in:
parent
a56eb52f08
commit
77bea7b01a
20
2022/01/01.ex
Normal file
20
2022/01/01.ex
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{:ok, contents} = File.read("input.txt")
|
||||||
|
elves = contents
|
||||||
|
|> String.split("\n\n", trim: true) # Split into elves.
|
||||||
|
|> Enum.map(fn elf -> elf # For each elf's food,
|
||||||
|
|> String.split("\n", trim: true) # split into calorie counts.
|
||||||
|
|> Enum.map(fn calories -> calories # For each calorie count,
|
||||||
|
|> String.to_integer # convert it to an integer.
|
||||||
|
end) # We're done with individual counts.
|
||||||
|
|> Enum.sum # Now add up each elf's calories.
|
||||||
|
end) # We're done with individual elves.
|
||||||
|
|
||||||
|
_part1 = elves # For part 1,
|
||||||
|
|> Enum.max # we just want the highest calorie count.
|
||||||
|
|> IO.inspect
|
||||||
|
|
||||||
|
_part2 = elves # For part 2,
|
||||||
|
|> Enum.sort # we can sort elves (default low->high)
|
||||||
|
|> Enum.take(-3) # and take the last 3 (highest 3) calorie counts,
|
||||||
|
|> Enum.sum # then add up those top 3 elves' calorie counts.
|
||||||
|
|> IO.inspect
|
12
2022/01/01.py
Normal file
12
2022/01/01.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
with open("input.txt", "r") as f:
|
||||||
|
elves = f.read().split('\n\n')
|
||||||
|
elf_calories = []
|
||||||
|
for elf in elves:
|
||||||
|
snacks = elf.split('\n')
|
||||||
|
calories = 0
|
||||||
|
for snack in snacks:
|
||||||
|
calories += int(snack)
|
||||||
|
elf_calories.append(calories)
|
||||||
|
elf_calories = sorted(elf_calories, reverse=True) # high -> low
|
||||||
|
print(f"Part 1: {elf_calories[0]}") # Top elf
|
||||||
|
print(f"Part 2: {sum(elf_calories[:3])}") # Top 3 elves
|
2237
2022/01/input.txt
Normal file
2237
2022/01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue