Skip to content

Commit 2bb15a0

Browse files
committed
solve day 10 in Ruby
1 parent 5178754 commit 2bb15a0

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

day10/input.txt

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
addx 1
2+
addx 4
3+
addx 1
4+
noop
5+
addx 4
6+
noop
7+
noop
8+
noop
9+
noop
10+
addx 4
11+
addx 1
12+
addx 5
13+
noop
14+
noop
15+
addx 5
16+
addx -1
17+
addx 3
18+
addx 3
19+
addx 1
20+
noop
21+
noop
22+
addx 4
23+
addx 1
24+
noop
25+
addx -38
26+
addx 10
27+
noop
28+
noop
29+
noop
30+
noop
31+
noop
32+
addx 2
33+
addx 3
34+
addx -2
35+
addx 2
36+
addx 5
37+
addx 2
38+
addx -13
39+
addx 14
40+
addx 2
41+
noop
42+
noop
43+
addx -9
44+
addx 19
45+
addx -2
46+
addx 2
47+
addx -9
48+
addx -24
49+
addx 1
50+
addx 6
51+
noop
52+
noop
53+
addx -2
54+
addx 5
55+
noop
56+
noop
57+
addx -12
58+
addx 15
59+
noop
60+
addx 3
61+
addx 3
62+
addx 1
63+
addx 5
64+
noop
65+
noop
66+
noop
67+
noop
68+
addx -24
69+
addx 29
70+
addx 5
71+
noop
72+
noop
73+
addx -37
74+
noop
75+
addx 26
76+
noop
77+
noop
78+
addx -18
79+
addx 28
80+
addx -24
81+
addx 17
82+
addx -16
83+
addx 4
84+
noop
85+
addx 5
86+
addx -2
87+
addx 5
88+
addx 2
89+
addx -18
90+
addx 24
91+
noop
92+
addx -2
93+
addx 10
94+
addx -6
95+
addx -12
96+
addx -23
97+
noop
98+
addx 41
99+
addx -34
100+
addx 30
101+
addx -25
102+
noop
103+
addx 16
104+
addx -15
105+
addx 2
106+
addx -12
107+
addx 19
108+
addx 3
109+
noop
110+
addx 2
111+
addx -27
112+
addx 36
113+
addx -6
114+
noop
115+
noop
116+
addx 7
117+
addx -33
118+
addx -4
119+
noop
120+
addx 24
121+
noop
122+
addx -17
123+
addx 1
124+
noop
125+
addx 4
126+
addx 1
127+
addx 14
128+
addx -12
129+
addx -14
130+
addx 21
131+
noop
132+
noop
133+
noop
134+
addx 5
135+
addx -17
136+
addx 1
137+
addx 20
138+
addx 2
139+
noop
140+
addx 2
141+
noop
142+
noop
143+
noop
144+
noop
145+
noop

day10/solution.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require_relative '../lib/base.rb'
2+
3+
class Day10 < AdventOfCode
4+
def self.take_input(s)
5+
s.lines.map(&:split)
6+
end
7+
8+
part(1) do |input|
9+
x = 1
10+
cycle = 0
11+
total = 0
12+
stops = [20, 60, 100, 140, 180, 220]
13+
input.each do |a|
14+
if a[0] == "addx"
15+
cycle += 2
16+
if cycle >= stops[0]
17+
total += stops.shift * x
18+
break if stops.empty?
19+
end
20+
x += a[1].to_i
21+
else
22+
cycle += 1
23+
end
24+
25+
if cycle >= stops[0]
26+
total += stops.shift * x
27+
break if stops.empty?
28+
end
29+
end
30+
total
31+
end
32+
33+
part(2) do |input|
34+
x = 1
35+
width = 40
36+
height = 6
37+
screen = Array.new(height){' ' * width}
38+
cycle = 0
39+
i = 0
40+
waiting = false
41+
height.times do |r|
42+
width.times do |w|
43+
screen[r][w] = (x-1..x+1) === w ? '█' : '.'
44+
cycle += 1
45+
46+
if input[i][0] == "addx"
47+
if waiting
48+
waiting = false
49+
x += input[i][1].to_i
50+
i += 1
51+
else
52+
waiting = true
53+
end
54+
else
55+
i += 1
56+
end
57+
end
58+
end
59+
screen.join("\n")
60+
end
61+
end
62+
63+
Day10.run

0 commit comments

Comments
 (0)