Solutions day1-day3

mark
Mark Bebawy 2025-12-14 14:27:16 +01:00
parent 78a2917ea3
commit 2b400c1d7a
13 changed files with 5208 additions and 0 deletions

11
mark/day1/example.txt Normal file
View File

@ -0,0 +1,11 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

4571
mark/day1/input.txt Normal file

File diff suppressed because it is too large Load Diff

29
mark/day1/part1.rb Normal file
View File

@ -0,0 +1,29 @@
class RotationDecoder
def initialize(file_path)
@file_path = file_path
@mapping = {
'L' => :-,
'R' => :+
}
end
def decode(debug: false)
code = 0
current_num = 50
puts "The dial starts by pointing at #{current_num}" if debug
File.readlines(@file_path, chomp: true).map do |line|
next if line.nil? || line.empty?
direction, *number_arr = line.split('')
number = Integer(number_arr.join)
current_num = current_num.send(@mapping[direction], number) % 100
code += 1 if current_num == 0
puts "The dial is rotaded #{line} to point at #{current_num}" if debug
end
code
end
end
rd = RotationDecoder.new('input.txt')
puts "The code is: #{rd.decode}"

53
mark/day1/part2.rb Normal file
View File

@ -0,0 +1,53 @@
require 'debug'
class RotationDecoder
def initialize(file_path)
@file_path = file_path
@mapping = {
'L' => :-,
'R' => :+
}
end
def decode(debug: false)
code = 0
current_num = 50
puts "The dial starts by pointing at #{current_num}" if debug
File.readlines(@file_path, chomp: true).map do |line|
next if line.nil? || line.empty?
direction, *number_arr = line.split('')
number = Integer(number_arr.join)
full_rotations = number / 100
rest = number - full_rotations * 100
raw_num = current_num.send(@mapping[direction], rest)
# include 100 since that does not pass *through* zero but becomes exactly zero.
passes_zero = 0
if current_num != 0
passes_zero = (0 <= raw_num && raw_num <= 100) ? 0 : 1
end
current_num = raw_num % 100
print "The dial is rotaded #{line} to point at #{current_num}" if debug
print "; during this rotation, it points at 0 #{full_rotations + passes_zero} times" if debug && full_rotations + passes_zero > 0
print "\n" if debug
code += full_rotations + passes_zero
code += 1 if current_num == 0
end
code
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby part2.rb <file_name> [debug]"
exit 1
end
debug = (ARGV[1] == "debug")
rd = RotationDecoder.new(ARGV[0])
puts "The code is: #{rd.decode(debug:)}"

1
mark/day2/example.txt Normal file
View File

@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

1
mark/day2/input.txt Normal file
View File

@ -0,0 +1 @@
1090286-1131879,3259566-3404881,138124-175118,266204727-266361099,16765-24272,7657360692-7657593676,88857504-88926597,6869078-6903096,48444999-48532270,61427792-61580535,71-103,8077-10421,1920-2560,2-17,951-1259,34-50,28994-36978,1309-1822,9393918461-9393960770,89479-120899,834641-988077,5389718924-5389797353,34010076-34214499,5063-7100,607034-753348,19098586-19261191,125085556-125188689,39839-51927,3246-5037,174-260,439715-473176,187287-262190,348-535,58956-78301,4388160-4505757,512092-584994,13388753-13534387

57
mark/day2/part1.rb Normal file
View File

@ -0,0 +1,57 @@
require 'debug'
class ValidIdFilter
def initialize(file_path)
@file_path = file_path
end
def sum_invalid_ids(debug: false)
ranges = parse_input(File.read(@file_path))
invalid_ids = ranges.flat_map {|range| get_invalid_ids_from_range(range)}
invalid_ids.sum
end
private
# @param [Range] e.g., 11-22
# @return [<Integer>] array of invalid ids in range
def get_invalid_ids_from_range(range)
range.select {|element| invalid_id?(element)}
end
def invalid_id?(integer)
# ID is invalid if it is made only of some sequence of digits repeated twice
string = integer.to_s
num_digits = string.length
# some sequence repeated twice => number of digits must be even
return false if num_digits % 2 != 0
half_length = num_digits / 2
string[0...half_length] == string[half_length..-1]
end
# @param [String] full input e.g., "11-22,33-604,..."
# @return [<Range>] e.g., [11..22, 33..604, ...]
def parse_input(input)
input.split(',').map {|range| to_range(range)}
end
# @param [String] e.g., "11-22"
# @return [Range] e.g., 11..22 (inclusive)
def to_range(string)
boundaries = string.split('-').map(&:to_i)
raise "Invalid boundaries for #{string}, got #{boundaries.inspect}" if boundaries.length != 2
Range.new(boundaries[0], boundaries[1])
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby part1.rb <file_name> [debug]"
exit 1
end
debug = (ARGV[1] == "debug")
filter = ValidIdFilter.new(ARGV[0])
puts "The answer is: #{filter.sum_invalid_ids(debug:)}"

