-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.rb
69 lines (61 loc) · 1.54 KB
/
part1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
require_relative "hand"
module AdventOfCode
module Puzzles2023
module Day07
##
# Class for solving Day 7 - Part 1 puzzle
class Part1
##
# @return [Array<Hand>] hands to play with
attr_reader :hands
##
# @param file [String] file name of the input file
def initialize(file: nil)
file ||= "#{File.dirname(__FILE__)}/input.txt"
parse_file(file)
end
##
# Compute the answer for the puzzle.
#
# @return [Integer] answer
def answer
total = 0
sort_hands.each_with_index do |hand, index|
total += hand.bid * (index + 1)
end
total
end
##
# Sort the hands.
#
# @return [Array<Hand>] sorted hands
def sort_hands
@hands.sort
end
protected
##
# Parse the input file.
#
# @param file [String] file name of the input file
def parse_file(file)
file_contents = File.readlines(file, chomp: true)
@hands = file_contents.map do |line|
cards, bid = line.split
build_hand(cards, bid.to_i)
end
end
##
# Build a hand.
#
# @param cards [String] cards in the hand
# @param bid [Integer] bid for the hand
#
# @return [Hand] hand
def build_hand(cards, bid)
Hand.new(cards:, bid:)
end
end
end
end
end