Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.1 kB · 42 lines
Ruby
at rust
12345678910111213141516171819202122232425262728293031323334353637383940414243#!/usr/bin/env ruby# frozen_string_literal: true
# Shamelessly ported from https://notes.dt.in.th/20211224T121217Z7595
input = ARGF.readlines.map(&:chomp)block_size = input.size / 14slices = input.each_slice(block_size).to_aadds = slices.map { |s| [s[5], s[15]].map { _1.split.last.to_i } }x_adds = adds.map(&:first)y_adds = adds.map(&:last)divs = input.map(&:split).select { _1[0] == 'div' }.map { _1.last.to_i }results = []
find = ->(w, i, zz, path) { if i == 14 results << path.join[0...14].to_i return end dx = x_adds[i] dy = y_adds[i] div = divs[i] if div == 26 return if w - dx != zz.last next_zz = zz[0...-1] 9.downto 1 do |next_w| # rubocop:disable Style/IdenticalConditionalBranches find[next_w, i + 1, next_zz, [*path, next_w]] end else next_zz = [*zz, w + dy] 9.downto 1 do |next_w| # rubocop:disable Style/IdenticalConditionalBranches find[next_w, i + 1, next_zz, [*path, next_w]] end end}
9.downto 1 do |w| find[w, 0, [], [w], results]end
puts results.maxputs results.min