74
mark/day2/part2.rb Normal file
View File

@ -0,0 +1,74 @@
require 'debug'
require 'prime'
class ValidIdFilter
def initialize(file_path, debug: false)
@file_path = file_path
@debug = debug
end
def sum_invalid_ids
ranges = parse_input(File.read(@file_path))
invalid_ids = ranges.flat_map {|range| get_invalid_ids_from_range(range)}
puts invalid_ids.inspect if @debug
invalid_ids.sum
end
private
# @param [Range] e.g., 11-22
# @return [<Integer>] array of invalid ids in range
def get_invalid_ids_from_range(range)
range.select do |element|
invalid_id?(element)
end
end
def invalid_id?(integer)
# ID is invalid if it is made only of some sequence of digits repeated AT LEAST twice
string = integer.to_s
num_digits = string.length
puts "is #{string} an invalid ID?" if @debug
(1...num_digits).each do |n|
next if num_digits % n != 0
slices = string.chars.each_slice(n).map(&:join)
puts "...trying divisior #{n}: #{slices} ..." if @debug
return true if slices.uniq.length == 1 && slices.length > 1
end
false
end
# @param [String] full input e.g., "11-22,33-604,..."
# @return [<Range>] e.g., [11..22, 33..604, ...]
def parse_input(input)
input.split(',').map {|range| to_range(range)}
end
# @param [String] e.g., "11-22"
# @return [Range] e.g., 11..22 (inclusive)
def to_range(string)
boundaries = string.split('-').map(&:to_i)
raise "Invalid boundaries for #{string}, got #{boundaries.inspect}" if boundaries.length != 2
Range.new(boundaries[0], boundaries[1])
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby part2.rb <file_name> [debug]"
exit 1
end
debug = (ARGV[1] == "debug")
filter = ValidIdFilter.new(ARGV[0], debug:)
puts "The answer is: #{filter.sum_invalid_ids}"
# The answer is: 41662374059
# real 0m24,122s
# user 0m24,049s
# sys 0m0,070s

4
mark/day3/example.txt Normal file
View File

@ -0,0 +1,4 @@
987654321111111
811111111111119
234234234234278
818181911112111

200
mark/day3/input.txt Normal file
View File

