Created
December 4, 2024 05:43
-
-
Save carlwiedemann/2ed751b6bb5b6ffa96b0e82bf6c3a39f to your computer and use it in GitHub Desktop.
Advent of Code 2024 day004.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative "main" | |
| module Day004 | |
| INPUT = File.read('INPUT.txt') | |
| ########## | |
| # Part 1 # | |
| ########## | |
| grid = INPUT.to_grid | |
| answer1 = 0 | |
| grid.each do |v| | |
| [ | |
| [v, v + V[1, 0], v + V[2, 0], v + V[3, 0]], | |
| [v, v + V[-1, 0], v + V[-2, 0], v + V[-3, 0]], | |
| [v, v + V[0, 1], v + V[0, 2], v + V[0, 3]], | |
| [v, v + V[0, -1], v + V[0, -2], v + V[0, -3]], | |
| [v, v + V[1, 1], v + V[2, 2], v + V[3, 3]], | |
| [v, v + V[1, -1], v + V[2, -2], v + V[3, -3]], | |
| [v, v + V[-1, -1], v + V[-2, -2], v + V[-3, -3]], | |
| [v, v + V[-1, 1], v + V[-2, 2], v + V[-3, 3]], | |
| ].each do |dir| | |
| if grid.get_values(dir) == ['X', 'M', 'A', 'S'] | |
| answer1 += 1 | |
| end | |
| end | |
| end | |
| pp answer1 | |
| ########## | |
| # Part 2 # | |
| ########## | |
| answer2 = 0 | |
| grid.each do |v| | |
| if [ | |
| ['M', 'S', 'A', 'M', 'S'], | |
| ['S', 'M', 'A', 'S', 'M'], | |
| ['M', 'M', 'A', 'S', 'S'], | |
| ['S', 'S', 'A', 'M', 'M'], | |
| ].include?(grid.get_values([ | |
| v + V[-1, -1], | |
| v + V[1, -1], | |
| v, | |
| v + V[-1, 1], | |
| v + V[1, 1], | |
| ])) | |
| answer2 += 1 | |
| end | |
| end | |
| pp answer2 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment