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
spiral-ish shapes of coloured dots with advent of code day 2 written in the middle
Photo, no description
Photo, no description
Photo, no description
Imagen con algunas lineas de código en python para el Advent of Code
Get off my lawn! - Grumpy Old Robot.
Photo, no description
Photo, no description
solution for advent of code 2026 day 2 in hopefully ok c++
Photo of my desk. In picture: A black desk; The Art of Writing Efficient Programs by Fedor G. Pikus; Apple magic keyboard; a Christmas elf plush; Analogue Super Nt with 8-Bit do controller; some wires' and Logitech speakers.
d ← /++×6⊸=0◿6◫-⊂0
↘¯1&fras "day2.txt"
⊃(d|d◿6¯⍆◿6¯) ⋉"roygbv"
Photo, no description
Photo, no description
Photo, no description
dec02 =>

  input := io.stdin.read_string! .trim.utf8

  part1 =>
    # start at pos 0, iterate over the program
    input.foldf 0 pos,wp->
      # from the next position, find the first matching waypint
      ((pos+1)..).filter (s -> "roygbv".utf8[s % 6] = wp)
                 .first!

  part2 =>
    # active_wps, i.e., exclude shortest with zero count
    active_wps => "oygbvr".utf8
                          .drop_while (wp -> input.filter =wp .is_empty)
                          .count
    (6*input.count        # repeated waypoints add 6 steps
     - active_wps         # new ones add only 5, one less
     + 1)                 # except the very first

  say "$part1:$part2"
Advent of Code day 2 solution screenshot
public enum Day02 {
  public static let puzzle = Puzzle(
    day: 2,
    title: "Fulminating Farmland",
    part1: { input in
      let instructions = try parseInput(input: input)

      return "\(calculateDistance(start: startingWaypointID, instructions: instructions))"
    },
    part2: { input in
      let reorderedInstructions = try parseInput(input: input).sorted(by: >)

      return "\(calculateDistance(start: startingWaypointID, instructions: reorderedInstructions))"
    }
  )

  private static func parseInput(input: String) throws -> [Int] {
    // Trim surrounding whitespace, then convert each waypoint colour to its ID.
    // Throw if any character is not a waypoint colour.
    return try Array(input.trimmingCharacters(in: .whitespacesAndNewlines)).map { character in
      guard let id = waypointID[character] else {
        throw InputError.invalidCharacter(character)
      }
      return id
    }
  }

  private static func calculateDistance(start: Int, instructions input: [Int]) -> Int {
    var currentWP = start, distance: Int = 0

    for nextWP in input {
      distance += stepsMatrix[currentWP][nextWP]
      currentWP = nextWP
    }

    return distance
  }

  private static let startingWaypointID = 5
  private static let waypointID: [Character: Int] = ["r": startingWaypointID, "o": 0, "y": 1, "g": 2, "b": 3, "v": 4]

  private static let stepsMatrix = [[6,1,2,3,4,5],
                                    [5,6,1,2,3,4],
                                    [4,5,6,1,2,3],
                                    [3,4,5,6,1,2],
                                    [2,3,4,5,6,1],
                                    [1,2,3,4,5,6]]

}
Photo, no description
Photo, no description
Load more