blob: 5b6b0f70ea27ba6089b66a153cb65ede353c9ef4 (
plain)
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
|
require 'set'
def split_in_half(s)
pivot = s.length/2
return [s[0, pivot], s[pivot, s.length]]
end
def shared_item(s)
rucksack1, rucksack2 = split_in_half(s)
tmp = (rucksack1.chars.to_set & rucksack2.chars.to_set)
puts tmp
return tmp.first
end
def scores_hash
a = ('a'..'z').zip((1..26)) + ('A'..'Z').zip((27..52))
a.to_h
end
TEST_S = <<-EOC
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
EOC
def test_1(s)
s.lines.map do |line|
item = shared_item(line)
scores_hash[item]
end.sum
end
def day1
puts test_1(TEST_S)
File.open('./day3.txt') do |f|
retval = f.readlines.map do |line|
item = shared_item(line)
scores_hash[item]
end
puts retval.sum
end
end
require './utils'
def day2
badges = []
File.open('./day3.txt') do |f|
f.readlines.each_n(3) do |a,b,c|
badge = a.chars.to_set & b.chars.to_set & c.chars.to_set
badges << scores_hash[badge.first]
end
end
puts badges.sum
end
day2()
|