Skip to content

Commit

Permalink
Day 7 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
hdf1996 committed Dec 8, 2021
1 parent b1320c6 commit c452521
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 2017/1/1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
input = File.read(File.join(__dir__, 'input.txt')).split('').map(&:to_i)

result = input.each_with_index.reduce([[], nil]) do |acc, (char, index)|
acc[0].push(char) if input[index +1 == input.length ? 0 : index + 1] == char
[acc[0], char]
end

puts result[0].sum
9 changes: 9 additions & 0 deletions 2017/1/2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
input = File.read(File.join(__dir__, 'input.txt')).split('').map(&:to_i)

result = input.each_with_index.reduce([[], nil]) do |acc, (char, index)|
i = (index + input.length / 2) % input.length
acc[0].push(char) if input[i] == char
[acc[0], char]
end

puts result[0].sum
1 change: 1 addition & 0 deletions 2017/1/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5255443714755555317777152441826784321918285999594221531636242944998363716119294845838579943562543247239969555791772392681567883449837982119239536325341263524415397123824358467891963762948723327774545715851542429832119179139914471523515332247317441719184556891362179267368325486642376685657759623876854958721636574219871249645773738597751429959437466876166273755524873351452951411628479352522367714269718514838933283861425982562854845471512652555633922878128558926123935941858532446378815929573452775348599693982834699757734714187831337546474515678577158721751921562145591166634279699299418269158557557996583881642468274618196335267342897498486869925262896125146867124596587989531495891646681528259624674792728146526849711139146268799436334618974547539561587581268886449291817335232859391493839167111246376493191985145848531829344198536568987996894226585837348372958959535969651573516542581144462536574953764413723147957237298324458181291167587791714172674717898567269547766636143732438694473231473258452166457194797819423528139157452148236943283374193561963393846385622218535952591588353565319432285579711881559343544515461962846879685879431767963975654347569385354482226341261768547328749947163864645168428953445396361398873536434931823635522467754782422557998262858297563862492652464526366171218276176258582444923497181776129436396397333976215976731542182878979389362297155819461685361676414725597335759976285597713332688275241271664658286868697167515329811831234324698345159949135474463624749624626518247831448143876183133814263977611564339865466321244399177464822649611969896344874381978986453566979762911155931362394192663943526834148596342268321563885255765614418141828934971927998994739769141789185165461976425151855846739959338649499379657223196885539386154935586794548365861759354865453211721551776997576289811595654171672259129335243531518228282393326395241242185795828261319215164262237957743232558971289145639852148197184265766291885259847236646615935963759631145338159257538114359781854685695429348428884248972177278361353814766653996675994784195827214295462389532422825696456457332417366426619555
10 changes: 10 additions & 0 deletions 2017/2/1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
input = File.read(File.join(__dir__, 'input.txt')).split("\n").map {|k| k.split(' ').map(&:to_i)}

result = input.map do |row|
min = row.min
max = row.max

max - min
end

pp result.sum
13 changes: 13 additions & 0 deletions 2017/2/2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
input = File.read(File.join(__dir__, 'input.txt')).split("\n").map {|k| k.split(' ').map(&:to_i)}

result = input.map do |row|
selected = nil
row.combination(2).reduce([]) do |acc, combo|
break selected = combo if combo.max % combo.min == 0
end
raise 'Impossible!' if selected.nil?

selected.max / selected.min
end

pp result.sum
16 changes: 16 additions & 0 deletions 2017/2/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1224 926 1380 688 845 109 118 88 1275 1306 91 796 102 1361 27 995
1928 2097 138 1824 198 117 1532 2000 1478 539 1982 125 1856 139 475 1338
848 202 1116 791 1114 236 183 186 150 1016 1258 84 952 1202 988 866
946 155 210 980 896 875 925 613 209 746 147 170 577 942 475 850
1500 322 43 95 74 210 1817 1631 1762 128 181 716 171 1740 145 1123
3074 827 117 2509 161 206 2739 253 2884 248 3307 2760 2239 1676 1137 3055
183 85 143 197 243 72 291 279 99 189 30 101 211 209 77 198
175 149 259 372 140 250 168 142 146 284 273 74 162 112 78 29
169 578 97 589 473 317 123 102 445 217 144 398 510 464 247 109
3291 216 185 1214 167 495 1859 194 1030 3456 2021 1622 3511 222 3534 1580
2066 2418 2324 93 1073 82 102 538 1552 962 91 836 1628 2154 2144 1378
149 963 1242 849 726 1158 164 1134 658 161 1148 336 826 1303 811 178
3421 1404 2360 2643 3186 3352 1112 171 168 177 146 1945 319 185 2927 2289
543 462 111 459 107 353 2006 116 2528 56 2436 1539 1770 125 2697 2432
1356 208 5013 4231 193 169 3152 2543 4430 4070 4031 145 4433 4187 4394 1754
5278 113 4427 569 5167 175 192 3903 155 1051 4121 5140 2328 203 5653 3233
33 changes: 33 additions & 0 deletions 2017/3/1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
input = File.read(File.join(__dir__, 'input.txt'))

