AoC Benchmarks

aoc2021-day10b Clojure/Babashka program

source code

; SPDX-License-Identifier: MIT
; Copyright (C) 2021 Tito Sacchi <tito@tilde.team>
; WARNING: These solutions were written while I was still learning Clojure and
; should by no means be taken as examples of good programming practice or fast
; implementations.

(ns aoc.2021.10a
  (:require
    [clojure.string :as str]
    [clojure.java.io :as io]))

(defn score [x]
  (case x
    \) 1
    \] 2
    \} 3
    \> 4
    nil 0))

(defn matching [x]
  (case x
    \( \)
    \[ \]
    \{ \}
    \< \>
    \) \(
    \] \[
    \} \{
    \> \<))

(defn opening [x]
  (case x
    \( true
    \[ true
    \{ true
    \< true
    \) false
    \] false
    \} false
    \> false))

(defn opened-stack [line]
  (reduce (fn [acc chr]
            (if (opening chr)
              (conj acc chr)
              (if
                (= (peek acc) (matching chr)) (pop acc)
                (reduced nil)))) () line))

(defn score-to-close [pending-opened]
  (reduce (fn [total-score chr]
            (+ (* 5 total-score) (score (matching chr)))) 0 pending-opened))

(defn median [xs] (nth (sort xs) (/ (count xs) 2)))

(println (->> *in*
              (java.io.BufferedReader.)
              (line-seq)
              (map opened-stack)
              (filter some?)
              (mapv score-to-close)
              (median)))
    

notes, command-line, and program output

NOTES:
Linux


Sun, 23 Jan 2022 16:39:16 GMT

COMMAND LINE:
bb -f aoc2021_day10b.clj_babashka-1.clj_babashka 0 < aoc2021_day10b-input1000.txt

PROGRAM FAILED 


PROGRAM OUTPUT:

----- Error --------------------------------------------------------------------
Type:     java.lang.ArithmeticException
Message:  integer overflow
Location: /bencher/tmp/aoc2021_day10b/tmp/aoc2021_day10b.clj_babashka-1.clj_babashka:52:16

----- Context ------------------------------------------------------------------
48:                 (reduced nil)))) () line))
49: 
50: (defn score-to-close [pending-opened]
51:   (reduce (fn [total-score chr]
52:             (+ (* 5 total-score) (score (matching chr)))) 0 pending-opened))
                   ^--- integer overflow
53: 
54: (defn median [xs] (nth (sort xs) (/ (count xs) 2)))
55: 
56: (println (->> *in*
57:               (java.io.BufferedReader.)

----- Stack trace --------------------------------------------------------------
clojure.lang.Numbers/LongOps - <built-in>
clojure.core/*               - <built-in>
aoc.2021.10a                 - /bencher/tmp/aoc2021_day10b/tmp/aoc2021_day10b.clj_babashka-1.clj_babashka:52:16
clojure.core/+               - <built-in>
aoc.2021.10a                 - /bencher/tmp/aoc2021_day10b/tmp/aoc2021_day10b.clj_babashka-1.clj_babashka:52:13
... (run with --debug to see elided elements)
aoc.2021.10a/median          - /bencher/tmp/aoc2021_day10b/tmp/aoc2021_day10b.clj_babashka-1.clj_babashka:61:15
aoc.2021.10a/median          - /bencher/tmp/aoc2021_day10b/tmp/aoc2021_day10b.clj_babashka-1.clj_babashka:54:1
aoc.2021.10a                 - /bencher/tmp/aoc2021_day10b/tmp/aoc2021_day10b.clj_babashka-1.clj_babashka:62:15
clojure.core/println         - <built-in>
aoc.2021.10a                 - /bencher/tmp/aoc2021_day10b/tmp/aoc2021_day10b.clj_babashka-1.clj_babashka:56:1