Giter VIP home page Giter VIP logo

advent-of-prolog-2023's Introduction

Solutions to Advent of code 2023 with Scryer-Prolog

I chose Scryer Prolog because I think is the more reasonable Prolog out there (not that I investigated much, but swi always felt too deviant from the ISO Prolog, in the sense that in the past many times I searched for help the answer was "nah, that is just a SWI-Prolog specific thing"). Also it feels principled, simple enough, yet powerful enough. And the authors are super responsive.

I'm not an expert on Prolog, but I like how clean the Prolog solution looks.

This being said, I try to be as idiomatic as possible, using DCGs where possible and avoiding "imperative style" prolog, and trying to avoid cuts.

Any improvements or corrections you see on the code, please send them my way as only by correcting our previous knowledge we learn new stuff.

Tricks

I found quite useful to have the following function around in my emacs as ediprolog does not work for me on Windows:

(defun javier/scryer ()
  "Call scryer on save"
  (when (eq major-mode 'prolog-mode)
    (message "%s" (string-trim
      (shell-command-to-string (format "scryer-prolog '%s' -g halt." buffer-file-name))))))

(add-hook 'after-save-hook #'javier/scryer)

It shows on the minibuffer whether scryer complains on the current file, which makes it somewhat easier to find syntax errors and missing punctuation symbols.

Commentaries

Number Part Commentary
1 1 N/A
1 2 Solved just by ;'ed DCGs
2 1 N/A
2 2 N/A
3 1 A bit more complex due to usage of big matrices, but can be parsed incrementally easily
3 2 More complex state to carry
4 1 N/A
4 2 N/A
5 1 Used asserta so quite bad practice but it works
5 2 Needed to manually craft CLP(Z) domains and perform multiple intersections, not trivial
6 1 Solved with labeling, a bit of math would have been faster
6 2 Ditto
7 1 Boilerplate-y because we can't overload compare, had to create assoc_by.pl
7 2 N/A
8 1 N/A
8 2 If I had not visualized the data on circo I might had gone the route of using dynamic programming/tabling
9 1 N/A
10 1 N/A
10 2 Quite complicated to get the math right, using ray casting solves it
11 1 N/A
11 2 N/A
12 1 Easy to brute-force
12 2 UNSOLVED

Disclaimer: I went back to read The Power of Prolog because I was feeling my answers were A) not fast enough B) not declarative. From this point on I try to minimize the use of custom "imperative" constructs I had in my src/utils.pl and instead declare relations. The results are much faster programs.

Number Part Commentary
13 1 N/A
13 2 Ah, a nice declarative solution
14 1 N/A
14 2 N/A

advent-of-prolog-2023's People

Contributors

jasagredo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.