@ -0,0 +1,200 @@
4426546555433545424424345444644242452452532444564422646557424354153538454225332755435544545533324152
4325282447422434333445212333343451413231333423353426332541123254243232342322553322244341312543932333
4332333442334543644333542384256325234452445449454359452442434444344242333247233254353342732444654333
3242342268563381365322464432323462722532321262324224633345426519263464226222933213111434342164622216
9447673765733475853338583623277776759662892975737572674219274588774764767584838732636637987466747925
5465666747656245473486677773645858655623848436585748584726454883586526877665853454785577677638954758
2444454545573358335454444444533657461424744633575454555414644566436546558449448833539855835444255524
6157461556353423546422234143542323532463543463631966444536448423543534355464334335525162636648321353
2835376666759365533343684448534555132335383873465765564643686464153633459349536367566364363345632544
7344353747645474324795566237335767744553352745445443443425543365567665332834442736643744656373346534
3432324344242432243242442431213424312243322133232222316433433435212164332223444243334342533334253482
2622221422722224222132452327221224222224622222233223222324242132332222422263234452222242224221222352
2333333333332434452126434233262133133233322433432323143323224312238323253333223423537495436441425233
4223121252222122212232222432222222321212224221232222222232212222212222132111222212222222222251232212
5656364443456476766564447464434634344344454564655366674845343634444454533645485474555461444675445464
2222222121422212221222211522213322122342313222422223311222223122112232223422311251182122213322222322
2333223312323513354132532323242345322322263522221232522335232221232532332552333214121322322432333326
7375355567864373516855538565346166466365834565633643466338563523647238744789722453348483369683756368
3433535626365433336245424263342632336633352551353243934354326534345544334562636336633544635333676646
4552543245436445453245462344463365545345654265453546534544522571554353554535445555264545655555435564
3462831622494255764853548733574574745524782433922944636735655223248563544574263724458232637544263524
3222443423222532333422343715535222233233323223229323147323212322223213235433123142326522263133222232
2333212333432333323325252331233333222732313232324333223824122338323314233632223363333234252333334334
2331142222264423421223242247254264221412241455233167423223334223352322243322332242134435224322124222
3221232222123222222425122224223222211121332122111521114212121232122222222222222122314421232222252312
4434524632643643753662622454563441256616464333645346343655435445363437355666544625446446124156665532
5763246121842223223525634383114633483812622132447222352264521354343613323122493132222226422223222227
4323333144422413453633413411233333333524243423323613442334134334272326443343531222133522232333343333
3234234214223423333422221324424143222312253223324541242142332343441224324344332443233314464323322144
6423483553431355233336233333334345336244367322543433123333331822542553361323313533255325347255353232
3132322222322223233332424222432332122221122223412222221632222233122242222122222212233244323221112222
3223234252425333332431323241242843445242944231814342524224422222443223243222362422833222432522631613
5452134346551715474544663545634746754756625755345636644657534453655793446525544636345465565236542746
2542562623212274282227172127123225532255466656628222232244712231262232124222523356442314912654421246
3211225233123342323233432331212112221333223152322132324122322221233222222322521122723211222213111242
3353334454347529351635537433344434323313154333345532453533454333633334344534233232145423334343453133
2421322222222122222412222224221426142223221233322432132223212222221222232223242222321211134225212332
2232211322312221132342223122223332153233222312242233123533242332222253322332323321225223222123351323
2212222224122111321322222224322232242322432433322422234351322233321213212322244451123232212221462122
4753242222111143324312132423244222742522462564632213322315132132323232112123373333383241221125242823
3343332433226522442332523325331434632334332234433541332334332333321533324463433414323223423452445324
4524344323452457441245426433352336433434326553565344545554463553455544438435453243345453543233364638
2222422222322342226232333126234322221442134145212323311233323344224332322333321262212224323432221122
3383323533448644425453227556452478544424474425324446235764223364445645333444575644414514755244224244
2343242223221423221232222232322235123135222322222232122251223342243132222424232324122223332321323212
5354554743424254448274373224228344347744467457167542342976472253445454527198343453444286454338544176
2223211322212241122221232422322724112224232322212224323223222222212223424124222122257222212222232222
2434554333445323364344355343542565534353653433345424223233434552444423334434254443374352343633354723
1122223211242218322232222242222112412222222221222222222212321214432122222222122244222323222222433232
3435223328333433433423336213335333445244223322372743356333333243335234343654352533322143336232326235
8378544742468475446585326743783446225742243547346625347879353444342544356656636165235963767334746667
4244322165354443483434332332423145332345674233324467345451342454527444434234355653425334335554423131
3244347437313313632433324346415274333463523334233534224637213384333473875425377585334343383721435623
2685554554535645656456735785556654556665554733943566616656484545455547455754473547517575534755375566
2222322216222333226262621222213222222227514342221543131325335242221222513222622112127222215253128122
2222222262225252142233624112522171323122223221242262223222122712226213222711321417112221235466122121
4333334333225345444622224221332232322243222233423332232235132211236444525373232243322332243234262321
2642248522122252125221232142221232256424223236215252122642362222222132321222242222522122221426222223
1226322213251222422211232523211123242331232172224333323215133213233424333516323452242312232222623422
3122222231213232113242212232822222232222122321222222222212222222122321123322222212322212121221222422
5423564478854726637332596675245464556354355735582626475634453543575355574784577434459673155684554357
7222321335635332262332412434655413219633232924622212225164226142242353542343246225753542125222223323
4267551322534512666454222747274155257242331311455351563556413453655624563424531822633534636274521322
2322252211221211212122132112222243131223523232132222423222341263332222222212222222222222222221122223
1768674274274986682341368484535667696977568449486752359465927464497766917178351845264522476299462558
9973345886329838748767628288667977447953869989379958839745584248767715872788288778581455834264386794
3147438151432333338344236338737232494224134234544451682652215132332644832842454283383842482244335321
4643566433347766744675635495457354222475461644663645226234346245536452754345745454465444932565942745
2225215324326423122223233242434242213262412222221233223222352542224342323121544223222222122424243522
5322245452664656566453255356611235646656355152264354425465513351415225316613615411625412314435245789
3532512623242331522454811463433144216352121322573268272964662224275267222324238362545294252247226249
4212831421213232133322223142223222232221223244343332213353123322323123224122232252313232232212321237
2559516646662636458444562545554866446626574222272432644244645625329345646257123324497646538129494375
9487697645944865567668753567656668967724574897467656455865855168454565445566546659685676656548457565
2271259332592124428922232222322232962517342622322222232214172224332228267227221228162482262221225237
6443233123316232234343521343174244263223442223223423321152221313245363424122444225443331425623722332
2631223233332234232222222232233223624324362331422523223632222321252622533132312233252333334122352232
4374594442425431244344493962542232434523455225227447382442522423334443974437423421342463423254543531
2355432333335223334533427244747262427442234442324341339233344374345153362152323144742423388326423162
4443445423434244443443444444444444444453454434424544344351234534453344444435446233443343433434443455
2253732224712335116222521332125212432325522332331223222422222222232262233125222132623218312312722532
2745126446333335224424526135332462445373274523354244145463333432422845424534532453225244484442333526
7243231322222522523243218221822229211222272222132121225212212218162223322262253352242226222836222214
8455234156755525686463552552391658345445893588246565526285564774355441646662555577978368642945158266
3343333323213243433334231333434335323412312313221233333132122333233383333233332333432113332333333423
3383456734935633752439463334363637796364566576539414396635336233843642426783335654663528832337243282
5344511435325253223124322422315232342443444521133233112342244413322214342143342332245442224284224114
5672432544132225246254263346129622122631332113422332144322322464231221212259362226232222422423235425
4524322944436266647664456155534453463635345234664725273342535542575445335444366346254436463682322565
3451334242232142352433232562333233532438333363343323646422333622532339344445433533245221232362638635
3233443334336222212333333313221143322323332233423311342213212323422233331233212221332323232313422232
3538335453453474345453632453342555554432484344312443323743442437447424536444524434375434843224232542
1134323333132313131333332323533133333233333623323322333244441332334233337333342274333223433233323332
4454243314521342324532544347434334434335235444344344323342233636214464723343447422324342472464431443
7212243259122532255215244522214125222124232452252245212117552255264655222522223426322454253422326222
2312222222222232222222222222222322717232232212282223722231223211112223222213222121232234222222322523
2423334254424242124413222223224442344344242422322434223443444442221241423332334442224443342321462224
2128125222642211322122234225124121551426222122221152223262222223412226123232213242622126562225123425
6363447276635566243523623745255552583521353635323266575519555685676553658347552943633545254924961552
3676667755587373466346533576626132364223763383165653452675353348646644426366668265737356353476244576
3422633322422226226233112222224522622422332233123762752272222426216644523363422226125279441225422222
2421513543234255328413453513213432322425243313515222254346232334321335531342525442552353235434465512
6415375652731773743516117625153734627224124775172663723121614653367227355752147244664377477315124189
2422422221222321721222213222222133221322412121222341242222222524122224222242242227224232222222122221
2222263221312126226422222222222232232232522122222312521212274512142222222239332233161324142222522322
5214224342122322832221621223821233232353524332825644542254213312221562222316123326232465523312421432
3666692346463343673765135653333572386454548636472533677643225343655156493533632665435433533363333333
3236336332133335334334333531334446329333352333444233443333333223336235333533336533533333723322331753
2332334253445774533552442335534343543373535353354533442353484244432315244243232631235533431325434332
4552521221112255538222453252525742232852622245255471828252221432256124422144256362213422525332324121
2372252333723353563333333333333473383323733335833336432411333324316543339422239234393336716637333323
3332446134233313232612333632163334334322633133212431333462333333224835333344223255333363212354123333
7552341253243523459433362762313446662433224122932234263432225853655524828528628382822322238267265157
2322134512221642323542323223511513233543226122331621224322223253615122232123422252522132621252231222
3429232333431322243333344632343394443442424314352323431333625322314431334423554324321344442415335134
3133332324343452323333322323333333333334333264133233333332532333133321223323533333342253313333423342
1343335423352323663332543443263364446232322235234335543232624626585721313544983224252225332324323924
2343384322234463353555425322583755232622465243247424222413556356413728443742314344222113244251532353
3232332334422662733311142222213624242324423442212272161353921332323323365114222662326433222211432822
4744234125222815354354425333344321443843244443221424452394343442222524324344242221332233234242225342
3462334428262249425482933131743232559746433332564353333244493126335533625323339453832154482421223425
2222222233134212222222432222224221127322233223212223221332321127222222212311222222122222243322422222
2432244232223322212223324331424131121232632326332432333422623232243223132243212732213222236112343243
2353263413345543333423353333424313435236563342233313443334554332372614164221334333214242433343743323
7262356332233565589343731635726333342324341343234343563337573343334533753353534223443345323333245553
3431242323312112222476523222532327232153214423528222422132232322222221233323223323234454532312329644
2333735367546552954626277275124585694263757293962825277347554513674261336273395694226566284745773263
4173649643475436334333444938463434845834634643336464565244456555431735334846564366333644454658838434
4224822332242633868373338733362313852898442333324233232246336466283378321233368462332243239624724493
3534333234432441324334224322153333431244222322152323333332413324252424224444334223352334223435244433
6797668434457363662697495794664668479675438735856666846457733757642696558784863738494973567987667785
2532234351434333422252314325113333335424552231452251415554155221313444214412351243321242521144116789
3342963235551769332228529676272853356744577245483232553575727464245624975847586372312522857261324765
2524433365455335343313442426354273335335232214443343435433184332364226333334324442532433433436434544
4222122212332332222223231213222222312211212212121323212233252322321222222213413222323221123352221114
5424545545342434324453533434332434353334333329332435543336333525325324324362551332333536454133333544
2222314222413221272111232133221322313112115242245223141212412222244222235114332221123332223221212312
3212651335442221253634222242124222222642441435234212423212262422522122224324245252222146563216221221
4534343343554434373455224333444314444534552343337347433344543433344554444335355543345534555526436343
2912822222483322222232264222224151451255223222333452422122242122248232332227722421522312222232532253
4363374545543434544442423374342545266566843654647345434434454434474633695544412328432786264722435234
3343564345744435343522324454522434441435423445134313333444444543324535244244543454334444434424484334
2322363112224333222332112622331313163122212323222223232231512332235223225122413223321223322222232531
8546436128277177943554413533685443435367862667384842533295736533834223234414837551795743345232755356
2213223311223816122359924212256213822322211521422222324321352333322322323236222322223293232222322212
2554435553538564422555424565552315565514525555355255522743428555232444455355554251246333452345246463
1333422325315555874222532212731223224223254342221837511242452534326213523243452252233223213322135222
5645369885892365427476667837637577773387463956656474667757655456846365465836456434746556672555434687
1421112121223232721222412222222242124512328121322221222122322232222222221131222232242224221212222222
4775668564556556665766856661654556565656666644646647565572556665554564646675658255256457555646455672
2342452332222224551123243223222322223222532243215346511152222243322321233351223234222223444213272213
3134413132411423221323242221321124214114211142114424214131341214344331244312323444323444224243356789
6474433335362433434337434438443364393293343534343624232383334873273856337846437336343634353362444433
1233243332444222224424623312622422472142523322262753551222225262222542223454332322545228267264346425
5236344253423354343636325463336333356423827726333436538779343734634346263644934337562547952563333333
4224632223222123112222412224243141262412423241322342423442281422522214232242353421222312244224243143
7469877946847665655586749745973337476666558883376764458965456455676568725755555867744856659595665642
5356755557561456634566445832832547543365283554567566675752345723326653666295553666765253254564575736
2321311332333231423323221223223244435231441322214322333343224233412234223313431433353223233353143421
1124322462241222221323531412334544222322323221214234222324232244223432212422222224223232224112243112
3332442446346324333247187342434234143434236438533225434637244346324434633333433333446545833348946344
6262322616264315213225632354273927727338283215826672178422371664423342442785822317832925655256257219
5445524443444335333443353344243433444334443434344463323445434545248233545423324544444294345453333455
4223232323333222323333332333322222233332323333313333323242343333332533345333222234233313223533211212
2223426222252122224112251232142242221222124233312222241221222222212222211222222212224522125231221311
4627241234423313342333322364213144422247433314234222223143223654226435149632233443252543432234336311
3333323422333333355233333234444322224324422233131223315323344321434242338123343332321421433143242699
5722624812261213242322822324282536352232415433623728387976822137157726222323252211524835225474266852
6336122553423455134143235434142621433345423333324344332423352336233623352364143643333142317333344763
2323435345339933333233234413323332823632433922347933212225322223237234444332332812753453212221655776
2114332232122222227222233321132222191224112232322223323413216233224325414223233154142153523213232123
4363233516332573234322264326642333411233222345332333464327253633423433332221226225313723253223432622
3422222121231221222421222222121272211223124211222222214432222512422226462422122222221351154222226222
6573185767843422374335785736331723571626823274547783151717468323887386811276575242725477122713746669
2433323232322344222233231233134132334523323222232325242333412233214322233314333222533333224362222422
3222122212322122231223222223232222222313222223113212232222222133211321213121322523223222211221322321
1242342322434314342233214544224322432222222225724432314723213225244242244514424424223342332323333244
3322223323232222222221522223212133322322222621322222322222322223213622312322343212233241322414122422
2625245231422282221441423513255742414624272364847631422343335664343424253113724224727363324547221242
3326371332622228837252563473123241224363133633313838447333321443724272332475132742349125916442355543
7233222122232253233222242323323223314121222243233553222212213232332233632222221633531222213332222322
4421224312252242222221224112221222222224212412222452223422331222221221224222221232323262264232222222
2253123212223333222222213222243313323332331322233232333213243323321232223233337222421123221325322512
3225422162433131224312223212312251243322224132425122234262233531224344222313224662223321322393231236
4642342261562254443344423445428214722743264973162525543742232816222224553343421222242126532254365481
2312212121222212523433224231211322312282222222522222132135222331121123255225272322122223213222222223
4312231325246222214421322221112212232222221236232224117221364342225122132212423231222513213355222221
2651242333442243323334334146333233144333442323333456343363233634333343243343244336244433225344135332
2763248524234572433753223233622223332331723214122435341443231732223246222334223132623282652622283543
3243222223323231422312437333423314711552416423122312422543424332522221326222321244222322221422322327
2233554224243333415331323334332253253232644223453213913343546821552553232233353435533223533435332663
9646747667546966454644685753753665685635164767563757666773248774626764856573263365676475553556725744
3865568344165174355665436356555345223355325727554952341715231585477375575282248527523565666551546585
3234454554445754244327453332241719736853566345534435633379658436763224463639555336653466444123247355
4438345364534443344333623345533334653253544343661423635845633336348738944346332432336336246326235385
2114324212222322222222212222254122222222224124132122221332222222424224232332221334212112412425623122
6221313223122322312222223332222212322222322243233162222222221321233262222522241312222311223223222251
3134343613223133354232233332661513531335333432332323336333332333333323334433432234333333333632313335
3413243315334423214232123334223123332333344421223321223234232342332444434512222243333333421331422733
3643433552173224434364263213563492453254231457525737335444546343413142646222466454322422842347463343

