Skip to content

Commit a4487f5

Browse files
committed
day 05 ugly
1 parent 37653e4 commit a4487f5

File tree

9 files changed

+363
-34
lines changed

9 files changed

+363
-34
lines changed

2019/day02/main.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ def value(memory, cursor)
5656
end
5757
puts '==================================='
5858

59-
6059
## Tests
61-
6260
class Day2 < Test::Unit::TestCase
6361
def test_plus
6462
assert_equal [2,0,0,0,99], play_step([1,0,0,0,99])

2021/day01/main.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
arr = File.readlines('data.txt')
35
arr.map!(&:to_i)
4-
compare = Proc.new { |arr| arr.first < arr.last }
6+
compare = proc { |a| a.first < a.last }
57

68
puts arr.each_cons(2).select(&compare).count
7-
puts arr.each_cons(4).select(&compare).count
9+
puts arr.each_cons(4).select(&compare).count

2021/day02/main.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
aim += x if cmd == 'down'
2121
aim -= x if cmd == 'up'
2222
if cmd == 'forward'
23-
position += x
23+
position += x
2424
depth += x * aim
2525
end
2626
end

2022/day09/main.rb

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env ruby
2-
arr = File.readlines("data.txt")
2+
arr = File.readlines('data.txt')
33

44
class Grid
55
def initialize(num = 2, debug)
66
@num = num
77
@debug = debug
8-
@grid_min_x, @grid_max_x, @grid_min_y, @grid_max_y = 0, 0, 0, 0
8+
@grid_min_x = 0
9+
@grid_max_x = 0
10+
@grid_min_y = 0
11+
@grid_max_y = 0
912
@knots_x = Array.new(num, 0)
1013
@knots_y = Array.new(num, 0)
1114

@@ -26,13 +29,13 @@ def move_to(x, y)
2629
def action(direction, distance)
2730
distance.times do
2831
case direction
29-
when "R"
32+
when 'R'
3033
move_to(@knots_x[0] + 1, @knots_y[0])
31-
when "L"
34+
when 'L'
3235
move_to(@knots_x[0] - 1, @knots_y[0])
33-
when "U"
36+
when 'U'
3437
move_to(@knots_x[0], @knots_y[0] - 1)
35-
when "D"
38+
when 'D'
3639
move_to(@knots_x[0], @knots_y[0] + 1)
3740
end
3841
end
@@ -53,14 +56,14 @@ def abs_y(num)
5356

5457
def move_tails
5558
@num.times do |i|
56-
if i != 0 && need_move_tail?(i)
57-
if @knots_x[i - 1] != @knots_x[i]
58-
@knots_x[i] = @knots_x[i] + (@knots_x[i - 1] > @knots_x[i] ? 1 : -1)
59-
end
60-
61-
if @knots_y[i - 1] != @knots_y[i]
62-
@knots_y[i] = @knots_y[i] + (@knots_y[i - 1] > @knots_y[i] ? 1 : -1)
63-
end
59+
next unless i != 0 && need_move_tail?(i)
60+
61+
if @knots_x[i - 1] != @knots_x[i]
62+
@knots_x[i] = @knots_x[i] + (@knots_x[i - 1] > @knots_x[i] ? 1 : -1)
63+
end
64+
65+
if @knots_y[i - 1] != @knots_y[i]
66+
@knots_y[i] = @knots_y[i] + (@knots_y[i - 1] > @knots_y[i] ? 1 : -1)
6467
end
6568
end
6669
mark_visited(@knots_x[@num - 1], @knots_y[@num - 1])
@@ -72,20 +75,21 @@ def mark_visited(x, y)
7275

7376
def print_visited(full = false)
7477
return unless @debug
75-
puts ""
78+
79+
puts ''
7680
(@grid_min_y..@grid_max_y).each do |y|
77-
puts ((@grid_min_x..@grid_max_x).map do |x|
78-
if !full
79-
knot = @visited["#{x},#{y}"] ? "#" : "." if knot.nil?
80-
@num.times do |i|
81-
if @knots_x[i] == x && @knots_y[i] == y
82-
knot = i.to_s
83-
break
84-
end
81+
puts((@grid_min_x..@grid_max_x).map do |x|
82+
next if full
83+
84+
knot = @visited["#{x},#{y}"] ? '#' : '.' if knot.nil?
85+
@num.times do |i|
86+
if @knots_x[i] == x && @knots_y[i] == y
87+
knot = i.to_s
88+
break
8589
end
86-
knot
8790
end
88-
end.join(""))
91+
knot
92+
end.join(''))
8993
end
9094
end
9195

@@ -96,7 +100,7 @@ def visited
96100
def self.run(arr, num, debug = false)
97101
grid = Grid.new(num, debug)
98102
arr.each do |line|
99-
direction, distance = line.split(" ")
103+
direction, distance = line.split(' ')
100104
distance = distance.to_i
101105
grid.action(direction, distance)
102106
end