target = 2
dimension = Math.sqrt(target).ceil

grid = Array.new(dimension) { Array.new(dimension, '.') }
middle = dimension / 2
x = middle
y = middle

dirs = [:right, :up, :left, :down]
dir = dirs[0]

def move(pos, dir)
case dir
when :right
[pos[0] + 1, pos[1]]
else
pos
end
end

(target - 1).times do |i|
grid[y][x] = i + 1

new_address = move([x, y], dir)
new_dir = dirs[(dirs.index(dir) + 1) % dirs.length]
dir = new_dir if grid[new_address[0]][new_address[1]].nil?

x, y = new_address
end

puts grid.map { |k|k.join('')}.join("\n")
1 change: 1 addition & 0 deletions 2017/3/2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
input = File.read(File.join(__dir__, 'input.txt'))
16 changes: 16 additions & 0 deletions 2017/3/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1224 926 1380 688 845 109 118 88 1275 1306 91 796 102 1361 27 995
1928 2097 138 1824 198 117 1532 2000 1478 539 1982 125 1856 139 475 1338
848 202 1116 791 1114 236 183 186 150 1016 1258 84 952 1202 988 866
946 155 210 980 896 875 925 613 209 746 147 170 577 942 475 850
1500 322 43 95 74 210 1817 1631 1762 128 181 716 171 1740 145 1123
3074 827 117 2509 161 206 2739 253 2884 248 3307 2760 2239 1676 1137 3055
183 85 143 197 243 72 291 279 99 189 30 101 211 209 77 198
175 149 259 372 140 250 168 142 146 284 273 74 162 112 78 29
169 578 97 589 473 317 123 102 445 217 144 398 510 464 247 109
3291 216 185 1214 167 495 1859 194 1030 3456 2021 1622 3511 222 3534 1580
2066 2418 2324 93 1073 82 102 538 1552 962 91 836 1628 2154 2144 1378
149 963 1242 849 726 1158 164 1134 658 161 1148 336 826 1303 811 178
3421 1404 2360 2643 3186 3352 1112 171 168 177 146 1945 319 185 2927 2289
543 462 111 459 107 353 2006 116 2528 56 2436 1539 1770 125 2697 2432
1356 208 5013 4231 193 169 3152 2543 4430 4070 4031 145 4433 4187 4394 1754
5278 113 4427 569 5167 175 192 3903 155 1051 4121 5140 2328 203 5653 3233
17 changes: 17 additions & 0 deletions 2021/7/1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
input = File.read(File.join(__dir__, 'input.txt')).split(',').map(&:to_i)
min = input.min
max = input.max

def simulate_movement(input, position)
input.reduce(0) do |acc, pos|
acc + (pos - position).abs
end
end

result = (min..max).inject(nil) do |acc, current|
simulation = simulate_movement(input, current)
acc = simulation if acc.nil? || simulation < acc
acc
end

puts result
20 changes: 20 additions & 0 deletions 2021/7/2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
input = File.read(File.join(__dir__, 'input.txt')).split(',').map(&:to_i)
min = input.min
max = input.max

def serie_sum(n)
(n * (n + 1)) / 2
end
def simulate_movement(input, position)
input.reduce(0) do |acc, pos|
acc + serie_sum((pos - position).abs)
end
end

result = (min..max).inject(nil) do |acc, current|
simulation = simulate_movement(input, current)
acc = simulation if acc.nil? || simulation < acc
acc
end