49
mark/day3/part1.rb Normal file
View File

@ -0,0 +1,49 @@
require 'debug'
module FileHandler
def self.read_lines(file_path)
File.readlines(file_path, chomp: true).map do |line|
yield(line)
end
end
def self.read_file(file_path)
File.read(file_path)
end
end
class JoltageCalculator
def initialize(file_path, debug: false)
@file_path = file_path
@debug = debug
end
def calculate_total_joltage
total = 0
FileHandler.read_lines(@file_path) do |line|
int_array = line.split('').map(&:to_i)
length = int_array.length
first_digit = int_array.slice(0, length - 1).max
first_pos = int_array.find_index(first_digit)
second_digit = int_array.slice(first_pos + 1, length).max
total += "#{first_digit}#{second_digit}".to_i
end
total
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby part1.rb <file_name> [debug]"
exit 1
end
debug = (ARGV[1] == "debug")
calculator = JoltageCalculator.new(ARGV[0], debug:)
puts "The answer is: #{calculator.calculate_total_joltage}"
# The answer is: 17166
# real 0m0,256s
# user 0m0,207s
# sys 0m0,049s

View File

@ -0,0 +1,67 @@
require 'debug'
module FileHandler
def self.read_lines(file_path)
File.readlines(file_path, chomp: true).map do |line|
yield(line)
end
end
def self.read_file(file_path)
File.read(file_path)
end
end
class JoltageCalculator
def initialize(file_path, debug: false)
@file_path = file_path
@debug = debug
end
def calculate_total_joltage
total = 0
FileHandler.read_lines(@file_path) do |line|
int_array = line.split('').map(&:to_i)
total += calculate_joltage(int_array)
end
total
end
# @param [<integer>] bank
def calculate_joltage(bank)
joltage = ''
first_digit, first_pos = max_and_index_from_slice(bank, 1)
joltage += first_digit.to_s
sub_bank = bank.slice(first_pos + 1, bank.length)
second_digit, _ = max_and_index_from_slice(sub_bank, 0)
joltage += second_digit.to_s
joltage.to_i
end
private
# Get max and index from the first part of the array. Ignore the last `offset`
# number of digits.
# @param [<integer>] array
# @param [integer] offset
def max_and_index_from_slice(array, offset)
maximum = array.slice(0, array.length - offset).max
index = array.find_index(maximum)
[maximum, index]
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby part2.rb <file_name> [debug]"
exit 1
end
debug = (ARGV[1] == "debug")
calculator = JoltageCalculator.new(ARGV[0], debug:)
puts "The answer is: #{calculator.calculate_total_joltage}"

