Grilled Cheese

ExploreLog inSign up
Terms of UsePrivacy PolicyCommunity StandardsHelpGet the app

Grilled Cheese is a product of Village Compute

Version devBuilt at: 2026-12-03 22:42:33 EST

PhotosVideosAllPeople
LatestRanked
package day05

import kodvent.graph.bfs

class Day05 {
    fun part1(buttons: List<String>): Int =
        bfs(0, maxDistance = 3) { display -> buttons.map { it.push(display) } }.keys.sum()

    fun part2(buttons: List<String>): Int =
        bfs(0) { display -> buttons.map { it.push(display) }.filter { it < 10_000 } }.keys.sum()

    private fun String.push(display: Int): Int {
        val label = removeSurrounding("[", "]")
        return when {
            label == "oct" -> display.toString(8).toInt()
            label == "bksp" -> display / 10
            label.startsWith('*') -> display * label.drop(1).toInt()
            label.startsWith('/') -> display / label.drop(1).toInt()
            "->" in label -> label.split("->").let { (from, to) -> "$display".replace(from, to).toInt() }
            else -> "$display$label".toInt()
        }
    }
}
Photo, no description
Advent of Code day 5 solution screenshot
∩(/+⍥(◴⊂⟜(▽⊸<1e4♭⊂₆⊃(⌝⊥10⊥8|≡⍜°⋕(+×7⊸=@0)|≡⍜°⋕(+×8⊸=@1)|⊞×8_9|⌊⊞÷2_10|⊞+4_9×10)))⊙0)3 ∞
Photo, no description
Photo, no description
# frozen_string_literal: true

# Advent of code - Day 5
class RhythmicFactory
  attr_reader :data

  def initialize(filename)
    @data = File.read(filename).strip.delete('[]').split
  end

  def part1 = combinations.to_a.map(&:to_i).sum

  def part2 = combinations_infinite.to_a.map(&:to_i).sum

  private

  def combinations
    ['0'].to_set.tap { |inputs| 3.times { inputs.merge(unfold(inputs)) } }
  end

  def unfold(inputs, limit: nil)
    inputs.flat_map { |inp| buttons.map { |but| but.push(inp, limit:) } }.to_set
  end

  def buttons = data.map { Button.new(it) }

  # part2
  def combinations_infinite
    ['0'].to_set.tap do |inputs|
      loop do
        results = unfold(inputs, limit: 4)
        break if results <= inputs # no new resuls

        inputs.merge(results)
      end
    end
  end
end

if __FILE__ == $PROGRAM_NAME
  obj = RhythmicFactory.new(ARGV[0])
  puts obj.part1
  puts obj.part2
end
dec05 =>

  input := io.stdin.read_string! .split_if (c -> "[] \n".find c ??) .force

  process(n, button) =>
    if      button.starts_with "*" then n * button.substring 1 .parse_i64!
    else if button.starts_with "/" then n / button.substring 1 .parse_i64!
    else if button.contains "->"   then a := button.substring 0 1
                                        b := button.substring 3 4
                                        ($n).replace a b .parse_i64!
    else if button = "oct"         then n.oct.parse_i64!
    else if button = "bksp"        then n / 10
    else if "0" <= button <= "9"   then 10*n + button.parse_i64!
                                   else panic "unknown button [$button]"

  part1 =>
    (1..3).foldf (container.ps_set i64 .new [0]) found,_->
            found.combine input process
                 .reduce found (f,n -> f.add n)
          .sum

  part2 =>
    (1..).reduce (container.ps_set i64 .new [0]) found,_->
            r := found.combine input process
                      .filter (n -> 0 <= n <= 9999)
                      .reduce found (f,n -> f.add n)
            r = found ? abort r : r
         .sum

  say "$part1:$part2"
Photo, no description
Photo, no description
{
  type buttons = App of int | Mul of int | Div of int | Swp of char * string | Oct | Del
  let press screen = function
    | App d -> screen * 10 + d
    | Mul d -> screen * d
    | Div d -> screen / d
    | Swp (a, b) -> int_of_string String.(concat b (split_on_char a (string_of_int screen)))
    | Oct -> int_of_string (Printf.sprintf "%o" screen)
    | Del -> screen / 10
}

let digit = ['0' - '9']

rule buttons = parse
| eof { [] }
| ' ' | '\n' { buttons lexbuf }
| "[" (digit as d) "]" { App (int_of_char d - int_of_char '0') :: buttons lexbuf }
| "[*" (digit as d) "]" { Mul (int_of_char d - int_of_char '0') :: buttons lexbuf }
| "[/" (digit as d) "]" { Div (int_of_char d - int_of_char '0') :: buttons lexbuf }
| "[" (digit as a) "->" (digit as b) "]" { Swp (a, String.make 1 b) :: buttons lexbuf }
| "[oct]" { Oct :: buttons lexbuf }
| "[bksp]" { Del :: buttons lexbuf }

{
  let buttons = buttons (Lexing.from_channel stdin) in
  let smash screen = List.map (press screen) buttons in
  let screens =
    let rec loop n screens =
      if n = 0 then screens else
      loop (n - 1) List.(map smash screens |> concat |> sort_uniq Stdlib.compare)
    in loop 3 [0]
  in
  Printf.printf "%d\n" (List.fold_left (+) 0 screens)
}
Photo, no description
library(stringr)
buttons <- list(
  
  function(n) if (n == 0) 7 else n * 10 + 7,
  
  function(n) n * 5,
  
  function(n) {
    as.character(n) |>
      str_replace_all("1", "9") |>
      as.integer()
  },
  
  function(n) n %/% 3,
  
  function(n) {
    as.integer(as.character(as.octmode(n)))
  },
  
  function(n) {
    as.character(n) |>
      str_replace_all("0", "5") |>
      as.integer()
  },
  
  function(n) if (n == 0) 9 else n * 10 + 9,
  
  function(n) n * 9,
  
  function(n) if (n < 10) 0 else n %/% 10
)
# all values within 3 is tractable
solve_microwave_part1 <- function() {
  
  depth0 <- 0
  
  depth1 <-
    vapply(
      buttons,
      \(button) button(0),
      numeric(1)
    ) |>
    unique()
  
  depth2 <-
    unlist(
      lapply(
        depth1,
        \(value)
        vapply(
          buttons,
          \(button) button(value),
          numeric(1)
        )
      ),
      use.names = FALSE
    ) |>
    unique()
  
  depth3 <-
    unlist(
      lapply(
        depth2,
        \(value)
        vapply(
          buttons,
          \(button) button(value),
          numeric(1)
        )
      ),
      use.names = FALSE
    ) |>
    unique()
  
  reachable <-
    c(depth0, depth1, depth2, depth3) |>
    unique()
  
  sum(reachable)
}
solve_microwave_part1()
A screenshot of my times for Day 1-5 of Advent of Code 2026.

Day 5
00:16:20 / 00:32:40

Day 4
00:27:03 / 00:29:12

Day 3
00:05:51 / 00:30:13

Day 2
00:07:22 / 00:16:51


Day 1
23:15:13 / 23:16:56
Photo, no description
Photo, no description
Photo, no description
Photo, no description
Load more