Skip to content

Instantly share code, notes, and snippets.

View stewSquared's full-sized avatar
Advent of Code 2023

Stewart Stewart stewSquared

Advent of Code 2023
View GitHub Profile

Tonights theme is Logic Puzzles. We have here two logic problems found in section 4.3.2 of "The Structure and Interpretation of Computer Programs" (SICP).

Please code and share your solution online at: http://www.scalapuzzle.com/dojo/index/550154DC80 Click "Enter" to be asigned an avatar.

While these puzzles can likely each be solved in about 15 minutes by hand, the goal tonight is to write a program in such a way that it leaves us confident in it's correctness. This should be an interesting

/**
* The following is an implementation of Combinatory Logic as a scala DSL. It
* is evaluated using head-first graph reduction, as described in "Compiling
* Functional Languages" by Antoni Diller. This particular gist is adapted from
* https://github.com/drivergroup/spn-combinatory-logic which I wrote for Scala
* Puzzle Night, a meetup that I run occasionally. See the README.md in that
* project for more information.
*
* The interesting bits of this are eval, reduce, and extract. The second half
* of this file is a demonstration. It's written so as to be easy to `:paste`
@stewSquared
stewSquared / clone-github.sh
Created May 18, 2019 21:56
clone github to consistent local directories
#!/usr/bin/bash
clone-github () {
local -r user_repo=$(sed -e "s|.*github.com/||" <<< $1)
local -r dest="$HOME/github.com/$user_repo"
if [[ -e $dest ]]; then
echo "$dest already exists"
echo "cd -- $dest"
cd $dest
return 1
@stewSquared
stewSquared / ByteCode.scala
Created June 1, 2020 20:24
Simple translation to ByteCode example
import scala.collection.mutable.ListBuffer
sealed trait ByteCode
object ByteCode {
case class Push(n: Int) extends ByteCode
case object Add extends ByteCode
}
sealed trait Instruction
for DAY in {1..25}; do echo "Day $DAY:"; jq -r --arg day $DAY '.members | .[] | (.completion_day_level | select(has($day)) | .[$day] | .[] | .get_star_ts | tostring | strptime("%s") | (((strftime("%d") | tonumber) - ($day | tonumber) | . * 24) + (strftime("%H") | tonumber) | tostring) + strftime(":%M:%S - ")) + .name' 94105.json | sort -h; done | less #aoc-splits
@stewSquared
stewSquared / Day17.worksheet.sc
Last active July 12, 2022 11:00
Playing with Scala 3 opaque types for AoC 2021 Day 17
val input = io.Source.fromResource("2021/day-17-1.txt").getLines.next()
case class Point(x: Int, y: Int)
object Point:
opaque type Position <: Point = Point
opaque type Velocity <: Point = Point
opaque type Origin <: Position = Position
def Position(x: Int, y: Int): Position = Point(x, y)
@stewSquared
stewSquared / Day22.worksheet.sc
Last active July 12, 2022 10:59
Advent of Code 2021 Day 22 in Scala 3
val steps =
io.Source.fromResource("2021/day-22-1.txt").getLines.toList.collect {
case s"$step x=${Range(xs)},y=${Range(ys)},z=${Range(zs)}" =>
Step(step == "on", Area(xs, ys, zs))
}
case class Step(on: Boolean, area: Area):
def countLastTouched(future: List[Step]): Long =
val overlaps = future.flatMap(_.area.intersect(area))
val touchedLater = Area.unionSize(overlaps)
@stewSquared
stewSquared / Day19.worksheet.sc
Last active July 13, 2022 07:56
Advent of Code 2020 Day 19 in Scala 3
val input = io.Source.fromResource("2020/day-19.txt").getLines.toList
enum Rule:
case MatchString(s: String)
case AndThen(a: Rule, b: Rule)
case Either(a: Rule, b: Rule)
case Ref(id: Int)
import Rule.*
@stewSquared
stewSquared / Day05.worksheet.sc
Last active December 6, 2022 01:17
Advent of Code 2022 Day 5 worksheet output
type State = Vector[String]
val lines = io.Source.fromResource("2022/day-05.txt").getLines()
// lines: Iterator[String] = <iterator>
val drawing = lines.takeWhile(_.nonEmpty).toVector
// drawing: Vector[String] = Vector([T] [V] [W] , [V] [C] [P] [D] [B] , [J] [P] [R] [N] [B] [Z] , [W] [Q] [D] [M] [T] [L] [T] , [N] [J] [H] [B] [P] [T] [P] [L] , [R] [D] [F] [P] [R] [P] [R] [S] [G], [M] [W] [J] [R] [V] [B] [J] [C] [S], [S] [B] [B] [F] [H] [C] [B] [N] [L], 1 2 3 4 5 6 7 8 9 )
val steps = lines.toList
// steps: List[String] = List(move 7 from 3 to 9, move 6 from 2 to 1, move 2 from 4 to 8, move 10 from 8 to 4, move 1 from 2 to 4, move 15 from 4 to 1, move 28 from 1 to 3, move 1 from 2 to 5, move 7 from 5 to 9, move 5 from 9 to 5, move 21 from 3 to 1, move 1 from 6 to 4, move 4 from 9 to 2, move 7 from 9 to 2, move 4 from 2 to 6, move 1 from 9 to 1, move 2 from 4 to 9, move 2 from 7 to 4, move 4 from 3 to 5, move 2 from 7 to 9, move 5 fro