Completed, got solution for reddit
parent
cf7a1a683b
commit
a4f4c2eace
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,7 @@
|
||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
|
@ -0,0 +1,40 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
# This is unfinished and broken. IDK why it doesn't work, but it's a bad solution.
|
||||||
|
# Check ./stolen.rb for a better solution that I stole from Reddit.
|
||||||
|
|
||||||
|
require 'pry'
|
||||||
|
# Part 1
|
||||||
|
input = File.readlines("./input").map(&:strip)
|
||||||
|
p1 = input.map{|line| line.scan(/\d/).values_at(0,-1).join}
|
||||||
|
solution = p1.map(&:to_i).sum
|
||||||
|
puts "Part 1 Solution is: #{solution}"
|
||||||
|
|
||||||
|
# Part 2
|
||||||
|
|
||||||
|
def transform(s)
|
||||||
|
digit_map = {one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9}
|
||||||
|
first_word = 0
|
||||||
|
until first_word.nil?
|
||||||
|
first_word = digit_map.map{ |word, digit| [ word.to_s,s.index(word.to_s)]}.to_h.reject{ |_k, val| val.nil? }.min_by{|_x, digit| digit}
|
||||||
|
s.sub!(first_word[0], digit_map[first_word[0].to_sym].to_s) unless first_word.nil?
|
||||||
|
end
|
||||||
|
s
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
p2 = input.map do |line|
|
||||||
|
transform(line).scan(/\d/).values_at(0,-1).join
|
||||||
|
# oline = line.dup
|
||||||
|
# new_line = transform(line)
|
||||||
|
# values = new_line.scan(/\d/).values_at(0,-1)
|
||||||
|
# final = values.join
|
||||||
|
# pp "#{oline}, #{new_line}, #{values}, #{final}"
|
||||||
|
# gets
|
||||||
|
# final
|
||||||
|
end
|
||||||
|
|
||||||
|
# binding.pry
|
||||||
|
|
||||||
|
solution = p2.map(&:to_i).sum
|
||||||
|
puts "Part 2 Solution is: #{solution}"
|
|
@ -0,0 +1,39 @@
|
||||||
|
@data = File.readlines("./input").map(&:strip)
|
||||||
|
|
||||||
|
NUM_DICT = {
|
||||||
|
"one" => "1",
|
||||||
|
"two" => "2",
|
||||||
|
"three" => "3",
|
||||||
|
"four" => "4",
|
||||||
|
"five" => "5",
|
||||||
|
"six" => "6",
|
||||||
|
"seven" => "7",
|
||||||
|
"eight" => "8",
|
||||||
|
"nine" => "9",
|
||||||
|
"1" => "1",
|
||||||
|
"2" => "2",
|
||||||
|
"3" => "3",
|
||||||
|
"4" => "4",
|
||||||
|
"5" => "5",
|
||||||
|
"6" => "6",
|
||||||
|
"7" => "7",
|
||||||
|
"8" => "8",
|
||||||
|
"9" => "9"
|
||||||
|
}
|
||||||
|
|
||||||
|
def part_1
|
||||||
|
calibrate(/\d/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def part_2
|
||||||
|
calibrate(/(?=(\d|one|two|three|four|five|six|seven|eight|nine))/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def calibrate(regex)
|
||||||
|
@data.each.map do |line|
|
||||||
|
digits = line.scan(regex)
|
||||||
|
(NUM_DICT[digits.first[0]] + NUM_DICT[digits.last[0]]).to_i
|
||||||
|
end.sum
|
||||||
|
end
|
||||||
|
|
||||||
|
puts part_1, part_2
|
Loading…
Reference in New Issue