puts result
1 change: 1 addition & 0 deletions 2021/7/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,720,75,471,1142,1442,166,896,138,61,643,1331,315,365,43,4,289,577,1249,30,53,117,956,28,385,27,168,92,110,127,331,27,93,730,122,27,19,1563,708,849,190,1620,1135,878,74,60,424,500,10,7,1687,375,159,566,228,1069,1603,194,463,387,2,890,1060,600,27,578,66,91,510,383,827,1485,18,30,500,0,386,1088,1259,136,1013,660,748,1115,53,22,726,270,29,573,95,5,87,1467,95,1087,27,145,1264,12,621,549,1078,22,243,986,738,64,75,243,47,381,475,565,183,33,645,1,221,11,698,1415,77,269,1,61,67,219,1543,860,1352,338,292,42,2,962,97,282,132,1147,375,479,59,538,617,328,167,1414,365,185,364,553,407,807,226,1698,427,569,448,1549,957,192,906,101,21,31,224,715,890,992,1319,40,663,1094,1647,273,76,268,554,9,18,446,204,61,480,187,1260,131,272,1290,298,199,632,19,95,62,90,46,30,255,165,464,816,405,215,762,197,555,270,306,291,777,122,6,594,1073,231,3,198,790,902,747,908,202,33,56,412,615,437,60,978,418,1871,882,794,32,91,1620,119,16,327,1091,487,1934,1,114,880,32,709,213,127,92,360,30,897,975,75,651,818,87,626,397,927,90,128,129,141,112,1059,609,590,835,76,1707,791,310,328,1937,2,257,646,1150,823,181,5,31,925,321,75,75,167,12,571,340,62,434,107,619,255,923,357,537,507,1044,110,408,1015,616,711,28,322,435,556,27,3,932,1430,218,1227,429,209,735,106,1388,20,17,778,865,1578,847,150,268,1256,208,50,739,268,9,1242,1044,33,895,675,913,68,40,973,256,1314,531,101,709,291,176,211,757,535,31,9,618,121,770,868,1264,1829,68,318,697,51,64,89,27,240,975,586,27,149,142,547,25,168,393,81,1009,924,58,75,1007,675,73,286,598,212,686,431,274,75,1383,270,586,63,419,190,88,1005,222,169,403,440,385,474,697,1303,205,21,978,266,164,42,25,1687,150,235,422,1699,1323,63,260,1366,136,824,378,21,243,330,611,919,305,369,175,596,65,145,837,277,333,974,772,259,1445,19,20,73,253,9,77,329,466,584,216,39,406,299,446,91,1466,1981,307,755,168,245,236,73,103,149,754,106,195,95,147,84,378,230,588,27,953,876,1495,1794,1,15,176,895,1304,888,1174,444,4,978,14,614,1307,291,454,313,908,206,863,170,1360,85,720,605,38,1071,1137,833,356,1490,39,1531,142,109,93,392,12,1887,301,888,344,411,11,202,909,189,245,614,39,140,63,363,590,872,328,30,1627,1329,17,60,961,105,176,771,1119,23,1107,796,446,617,215,298,203,844,0,369,694,125,413,1412,167,95,858,1190,53,1238,83,126,482,2,93,1838,109,342,15,89,280,187,73,129,23,487,555,168,502,175,43,228,109,1170,1208,296,104,243,99,344,107,917,506,1434,1013,566,615,351,18,265,1152,585,1802,347,39,274,27,825,170,409,10,1159,43,10,1139,53,46,283,706,152,1804,863,797,31,141,351,131,242,1014,1617,228,258,739,336,665,409,1398,0,77,168,1376,464,135,1122,219,13,33,315,174,387,186,814,374,7,712,174,10,752,74,660,139,844,478,318,1866,26,307,82,87,1036,696,411,109,6,423,651,7,410,95,1600,134,203,273,37,81,151,320,1043,59,1074,1166,741,1040,155,534,972,1157,167,27,688,63,94,168,808,265,1791,328,781,403,1177,818,422,0,101,1483,67,1098,1679,148,546,1224,117,188,376,886,226,3,132,114,44,479,29,16,43,472,175,335,77,17,1252,468,390,71,111,728,1167,822,57,30,620,81,945,379,362,731,22,709,1428,1588,681,30,930,48,64,819,21,1179,907,1969,537,98,545,1303,217,1234,713,39,929,37,50,949,165,164,702,1706,1133,935,438,298,565,661,1133,762,1501,179,74,793,323,20,260,1396,204,421,654,699,64,1080,119,171,1217,652,11,13,298,302,1569,841,142,428,1535,159,538,381,1,721,59,12,297,421,126,9,91,1549,361,941,159,273,132,51,576,1247,754,18,105,355,500,121,714,264,363,27,515,109,359,57,1237,85,541,407,71,150,60,1089,125,462,214,76,21,1,865,605,1054,36,1332,687,1023,1012,961,298,537,1519,1644,971,93,596,289,355,526,359,348,706,232,133,523,95,723,489,1061,646,769,158,150,699,759,355,97,447,737,140,98,12,235,238,1175,705,65,158,204,164,109,1150

0 comments on commit c452521

Please sign in to comment.