Note: deprecated.
See instead:
See:
Use this command, choosing the day number you want:
$ lein run-day-01 < resources/day_01/input.txt
$ lein test-refresh
See also:
- https://github.com/sroccaserra/aoc2015#learnings
- https://github.com/sroccaserra/aoc2018#learnings
- https://github.com/sroccaserra/aoc2020#learnings
- https://github.com/sroccaserra/aoc2021#learnings
- https://github.com/sroccaserra/aoc2022#learnings
-
Using
stdin
instead of a file is handy: I can useecho
to easily pass various made up input to themain
function. -
If I have a strictly monotone function f(x) = y that is hard to invert, to compute the amount of x provided by a large amount of y I can use binary search (see day 14 code,
f(fuel) = ore
). -
The upper triangular matrix with only ones can be constructed by summing I and powers of J (J being the matrix with ones only above the diagonal)
[ 1 1 1 1 ]
[ 0 1 1 1 ] = I + J + J^2 + J^3
[ 0 0 1 1 ]
[ 0 0 0 1 ]
with:
[ 0 1 0 0 ]
[ 0 0 1 0 ] = J
[ 0 0 0 1 ]
[ 0 0 0 0 ]
- The nth diagonal of Pascal's Triangle can be computed iteratively: xk = xk-1*(n+k)/k
- There is a Pascal Matrix:
- Fermat's little theorem is useful to compute the modular inverse, see day 22 comments
-
Small clojure setup:
- an
src/toto.clj
file, with a(ns toto)
declaration and a(defn -main [& args] (println "salut"))
function - run with
clj -M -m toto
- an
-
clj
REPL starts faster thanlein repl
-
For fast feedback, start a REPL and don't close it. Change the file and reload it, with
(do (use 'day-21.main :reload) (-main))
for example. Not working the first time it seems? -
fn defines a recursion point, so I don't need to
loop
torecur
afn
(also true fordefn
). -
Destructuring is powerfull in Clojure.
-
Clojure has
:pre
and:post
conditions. See: fn. -
We can substitute functions, during a test execution for instance, with with-redefs.
-
I can access the Clojure doc from the REPL: doc, find-doc, apropos.
-
I can use run! to print each item in a lazy collection on a new line.
- In
Data.Array
,listArray
can construct an array using its previous values (sort of recrusively):
fibs :: Array Int Integer
fibs = listArray (0, n-1) $ 0 : 1 : [fibs!(i-1) + fibs!(i-2) | i <- [2..n-1]]
where n = 100
- For a defaultdict,
tuple(sorted(a_defaultdict.items()))
can be inserted in a set (to build a history of states) - For a set,
tuple(sorted(a_set))
can be inserted in a set (to build a history of states)