Skip to content

Instantly share code, notes, and snippets.

@bo-tato
Created April 25, 2023 10:46
Show Gist options
  • Save bo-tato/7062c3770cbe7df105b8aa839a244126 to your computer and use it in GitHub Desktop.
Save bo-tato/7062c3770cbe7df105b8aa839a244126 to your computer and use it in GitHub Desktop.
protohackers challenge4
(defpackage :challenge4
(:use cl usocket)
(:export main)
(:import-from flexi-streams
:string-to-octets
:octets-to-string)
(:import-from serapeum
:fmt
:@
:dict))
(in-package :challenge4)
(defvar *db* (dict "version" "Ken's Key-Value Store 1.0"))
(defun handler (msg)
(destructuring-bind (key &optional value)
(str:split "=" (octets-to-string msg) :limit 2)
(if value
(unless (string= key "version")
(setf (@ *db* key) value)
nil)
(string-to-octets (fmt "~a=~a" key (@ *db* key))))))
(socket-server "0.0.0.0" 1234 'handler nil :protocol :datagram)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment