Clojure Core Logic for a Puzzle

11 May 2020

Lock Puzzle

I saw a puzzle pop up on Facebook a couple weeks ago, and it looked like a fun exercise for core.logic, since the puzzle simply requires keeping track of some constraints and reconciling them to one answer.

I had previously tinkered with the core.logic primer and I referred back to it to complete this little puzzle. I had originally coded some more complete rules about exclusion of some values which could have been implied by the puzzle, but I found they could be dropped and still get down to one answer. I started from the entire problem space of all digits and added the constraints to watch and verify each constraint’s effects.

(ns scratch.2020-05
  (:require [clojure.core.logic :as l]))

(l/run* [a b c]
  ;; all digits 0-9
  (l/membero a (range 10))
  (l/membero b (range 10))
  (l/membero c (range 10))
  ;; 6 8 2 one digit is right and in its right place
  (l/conde
    [(l/== a 6)]
    [(l/== b 8)]
    [(l/== c 2)])
  ;; 6 1 4 one digit is right but in the wrong place
  (l/conde
    [(l/membero a [1 4])]
    [(l/membero b [6 4])]
    [(l/membero c [6 1])])
  ;; 206 2 digits are right, but both are in the wrong place
  (l/conde
    [(l/membero a [0 6])
      (l/membero b [2 6])
      (l/membero c (remove #{2 0 6} (range 10)))]
    [(l/membero a [0 6])
      (l/membero c [2 0])
      (l/membero b (remove #{2 0 6} (range 10)))]
    [(l/membero b [2 6])
      (l/membero c [2 0])
      (l/membero a (remove #{2 0 6} (range 10)))]
      )
  ;; 3 8 0 one digit is right but in the wrong place
  (l/conde
    [(l/membero a [8 0])]
    [(l/membero b [3 0])]
    [(l/membero c [3 8])])
  ;; 7 3 8 all the digits are wrong
  (l/membero a (remove #{7 3 8} (range 10)))
  (l/membero b (remove #{7 3 8} (range 10)))
  (l/membero c (remove #{7 3 8} (range 10))))
;; => ([0 4 2])


Advent of Code

14 December 2019

For the second year I’m taking part in the Advent of Code with friends and coworkers. I’m using Clojure again this year.

The Advent of Code is a story and series of puzzles released as 2 parts daily. There’s a community of people competing at various levels to complete the puzzles faster than others. I was keeping up fine for the first 7 days or so, but I eventually fell behind like many people. Fortunately, everyone can continue to work at our own pace. The puzzles from previous years continue to be available.

I’m happy to find that I’m having an easier time with Clojure this year, and I’m more easily able to use it to describe the algorithms instead of struggling with the language. I publish my code to Github.


Missing Classes in IDEA

25 November 2019

I use InteliJ IDEA for work when working on Java code. When the IDE doesn’t work, though, it’s incredibly distracting.

I had a problem where IDEA would not find a few auxiliary classes in my application. It would highlight them as errors in imports, and the search would find the source file, but technically not the class definition. I’d poke at the problem over a couple weeks, but I’d otherwise ignore it most the time until it seemed to be losing more and more of my classes, some of which I was actively modifying.

Re-importing the project’s Maven build didn’t fix it; re-cloning a brand new project didn’t fix it; and re-installing IDEA didn’t even work. Finally, I found IDEA’s config directories, and wiped those out to start over, and that cleared up the problem: I was able to find all my classes again.


Browser Automation with Geb, Spock, and Groovy

22 October 2018

I recently gave a talk and demonstration, Browser Automation with Geb, Spock, and Groovy, at the Capital Area Software Engineers group in Harrisburg, PA. While explaining the whole stack of software, I showed how to:

  • Start a project in Gradle

  • Get the Geb and Webdriver dependencies in place

  • Get started in Spock testing framework

  • Start up a browser for testing

  • Interact with the page content

  • Wait for asynchronous content

  • Abstract away page components into Geb Page classes

The slides and all the code are available in my geb-preso repo. It includes copies of the code I had prepared, the code we wrote live as a group, and my little toy Planning Poker JS app I was testing.


All the Posts

May 2020

December 2019

November 2019

October 2018

May 2018

March 2018

February 2018

January 2018

November 2017

September 2017

June 2017