commit d7a7e7d1f14478632441563cef9ab0ddfaf7850e
parent c75efa5cd117c7cd77e394fde7edaff975b9ad2f
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Thu, 1 Dec 2022 10:52:58 -0800
Minor refactor and solution to day 1, part 2
Diffstat:
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/day01.jl b/day01.jl
@@ -23,9 +23,14 @@ part1_example = """
"""
# I'm sure this is hideous to Julia programmers.
+# This function returns an array of the total snack calories for each elf
+function snack_totals(input)
+ [sum([parse(Int64, food) for food in split(inventory, "\n")])
+ for inventory = split(rstrip(input), "\n\n")]
+end
+
function solve1(input)
- maximum([sum([parse(Int64, food) for food in split(inventory, "\n")])
- for inventory = split(rstrip(input), "\n\n")])
+ maximum(snack_totals(input))
end
@assert solve1(part1_example) == 24000
@@ -35,3 +40,15 @@ end
# directory for now. :/
println("Part 1: ", solve1(read("day01_part1_input.txt", String)))
+#############################################################################
+# Part 2
+#############################################################################
+
+function solve2(input)
+ sum(sort(snack_totals(input), rev=true)[1:3])
+end
+
+@assert solve2(part1_example) == 45000
+
+println("Part 2: ", solve2(read("day01_part1_input.txt", String)))
+