commit 89f1b4cc16176e9af1cd4d636565aeee754a76ae
parent f84dd861d09545ffe1da81ca7ceb57fbf9802f30
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Sun, 4 Dec 2022 05:07:12 -0800
Specifying types for function arguments.
I don't know if the practice is ver Julia-ish, especially since they
really seem to like using types for method overloading, but I find that
"type hints" often make Adent of Code solutions easier to read, since
you're often switching datastructures to represent related concepts
(this doesn't happen so much for dayjob code).
Diffstat:
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/day_3.jl b/src/day_3.jl
@@ -15,7 +15,7 @@ input = readlines("data/day_3.txt")
# Return a tuple of arrays for a rucksack, consisting of the priority values of
# all of the (unique) items in the compartment. This abuses ASCII/UTF-8
# encoding of [a-zA-Z]
-function get_compartments(rucksack)
+function get_compartments(rucksack::String)
rucksack = Vector{UInt8}(rucksack)
rucksack = map(x->(x>Int('Z')) ? x-Int('a')+1 : x-Int('A')+27,
rucksack)
@@ -24,12 +24,12 @@ function get_compartments(rucksack)
end
# This is written to assume there's only one item in both compartments
-function get_compartment_intersection_priority(rucksack)
+function get_compartment_intersection_priority(rucksack::String)
compartments = get_compartments(rucksack)
- intersect(compartments[1], compartments[2])[1]
+ intersect(compartments...)[1]
end
-function part_1(input)
+function part_1(input::Array{String,1})
sum(map(get_compartment_intersection_priority, input))
end
@assert part_1(example) == 157
@@ -44,12 +44,12 @@ end
# value of the "badge". Also, you can splat the input to intersect (and I did
# that for my original solution), but I saw somebody else call reduce and that
# feels worth cribbing (even if it's no more efficient).
-function get_rucksacks_badge(rucksacks)
+function get_rucksacks_badge(rucksacks::Array{String,1})
reduce(intersect,
map(x->union(get_compartments(x)...),rucksacks))[1]
end
-function part_2(input)
+function part_2(input::Array{String,1})
sum(map(get_rucksacks_badge,
Iterators.partition(input, 3)))
end