-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.rb
46 lines (36 loc) · 863 Bytes
/
day02.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
# frozen_string_literal: true
require_relative "../task"
class Day02
include Task
def part_one
action_map = {
forward: [1, 0],
down: [0, 1],
up: [0, -1]
}.freeze
xy = [0, 0]
read_input.lines.map(&:split).each do |command_data|
action = action_map[command_data[0].to_sym]
num = command_data[1].to_i
xy[0] += (action[0] * num)
xy[1] += (action[1] * num)
end
xy.reduce(&:*).to_s
end
def part_two
aim = 0
xy = [0, 0]
read_input.lines.map(&:split).map { |it| [it[0], it[1].to_i] }.each do |command_data|
case command_data[0]
when "down"
aim += command_data[1]
when "up"
aim -= command_data[1]
when "forward"
xy[0] += command_data[1]
xy[1] += aim * command_data[1]
end
end
xy.reduce(&:*).to_s
end
end