91
mark/day3/part2.rb Normal file
View File

@ -0,0 +1,91 @@
require 'debug'
module FileHandler
def self.read_lines(file_path)
File.readlines(file_path, chomp: true).map do |line|
yield(line)
end
end
def self.read_file(file_path)
File.read(file_path)
end
end
class JoltageCalculator
def initialize(file_path, debug: false)
@file_path = file_path
@debug = debug
end
def calculate_total_joltage
total = 0
FileHandler.read_lines(@file_path) do |line|
int_array = line.split('').map(&:to_i)
total += calculate_joltage(int_array)
end
total
end
# @param [<integer>] bank
def _calculate_joltage(bank)
joltage = ''
first_digit, first_pos = max_and_index_from_slice(bank, 1)
joltage += first_digit.to_s
sub_bank = bank.slice(first_pos + 1, bank.length)
second_digit, _ = max_and_index_from_slice(sub_bank, 0)
joltage += second_digit.to_s
joltage.to_i
end
# @param [<integer>] bank
def calculate_joltage(bank, total_digits: 12)
joltage = ''
current_offset = total_digits - 1
while current_offset >= 0
maximum, i = max_and_index_from_slice(bank, current_offset)
joltage += maximum.to_s
bank = bank.slice(i + 1, bank.length)
current_offset -= 1
end
joltage.to_i
end
private
# Get max and index from the first part of the array. Ignore the last `offset`
# number of digits.
# @param [<integer>] array
# @param [integer] offset
def max_and_index_from_slice(array, offset)
maximum = array.slice(0, array.length - offset).max
index = array.find_index(maximum)
[maximum, index]
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby part2.rb <file_name> [debug]"
exit 1
end
debug = (ARGV[1] == "debug")
calculator = JoltageCalculator.new(ARGV[0], debug:)
puts "The answer is: #{calculator.calculate_total_joltage}"
# The answer is: 169077317650774
# real 0m0,260s
# user 0m0,222s
# sys 0m0,038s