Skip to content

Commit 357a3cc

Browse files
committed
Start adding days to github
0 parents  commit 357a3cc

File tree

6 files changed

+219
-0
lines changed

6 files changed

+219
-0
lines changed

advent1.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def advent
2+
file = File.open("input.txt")
3+
contents = file.read
4+
elf_hash = Hash.new(0)
5+
elf_counter = 0
6+
contents.split("\n").each do |row|
7+
if row.empty?
8+
elf_counter += 1
9+
else
10+
elf_hash[elf_counter] += row.to_i
11+
end
12+
end
13+
14+
elf_hash
15+
end

advent2.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
def advent(guide)
2+
guide.map{|i| score(i) }
3+
end
4+
5+
def score(match)
6+
win_or_lose(match)
7+
rescue => e
8+
puts e.inspect
9+
end
10+
11+
def shape(match)
12+
puts match
13+
case match[1]
14+
when 'X'
15+
1
16+
when 'Y'
17+
2
18+
when 'Z'
19+
3
20+
end
21+
end
22+
23+
def win_or_lose(match)
24+
case match[0]
25+
when 'A'
26+
case match[1]
27+
when 'X'
28+
3
29+
when 'Y'
30+
4
31+
when 'Z'
32+
8
33+
end
34+
when 'B'
35+
case match[1]
36+
when 'X'
37+
1
38+
when 'Y'
39+
5
40+
when 'Z'
41+
9
42+
end
43+
when 'C'
44+
case match[1]
45+
when 'X'
46+
2
47+
when 'Y'
48+
6
49+
when 'Z'
50+
7
51+
end
52+
end
53+
end

advent3.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
def score_of_all_contents(all_contents)
2+
results = all_contents.map do |row|
3+
item = item_in_both(row)
4+
score_of_item(item)
5+
end
6+
7+
results.sum
8+
end
9+
10+
def by_threes(all_contents)
11+
total_score = 0
12+
13+
all_contents.each_slice(3) do |slice|
14+
slice_score = score_of_item(items_in_three(slice))
15+
puts slice_score
16+
total_score += slice_score
17+
end
18+
19+
total_score
20+
end
21+
22+
def scores
23+
return @scores if @scores
24+
25+
@scores = {}
26+
score = 1
27+
('a'..'z').to_a.each do |c|
28+
@scores[c] = score
29+
score += 1
30+
end
31+
('A'..'Z').to_a.each do |c|
32+
@scores[c] = score
33+
score += 1
34+
end
35+
end
36+
37+
def score_of_item(item)
38+
scores[item]
39+
end
40+
41+
def item_in_both(contents)
42+
count = contents.length / 2
43+
first_compartment = contents.split('')[0..count-1]
44+
second_compartment = contents.split('')[count..-1]
45+
46+
(first_compartment & second_compartment).first
47+
end
48+
49+
def items_in_three(array)
50+
contents = array.map{|i| i.split('') }
51+
52+
(contents[0] & contents[1] & contents[2]).first
53+
end

advent4.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def overlap_count(data)
2+
data.select{|i| overlap(i) }.count
3+
end
4+
5+
def parse_row(row)
6+
row.split(",").map { |i| i.split('-') }.map { |i| i[0].to_i..i[1].to_i }
7+
end
8+
9+
def overlap(row)
10+
row = parse_row(row)
11+
12+
one = row.first.to_a
13+
two = row.last.to_a
14+
15+
(one & two).any?
16+
end
17+
18+
def completely_overlap(row)
19+
row = parse_row(row)
20+
21+
one = row.first.to_a
22+
two = row.last.to_a
23+
24+
one.map { |i| two.include? i }.all? || two.map { |i| one.include? i }.all?
25+
end

advent5.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
def parse_crates(input)
2+
Matrix(input).transpose.to_a
3+
end
4+
5+
def new_crates
6+
{
7+
"1"=>["[N]", "[B]", "[D]", "[T]", "[V]", "[G]", "[Z]", "[J]"],
8+
"2"=>["[S]", "[R]", "[M]", "[D]", "[W]", "[P]", "[F]"],
9+
"3"=>["[V]", "[C]", "[R]", "[S]", "[Z]"],
10+
"4"=>["[R]", "[T]", "[J]", "[Z]", "[P]", "[H]", "[G]"],
11+
"5"=>["[T]", "[C]", "[J]", "[N]", "[D]", "[Z]", "[Q]", "[F]"],
12+
"6"=>["[N]", "[V]", "[P]", "[W]", "[G]", "[S]", "[F]", "[M]"],
13+
"7"=>["[G]", "[C]", "[V]", "[B]", "[P]", "[Q]"],
14+
"8"=>["[Z]", "[B]", "[P]", "[N]"],
15+
"9"=>["[J]", "[P]", "[J]"]
16+
}
17+
end
18+
19+
def follow_instruction(instruction)
20+
puts instruction
21+
puts @crates.inspect
22+
split = instruction.split(' ')
23+
count = split[1].to_i
24+
from_spot = split[3]
25+
to_spot = split[5]
26+
27+
count.times do
28+
puts from_spot.inspect
29+
crate = @crates[from_spot].pop
30+
@crates[to_spot] << crate if crate
31+
end
32+
end
33+
34+
def follow_instruction_plus(instruction)
35+
puts instruction
36+
puts @crates.inspect
37+
split = instruction.split(' ')
38+
count = split[1].to_i
39+
from_spot = split[3]
40+
to_spot = split[5]
41+
42+
movers = []
43+
count.times do
44+
movers << @crates[from_spot].pop
45+
end
46+
47+
movers.compact!
48+
49+
@crates[to_spot] += movers.reverse
50+
end

advent6.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def marker_finder(string)
2+
holding = []
3+
counter = 0
4+
answer = nil
5+
array = string.split('')
6+
7+
array.each do |c|
8+
holding << c
9+
counter += 1
10+
11+
if holding.length == 14
12+
uniq_length = holding.uniq.length
13+
puts "#{counter}: #{uniq_length} : #{holding.join('')}"
14+
if uniq_length == 14
15+
answer ||= counter
16+
else
17+
holding.shift
18+
end
19+
end
20+
end
21+
22+
answer
23+
end

0 commit comments

Comments
 (0)