commit 7487d45e43c9b8d7d323a6d6befd9a5525e2aeaa
parent 3698bc608ac6fc291ae76fd58dbcbc9fbadc842e
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Fri, 2 Dec 2022 14:20:11 -0800
So mod() isn't the same as %. Good to know!
I wrote a (silly) function because I thought % was the same as mod(),
but actually % is rem(). So I can just skip % and go on with my life
happily, which is terrific.
Thanks Gunnar!
https://julialang.zulipchat.com/#narrow/stream/357313-advent-of-code-.282022.29/topic/day.2002/near/313598721
Diffstat:
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/src/day_2.jl b/src/day_2.jl
@@ -9,14 +9,10 @@ example = split("""
C Z""", "\n")
input = readlines("data/day_2.txt")
-function mod_pos(a, b)
- a >= 0 ? a % b : (a % b) + b
-end
-
# Note that this rearranges the input rows. Not sure if that will be a problem.
-# Maybe leftjoin needs to be told not to do that? IDK. Also Julia has the whack
-# negative modulus behavior. Finally, I don't know if this is idiomatic Julia
-# at all, I'm basically hacking dplyr into Julia and it's pretty ugly IMO.
+# Maybe leftjoin needs to be told not to do that? IDK. I don't know if this is
+# idiomatic Julia at all, I'm basically hacking dplyr into Julia and it's
+# pretty ugly IMO.
function parse_input(input)
scores = DataFrame(
a = ['A', 'B', 'C'],
@@ -31,7 +27,7 @@ function parse_input(input)
on = :b) |>
x->transform(x,
[:score_a, :score_b] =>
- ByRow((a, b) -> mod_pos(b-a+1, 3)) =>
+ ByRow((a, b) -> mod(b-a+1, 3)) =>
:win_score) |>
x->transform(x,
[:score_b, :win_score] =>
@@ -64,7 +60,7 @@ function parse_input_2(input)
on = :b) |>
x->transform(x,
[:score_a, :score_b] =>
- ByRow((a,b)->mod_pos(a+b-1, 3)) =>
+ ByRow((a,b)->mod(a+b-1, 3)) =>
:shape_score) |>
x->transform(x,
[:nrow, :shape_score, :score_b] =>