aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-01-26 22:09:57 +0100
committerOscar Najera <hi@oscarnajera.com>2023-01-26 22:09:57 +0100
commitc7f01066164817bbf624f2e7a63c6ac0aeea5f61 (patch)
tree8dec4fd44655094ae59ca101672e9ee3ed339f7b
parent31aa0839953395f5998c7ec4350760cfebae6506 (diff)
downloadscratch-c7f01066164817bbf624f2e7a63c6ac0aeea5f61.tar.gz
scratch-c7f01066164817bbf624f2e7a63c6ac0aeea5f61.tar.bz2
scratch-c7f01066164817bbf624f2e7a63c6ac0aeea5f61.zip
Learning some julia
-rw-r--r--AoC2022/01/solver.jl10
-rw-r--r--AoC2022/02/solver.jl69
-rw-r--r--AoC2022/03/solver.el40
-rw-r--r--AoC2022/03/solver.jl12
-rw-r--r--AoC2022/makefile4
5 files changed, 108 insertions, 27 deletions
diff --git a/AoC2022/01/solver.jl b/AoC2022/01/solver.jl
new file mode 100644
index 0000000..a73deb7
--- /dev/null
+++ b/AoC2022/01/solver.jl
@@ -0,0 +1,10 @@
+using Test
+
+rations = open("input", "r") do f
+ map(x->parse.(Int, eachsplit(x)) |> sum, eachsplit(read(f, String), "\n\n")) |> sort
+end
+
+@testset "solutions" begin
+ @test last(rations) == 75622
+ @test rations[end-2:end] |> sum == 213159
+end
diff --git a/AoC2022/02/solver.jl b/AoC2022/02/solver.jl
new file mode 100644
index 0000000..b93a88f
--- /dev/null
+++ b/AoC2022/02/solver.jl
@@ -0,0 +1,69 @@
+using Match
+using Test
+
+plays = open("input") do f
+ split.(eachsplit(read(f, String),"\n", keepempty=false))
+end
+
+translate(play) = @match play begin
+ "A" || "X" => :rock
+ "B" || "Y" => :paper
+ "C" || "Z" => :scissors
+end
+
+weigth(play)= @match play begin
+ :rock => 1
+ :paper => 2
+ :scissors => 3
+end
+
+function trumps(play)
+ @match play begin
+ :rock => :scissors
+ :scissors => :paper
+ :paper => :rock
+ _ => error("Invalid play")
+ end
+end
+
+function looses(play)
+ @match play begin
+ :rock => :paper
+ :scissors => :rock
+ :paper => :scissors
+ _ => error("Invalid play")
+ end
+end
+
+function points(my, other)
+ if trumps(my) == other
+ return 6
+ elseif my == other
+ return 3
+ else
+ return 0
+ end
+end
+
+function fixed_play(oponent, my)
+ oponent_hand = translate(oponent)
+ my_hand = translate(my)
+ points(my_hand, oponent_hand) + weigth(my_hand)
+end
+
+function reactive_play(oponent, my)
+ oponent_hand = translate(oponent)
+ my_hand = @match my begin
+ "X" => trumps(oponent_hand) # I loose
+ "Y" => oponent_hand
+ "Z" => looses(oponent_hand) # I win
+ _ => error("Invalid input")
+ end
+ points(my_hand, oponent_hand) + weigth(my_hand)
+end
+
+
+@testset "solutions" begin
+ @test map(((op, my),) -> fixed_play(op,my), plays) |> sum == 12535
+ @test map(((op, my),) -> reactive_play(op,my), plays) |> sum == 15457
+end
diff --git a/AoC2022/03/solver.el b/AoC2022/03/solver.el
index 355ddf7..62b91e2 100644
--- a/AoC2022/03/solver.el
+++ b/AoC2022/03/solver.el
@@ -15,6 +15,7 @@
;;
;;; Code:
(require 'subr-x)
+(require 'ert)
(defsubst solver-recover-value (cross)
(let ((result 0))
(while (= 0 (logand cross 1))
@@ -34,45 +35,32 @@
(defun solver-line-priority (str)
(cl-assert (length> str 0))
- (let ((mid (/ (length str) 2))
- (left-pack 0) (right-pack 0))
- (cl-loop for l across (substring str 0 mid)
- for r across (substring str mid)
- do (progn
- (setf left-pack (logior left-pack (ash 1 (- l 64)))) ;; ASCII letters start at 65=A
- (setf right-pack (logior right-pack(ash 1 (- r 64)))))
- for cross = (logand left-pack right-pack)
- when (< 0 cross)
- return (solver-to-priority (solver-recover-value cross)))))
+ (let ((mid (/ (length str) 2)))
+ (thread-first
+ (cl-intersection (string-to-list (substring str 0 mid))
+ (string-to-list (substring str mid)))
+ (car) (- 64)
+ (solver-to-priority))))
(ert-deftest test-line-priority ()
(should (= 16 (solver-line-priority "vJrwpWtwJgWrhcsFMMfFFhFp")))
(should (= 19 (solver-line-priority "hMHLcmGLMLhHmsRMsSvsQSqrsrlJTTdV" )))
(should (= 20 (solver-line-priority "ttgJtRGJQctTZtZT"))))
-(defsubst solver-badge (packs)
- (seq-reduce (lambda (acc item)
- (logior acc (ash 1 (- item 64))))
- packs 0))
-
(defun solver-badge-priority (group-packs)
(thread-last group-packs
- (split-string)
- (mapcar #'solver-badge)
- (apply #'logand)
- (solver-recover-value)
+ (mapcar #'string-to-list)
+ (cl-reduce #'cl-intersection)
+ (car) (+ -64)
(solver-to-priority)))
(defun solver (task lines)
(with-temp-buffer
(insert-file-contents "input")
- (goto-char (point-min))
- (cl-loop while (not (eobp))
- sum
- (funcall task
- (buffer-substring-no-properties (point)
- (line-end-position lines)))
- do (forward-line lines))))
+ (let ((packs (split-string (buffer-string))))
+ (apply #'+
+ (mapcar task (if (= lines 1)
+ packs (seq-partition packs 3)))))))
(ert-deftest test-problems ()
(should (= 8072 (solver #'solver-line-priority 1)))
diff --git a/AoC2022/03/solver.jl b/AoC2022/03/solver.jl
new file mode 100644
index 0000000..1ebadbf
--- /dev/null
+++ b/AoC2022/03/solver.jl
@@ -0,0 +1,12 @@
+using Test
+
+data = open("input") do f
+ split(read(f, String))
+end
+
+priority(x) = islowercase(x) ? x - 'a' + 1 : x - 'A' +27
+
+@testset "solutions" begin
+ @test map(s-> intersect(s[1:length(s)÷2], s[length(s)÷2+1:end])[1] |> priority, data) |> sum == 8072
+ @test [intersect(data[i:i+2]...)[1] for i ∈ 1:3:length(data)] .|> priority |> sum == 2567
+end
diff --git a/AoC2022/makefile b/AoC2022/makefile
index 95062ff..318c409 100644
--- a/AoC2022/makefile
+++ b/AoC2022/makefile
@@ -9,7 +9,7 @@ clean:
emacs: ${PWD}/solver.el
- cd ${PWD} && emacs -batch -l ert -l solver.elc -f ert-run-tests-batch-and-exit
+ cd ${PWD} && emacs -batch -l ert -l solver.el -f ert-run-tests-batch-and-exit
lisp: ${PWD}/solver.lisp
cd ${PWD} && time sbcl --load ~/.sbclrc --load solver.lisp --eval '(fiveam:run-all-tests)' --non-interactive
@@ -20,4 +20,6 @@ rust: ${PWD}/solver.rs
elixir: ${PWD}/solver.exs
cd ${PWD} && elixir solver.exs
+julia: ${PWD}/solver.jl
+ cd ${PWD} && julia solver.jl
# end