Code for the Advent of Code event
aoc
advent-of-code
1#!/usr/bin/env ruby
2# frozen_string_literal: true
3
4require_relative '../../../lib/astar'
5require_relative '../../../lib/grid'
6
7INPUT = ARGF.readlines.map(&:strip).map { _1.chars.map(&:to_i) }
8WIDTH = INPUT[0].size
9HEIGHT = INPUT.size
10START = Vector[0, 0]
11GOAL = Vector[WIDTH - 1, HEIGHT - 1]
12FULL_WIDTH = WIDTH * 5
13FULL_HEIGHT = HEIGHT * 5
14FULL_GOAL = Vector[FULL_WIDTH - 1, FULL_HEIGHT - 1]
15
16grid = GridWithWeights.new(FULL_WIDTH, FULL_HEIGHT)
17
18INPUT.each_with_index do |row, y|
19 row.each_with_index do |weight, x|
20 grid.add_weight Vector[x, y], weight
21 (1..4).each do |n|
22 (0..4).each do |n2|
23 new_weight = ((weight + n + n2 - 1) % 9) + 1
24 grid.add_weight Vector[x + (WIDTH * n), y + (HEIGHT * n2)], new_weight
25 grid.add_weight Vector[x + (WIDTH * n2), y + (HEIGHT * n)], new_weight
26 end
27 end
28 end
29end
30
31_, costs = AStar.find_path(grid, START, GOAL)
32_, full_costs = AStar.find_path(grid, START, FULL_GOAL)
33
34puts costs[GOAL]
35puts full_costs[FULL_GOAL]