Code for the Advent of Code event
aoc
advent-of-code
1#!/usr/bin/env ruby
2# frozen_string_literal: true
3
4map = Hash.new(10)
5ARGF.readlines.each_with_index do |l, r|
6 l.strip.chars.map(&:to_i).each_with_index { map[[_2, r]] = _1 }
7end
8
9def around(pos)
10 [
11 [pos[0], pos[1] - 1], [pos[0], pos[1] + 1],
12 [pos[0] - 1, pos[1]], [pos[0] + 1, pos[1]]
13 ]
14end
15
16lows = map.keys.select { |p| around(p).all? { map[p] < map[_1] } }
17
18puts lows.map { map[_1] + 1 }.sum
19
20puts lows.map { |start|
21 visited = Set.new
22 queue = [start]
23 while queue.any?
24 current = queue.pop
25 visited << current
26 queue.push *around(current).select { !visited.include?(_1) && map[_1] < 9 }
27 end
28 visited.size
29}.sort.pop(3).reduce(:*)