-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd14_part_two.rb
75 lines (62 loc) · 1.38 KB
/
d14_part_two.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
70
71
72
73
74
75
# frozen_string_literal: true
def transpose(input)
input.map(&:chars).transpose.map(&:join)
end
def tilt(input, asc = 1)
input.map do |row|
row
.split(/(#)/)
.map do |group|
group.chars.sort { |char| char == "." ? asc : -asc }
end
.join
end
end
def cycle(input)
# north
input = tilt(transpose(input))
# west
input = tilt(transpose(input))
# south - # transpose back and tilt it in the other direction
input = tilt(transpose(input), -1)
# east
tilt(transpose(input), -1)
end
def day14_part_two(input)
rows = input.length
grids = []
# Found the solution here https://www.youtube.com/watch?v=WCVOBKUNc38
# Count cycles and find the first duplicate
until grids.include?(input)
grids << input
input = cycle(input) # run a cycle
end
iterations = grids.length
first_dup = grids.index(input)
grid = grids[(1_000_000_000 - first_dup) % (iterations - first_dup) + first_dup]
# Calc load
grid
.each_with_index
.reduce(0) do |acc, (row, index)|
acc + row.count("O") * (rows - index)
end
end
test = <<~INPUT
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
INPUT
.split(/\n/)
pp day14_part_two(test)
# Large puzzle input
file = File.open("d14.txt")
file_data = file.readlines.map(&:chomp)
file.close
pp day14_part_two(file_data)