-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.rb
55 lines (48 loc) · 1.34 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
# frozen_string_literal: true
module AdventOfCode
module Puzzles2024
module Day01
##
# Class for solving Day 1 (2024) - Part 1 puzzle
class Part1
##
# @param file [String|nil] file with puzzle input
def initialize(file: nil)
file ||= "#{File.dirname(__FILE__)}/input.txt"
init_lists File.readlines(file, chomp: true)
end
##
# Compute the answer for the puzzle.
# The answer is the sum of all distances.
#
# @return [Integer] answer for the puzzle
def answer
distances.sum
end
##
# Compute distances based on list_a and list_b.
#
# @return [Array<Integer>] calibration values
def distances
@distances ||= list_a.sort.zip(list_b.sort).map { |a, b| (a - b).abs }
end
protected
##
# @return [Array<String>] list of values from the first list
attr_reader :list_a
##
# @return [Array<String>] list of values from the second list
attr_reader :list_b
def init_lists(file_contents)
@list_a = []
@list_b = []
file_contents.each do |line|
a, b = line.split.map(&:to_i)
@list_a << a
@list_b << b
end
end
end
end
end
end