2023/day04/main.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
e[0].split.map(&:to_i).intersection(e[1].split.map(&:to_i)).count
99
end
1010

11-
# star1
12-
card_score.filter(&:positive?).map { |e| 2.pow(e - 1) }.sum
11+
# stars1
12+
puts card_score.filter(&:positive?).map { |e| 2.pow(e - 1) }.sum
1313

14+
# stars2
1415
copies = {}
1516
card_score.map.with_index do |e, index|
1617
copies[index] ||= 1
@@ -20,5 +21,4 @@
2021
end
2122
end
2223

23-
# star2
2424
puts copies.values.sum

2023/day05/.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.1.0

2023/day05/data_1.txt

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
seeds: 2019933646 2719986 2982244904 337763798 445440 255553492 1676917594 196488200 3863266382 36104375 1385433279 178385087 2169075746 171590090 572674563 5944769 835041333 194256900 664827176 42427020
2+
3+
seed-to-soil map:
4+
3566547172 3725495029 569472267
5+
2346761246 1249510998 267846697
6+
1812605508 937956667 271194541
7+
1421378697 1209151208 40359790
8+
2083800049 2788751092 262961197
9+
2938601691 473979048 463977619
10+
473979048 1517357695 947399649
11+
4136019439 3566547172 158947857
12+
1461738487 3051712289 350867021
13+
2614607943 2464757344 323993748
14+
15+
soil-to-fertilizer map:
16+
3107230831 2583931429 576709409
17+
970181981 608291332 1441137369
18+
743954495 3859046283 158951815
19+
3683940240 3227916509 91282070
20+
608291332 2448268266 135663163
21+
3775222310 2049428701 398839565
22+
2411319350 3319198579 539847704
23+
2951167054 4017998098 156063777
24+
902906310 3160640838 67275671
25+
26+
fertilizer-to-water map:
27+
1257642402 395703749 69589612
28+
1800674 2215701547 90550534
29+
2757853693 358464863 37238886
30+
3285451399 181079109 43937782
31+
2346544130 3513448371 192150886
32+
3866348216 4231433060 63534236
33+
1327232014 1560332334 90281838
34+
2538695016 616206288 114467702
35+
255018176 225016891 46372244
36+
1171065990 3705599257 27021880
37+
1070753744 730673990 442780
38+
221369008 3479799203 33649168
39+
2987721226 271389135 80072982
40+
1198087870 732917444 24556356
41+
199036270 2306252081 22332738
42+
0 731116770 1800674
43+
3929882452 3989920675 212758268
44+
631506549 757473800 322942578
45+
301390420 0 157952443
46+
2795092579 157952443 1997721
47+
1222644226 2619085211 34998176
48+
954449127 499901671 116304617
49+
1429766246 159950164 21128945
50+
2205492221 2074649638 141051909
51+
2749302577 3732621137 8551116
52+
459342863 1219863414 57737723
53+
3329389181 3741172253 59047790
54+
2797090300 2328584819 190630926
55+
3278448653 351462117 7002746
56+
126959518 1277601137 72076752
57+
92351208 465293361 34608310
58+
4142640720 3866348216 123572459
59+
2143980560 1080416378 43307177
60+
1450895191 2672287871 693085369
61+
517080586 3365373240 114425963
62+
3388436971 1662866566 411783072
63+
2187287737 2654083387 18204484
64+
1417513852 1650614172 12252394
65+
3067794208 1349677889 210654445
66+
4266213179 4202678943 28754117
67+
2653162718 1123723555 96139859
68+
1071196524 2519215745 99869466
69+
70+
water-to-light map:
71+
512627839 90187036 1196629
72+
3379634653 2059506154 33434334
73+
3286651054 4276482087 18485209
74+
4233695090 28914830 61272206
75+
3413068987 3322576776 23288997
76+
3736304424 3345865773 43267308
77+
1246285471 2994853001 251748584
78+
3779571732 1946298040 113208114
79+
390808412 3287769466 34807310
80+
1881283842 2879009693 106527924
81+
3964031050 2506138169 12994476
82+
3436357984 793897944 162691614
83+
2255160753 2092940488 151061610
84+
853985057 3506201042 119010035
85+
301385394 1856875022 89423018
86+
972995092 658665705 34308693
87+
4159948022 1315925500 65322692
88+
640912738 250463411 213072319
89+
1761800914 91383665 102591221
90+
450345319 3246601585 5793995
91+
3186220306 4173678310 91115364
92+
28914830 3633635453 176360375
93+
456139314 193974886 56488525
94+
2523290324 3809995828 187303152
95+
2406222363 3389133081 117067961
96+
205275205 2782899504 96110189
97+
2135785589 1100569535 119375164
98+
1121466033 533846267 124819438
99+
1007303785 2244002098 114162248
100+
3599049598 3997298980 137254826
101+
4077949072 463535730 70310537
102+
4225270714 3625211077 8424376
103+
1498034055 2519132645 263766859
104+
2710593476 1381248192 475626830
105+
3977025526 692974398 100923546
106+
4148259609 4264793674 11688413
107+
1987811766 2358164346 147973823
108+
3892779846 1244674296 71251204
109+
3340510149 4134553806 39124504
110+
1864392135 956589558 16891707
111+
425615722 1219944699 24729597
112+
513824468 973481265 127088270
113+
3277335670 2985537617 9315384
114+
3305136263 3252395580 35373886
115+
116+
light-to-temperature map:
117+
1094191559 698410082 28110394
118+
383870732 1189042355 107231661
119+
3711052230 2164474756 34756304
120+
745558539 170241759 7170863
121+
491102393 503970250 194439832
122+
4034618875 3142749029 146609939
123+
3781998432 1718948669 129329785
124+
2440091414 3071819711 70929318
125+
1301358031 55123603 115118156
126+
0 2789116652 87933685
127+
770729148 177412622 48955790
128+
3772681560 3886204605 9316872
129+
752729402 37123857 17999746
130+
3745808534 2137385460 7147939
131+
2028807236 3677936618 208267987
132+
2237075223 3289358968 92979022
133+
88764920 1960439220 176946240
134+
3568470355 2258695303 142581875
135+
3276170082 1848278454 112160766
136+
2637902204 1129503077 39814191
137+
3000547589 892603630 188042422
138+
2511020732 226368412 126881472
139+
1122301953 1296274016 52818372
140+
1440958847 1353023078 243104929
141+
2963423732 0 37123857
142+
3388330848 2199231060 48304954
143+
1175120325 377732544 126237706
144+
819684938 1349092388 3930690
145+
3752956473 1169317268 19725087
146+
3911328217 2144533399 19941357
147+
1416476187 353249884 24482660
148+
2677716395 3895521477 285707337
149+
265711160 2413138935 118159572
150+
685542225 1080646052 48857025
151+
3556608598 2401277178 11861757
152+
734399250 2247536014 11159289
153+
87933685 3677105383 831235
154+
3188590011 1596128007 87580071
155+
836373414 2531298507 257818145
156+
3471876393 2877050337 84732205
157+
1684063776 726520476 166083154
158+
823615628 3560998296 12757786
159+
3436635802 1683708078 35240591
160+
3931269574 3573756082 103349301
161+
1850146930 3382337990 178660306
162+
2330054245 2961782542 110037169
163+
164+
temperature-to-humidity map:
165+
1773059646 4122818507 172148789
166+
2417158855 2859734866 110076859
167+
977168274 1576624124 28149321
168+
4275291678 3797606290 19675618
169+
1141296808 749646180 267286171
170+
3592756112 2969811725 273274339
171+
0 19621130 7167651
172+
2059084943 2697725300 48133058
173+
2107218001 3920609496 145140777
174+
1453481278 1152911564 151292167
175+
1408582979 1465584228 44898299
176+
7167651 0 19621130
177+
2907567891 1829621431 240604380
178+
2252358778 3652347291 145258999
179+
1005317595 1016932351 135979213
180+
1945208435 2745858358 113876508
181+
2397617777 4065750273 16506015
182+
3251499859 1776094709 53526722
183+
2867005672 4082256288 40562219
184+
26788781 1304203731 161380497
185+
3305026581 2409995769 287729531
186+
3866030451 3243086064 409261227
187+
2414123792 1773059646 3035063
188+
911026677 1510482527 66141597
189+
3148172271 3817281908 103327588
190+
2527235714 2070225811 339769958
191+
188169278 26788781 722857399
192+
193+
humidity-to-location map:
194+
3907319746 3137303541 31421983
195+
3085093695 1018495475 286155292
196+
2898003508 2491485887 87665522
197+
2546787368 2901838353 7997221
198+
3835317650 2829836257 72002096
199+
2554784589 3509894030 133012322
200+
3487595595 3719561871 104747874
201+
3714670750 2667334372 120646900
202+
975094571 2909835574 227467967
203+
2985669030 3864000834 99424665
204+
3672962118 2449777255 41708632
205+
3631107133 2787981272 41854985
206+
3938741729 3963425499 15057061
207+
3447904506 3824309745 39691089
208+
1824175159 1304650767 641793976
209+
242892183 0 6504921
210+
3371248987 3642906352 76655519
211+
1698833898 2258930589 81940357
212+
0 6504921 242892183
213+
2465969135 3978482560 80818233
214+
3592343469 4256203632 38763664
215+
3953798790 3168725524 341168506
216+
2775979874 4134179998 122023634
217+
1780774255 975094571 43400904
218+
1311468847 1946444743 312485846
219+
2687796911 2579151409 88182963
220+
1202562538 2340870946 108906309
221+
1623954693 4059300793 74879205

0 commit comments

Comments
 (0)