Code for the Advent of Code event
aoc
advent-of-code
1#!/usr/bin/env ruby
2# frozen_string_literal: true
3
4# Shamelessly ported from https://notes.dt.in.th/20211224T121217Z7595
5
6input = ARGF.readlines.map(&:chomp)
7block_size = input.size / 14
8slices = input.each_slice(block_size).to_a
9adds = slices.map { |s| [s[5], s[15]].map { _1.split.last.to_i } }
10x_adds = adds.map(&:first)
11y_adds = adds.map(&:last)
12divs = input.map(&:split).select { _1[0] == 'div' }.map { _1.last.to_i }
13results = []
14
15find = ->(w, i, zz, path) {
16 if i == 14
17 results << path.join[0...14].to_i
18 return
19 end
20 dx = x_adds[i]
21 dy = y_adds[i]
22 div = divs[i]
23 if div == 26
24 return if w - dx != zz.last
25 next_zz = zz[0...-1]
26 9.downto 1 do |next_w| # rubocop:disable Style/IdenticalConditionalBranches
27 find[next_w, i + 1, next_zz, [*path, next_w]]
28 end
29 else
30 next_zz = [*zz, w + dy]
31 9.downto 1 do |next_w| # rubocop:disable Style/IdenticalConditionalBranches
32 find[next_w, i + 1, next_zz, [*path, next_w]]
33 end
34 end
35}
36
379.downto 1 do |w|
38 find[w, 0, [], [w], results]
39end
40
41puts results.max
42puts results.min