Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

sroccaserra/aoc2019

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Note: deprecated.

See instead:

Advent of Code 2019

See:

Running

Use this command, choosing the day number you want:

$ lein run-day-01 < resources/day_01/input.txt

Running tests

$ lein test-refresh

Learnings

See also:

General

  • Using stdin instead of a file is handy: I can use echo to easily pass various made up input to the main 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 ]

Clojure

  • 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
  • clj REPL starts faster than lein 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 to recur a fn (also true for defn).

  • 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 doto as a kind of tap or kestrel.

  • I can use run! to print each item in a lazy collection on a new line.

Haskell

  • 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

Python

  • 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)

References