Compare commits

...

6 Commits
main ... mark

Author SHA1 Message Date
Mark Bebawy f91dffa0a4 Solve day 6 2025-12-14 20:51:33 +01:00
Mark Bebawy 9aacd7d8ba Solve day5 (part2 code is very ugly... but it works :)) 2025-12-14 18:49:08 +01:00
Mark Bebawy 3a3764bf4c Solve day 4 2025-12-14 16:06:14 +01:00
Mark Bebawy d1b90dfefb Remove .keep 2025-12-14 14:34:00 +01:00
Mark Bebawy 2b400c1d7a Solutions day1-day3 2025-12-14 14:27:16 +01:00
Mark Bebawy 78a2917ea3 Initial commit 2025-12-14 14:25:36 +01:00
28 changed files with 7356 additions and 0 deletions

0
mark/0_dayn/example.txt Normal file
View File

0
mark/0_dayn/input.txt Normal file
View File

61
mark/0_dayn/part1.rb Normal file
View File

@ -0,0 +1,61 @@
require 'debug'
# Generic FileHandler
class FileHandler
def initialize(file_path)
@file_path = file_path
end
# Usage:
# FileHandler.new('/path_to_file').read_lines do |line|
# # process line
# end
def read_lines
File.foreach(@file_path, chomp: true).map do |line|
yield(line)
end
end
# Usage:
# file_content = FileHandler.new('/path_to_file').read_file(
def read_file
File.read(@file_path)
end
end
class PuzzleSolver
def initialize(file_handler, debug: false)
@handler = file_handler
@debug = debug
end
def debug(message)
puts message if @debug
end
def print_debug(message)
print message if @debug
end
def solve
raise NotImplementedError, 'Please implement this method in subclasses.'
end
end
# TODO: Puzzle-specific
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby #{__FILE__} <file_name> [debug]"
exit 1
end
file_path = ARGV[0]
debug = (ARGV[1] == "debug")
file_handler = FileHandler.new(file_path)
## Puzzle-specific
cls = PuzzleSolver
##
puts "The answer is: #{cls.new(file_handler, debug:).solve}"

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

10
mark/day4/example.txt Normal file
View File

@ -0,0 +1,10 @@
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.

137
mark/day4/input.txt Normal file
View File

@ -0,0 +1,137 @@
@@@@@@.@@..@@.@.@@.@@...@@.@@@.@.@.@.@@.@@@@@@@@@@@@.@.@@.@.@...@.@@@@@@@@..@@..@@@..@@@.@@@@..@.....@@@.@@.@@@@@@.@@.@@.@.@@@@@@.@@.@@.@
..@@.@@@@.@.@.@.@.@@..@@.@@@@@@.@@@..@@.@@.@.@.@.@.....@@..@.@@@@...@@@.@.@@.@..@.@@@@@..@..@@@@.@@@@.@@@@@@@@@@@@@@.@@..@.@.@..@.@@@@@@@
@.@.@@@@@@@@@@@@..@@@.@...@@.@..@.@@@.@..@@@.@@@.@@@@..@@@@.@@@@@.@@@@.@@@@@..@@@@@@@@.@@@@@@@@.@@@@.@.@.@..@@..@@@..@..@.@@@@.@.@..@@..@
@@@@@@@@@@...@@@.@@.@@@.@@@@.@..@@@..@@@@@.@.@@..@.@..@@@@..@.@@..@.@@..@@@@@@@@..@@@@@@.@@.@@@.@.@.@....@@@@.@.....@.@..@@@@@..@.@@..@.@
.@@@@@@@...@@.@@@@.@@.@@.@@@@@.@..@@@.@@.@@@..@.@@.@@..@@.@@@@@@.@@@@@@@@@..@@@.@@@..@@@..@..@@@@.@@@.@@@.@@@.@@..@@@@.@@@..@.@...@@..@@@
.@......@@@@.@.@.@@..@...@@@@.@@..@@@..@.@.@@@@.@..@@.@.@.@@@@@....@@.@@.@..@@@.@.@....@@..@@@@@@@@......@@@@@@.@@.@.@.@@..@.@.@@@@@@@@@@
@.@..@@@@.@@.@@@@@@.@@@.@@@@@.@.@@@@@.@@@@.@@@@@@@@.@.@@@..@@@.@@@@@@.@@@.@.@@.@...@.@@.....@.@...@@@.@.@@@.@@.@@..@@@@...@@.@@.@@.@..@@@
@..@@@@@@@@..@@@@@@.@@@@@.@.@@@@@@.@@..@@@@.@@@@...@@..@@.@.@@@@.@..@@@@.@.@@@...@@@..@@@.@.@....@.@@.@@.@@@..@@@@@@@.@@.@@..@@..@.@@.@@.
..@@.@@@@@@@@@@@@@...@.@@@@@.@@.@@@@...@@@@@@..@@@.@@...@.@.@@@.@.@@@.@@@@@.@@.....@@.@@@@@@.@@@@@@.@@@.@@..@@@@@@@.@..@@@@@@@.@@@.@@@@@.
@.@@@..@@@@@@@@@@@@@@.@@.@.@@.@@@@@@@@@@@@.@.@@@.@@@.@@.@@@@..@.@@@@@@@@@@@@@@.@.....@..@.@@@@@@.@..@.@@....@@..@.@@....@@@@@@.@.@@@@....
@@.@@.@@.@@@.@@@@@@.@@.@.@@@@@@.@....@@@.@.@.@.@@.@@.@@...@.@.@..@.@..@@@.@.@@.@@@@@@..@@@@@@.@.@....@@@..@@..@.@@.@.@@.@.@@@@@.@.@.@@..@
@@@@@@@@.@@@@@@@@@.@@@@@@..@.@@.@@.@@@...@.@.@@@.@@@@@@@@.@@@@..@.@.@@@@@.@@@.@@@@@@.@@@@.@.@@@@@@.@@..@...@@..@@@......@@..@..@@.@@@@@..
.@@@@.@@@@.@@.@@.@.@@@@...@@@..@@@.@..@@@@.@@.@@@@@@@@..@@@..@@.@@@@@.@@..@@..@.@@@...@@..@.@@@@@...@..@@....@.@.@@@@.@@@.@....@@.@@.@@@@
@@.@.@.@@@@@@@@@@@.@@@@@.....@..@@.@@@.@@..@.@.@.@@@..@@..@@@@@@@@@@@@@@.@@..@.@.@@@..@@@@@@@@@@......@.@@@@..@@@.@@@.@@@.@.@@@@@@@@@@@..
@@@@@@.@..@.@@@.@@@@@@@@@..@..@..@@@@@...@.@@@.@@@@..@@@...@@.@@@..@.@.@@..@.@@.@@@.@@@@.@@.@@@@@@@@..@@@@@.@@@.@@@@@@@@@@....@@@@@.@.@.@
@@@@@@.@@.@@@.@@@@.@@@.@.@.@.@@@@.@.@@.@.@@@@...@@@..@@@@.@@..@@..@@.@@.@@.@@@@..@@.@.@.@.@.@@@@@@.@.@.@@.@.@.@@@....@@.@@@@..@.@@..@@@@@
@@@@.@.@.@@@@.@.@@@.@@@.@..@.@@..@...@..@....@@..@@@...@@@@..@.@...@@@..@@@@@@@@..@..@.@@.@@@@@@@.@@@.@.@@@.@.@@.@@@@@.@@.@.@.@@.@@.@@.@@
.@...@@@@.@.@@@@.@@.@..@@@@.@@@.@@@.@.@.@@@@.@.@.@.@.@@@.@@@.@@@@@..@.@.@@@@@..@@.@@@@@.@..@..@.@@@@..@@@.@@@.....@@@@@@@.@@@@@@@@..@..@@
@@..@@@@.@@..@@.@@.@..@.@@.@@@@.@@@@@.@@@@..@@..@@.@.@@..@.@@@.@@@@.@.@.@@@@.@..@..@@@@...@.@@@.@@@.@@.@@@.@@@@@@.....@@@...@@..@@.@..@@@
@.@@@..@@.@@@@@@@@@@..@@@@@@@.@..@@@@@..@@@@.@@@.@@..@.@@.@...@.@@@@.@.@.@@@@..@..@..@.@@..@.@@.@@..@@@@@@@@.@@.@@@@@...@.@@@@@.@..@..@..
@@@..@@@@@.@..@@..@@..@@@.@@..@...@..@@@@@@@@@@..@@@@@@..@.@.@@@@@...@@@@@@@@.@.@.@@@.@@.@@@@@@.@..@..@..@@@@..@@@@.@@..@...@@@.@..@@.@@.
.@@@.@.@@.@@.@.@...@@..@@@@@...@@@...@@@@.@@@@.@@@@@@@@.@@@@@.@@@..@@@@@..@....@.@@@@@@@@..@@@@@@@.@@.@.@.@@.@@@.@.@.@@@@.@....@@.@@@@.@.
@@@@@@@.@.@.@@.@.@@.@@.@@@.@@@@..@@@@@@@@@@@@.@@@@....@@..@@@..@@@@@@.@.@@@.@..@..@@.@@@.@@..@......@@@@.@.@@.@@...@@....@@.@@@@@@@.@@@@@
@@.@@...@@..@@.@@@.@@@@@.@@...@@..@@...@@@.@@..@.@.@.@@.@.@.@@@@@@@.@@..@...@.@@@@@...@@@.@@@.@@.@..@@.@@@..@..@@@..@.@@.@@@@@.@..@@@@@@@
.@@@.@@....@@@.@@.@@...@.@.@@@@@@@...@.@.@@@@@.@.@..@@@.@@..@@@..@@@..@.@.@..@@.@@..@@.@...@.@.@@@@@@.@@@@.@..@.@@@@.@.@@.@@...@@@.@@@@..
.@...@@@@.@@.@@@...@@.@...@@@.@@.@@.@@@.@.@@@.@@@@@@@@..@@.@@..@.@@@@..@@.@@@..@.@@.@.@.@@.@@.@@@@@@@@@.@@@@@..@@@@@@@@...@..@..@@@@@@..@
...@@@@...@.@.@.@@@@@..@.@@.@@.@@@@@.@@@@..@@.@...@@@@@..@@@@@..@@...@..@@@..@@@@.@.@.@@@@@@@.@@@@@@.@@@@........@@.@@@@.@@@@@@@@.@@@@@@@
@@@@@@.@@.@..@@@@@.@@@@.@@..@@@@@.@@@@@@.@@.@.@@..@@@@@.@@@@.@@.@@@.@@.@.@@@...@@...@..@@@.@@@@@@.@@@@.@@.@..@@@@@@.@@..@.@@....@.@....@@
@.@.....@@@@@@@@.@@.@@..@@@...@..@@.........@@.@@@@@@@@.@@@@..@.@@.@@@@...@@..@@@@...@@.@..@@@@@@@@@@.@@@.@@@@.@.@@.@@@..@.@@@.@.@...@@.@
@.@.@..@..@.@@..@@@@.@.@.@...@@@@@.@.@.@@@@.@@@.@@....@.@@.@@@.@@.@@@.@@.@..@@@@@@@.@@...@@@@..@.@@@.@@@@@@@@.@@@@.@..@.@.........@@@...@
@@@@@.@@@@@@@@@..@@@.@.@@@@@.@@@@..@@@.@@@@@.@@@@@@.@@@.@@@.@.@@@.@.@.@@...@@@.@@@@.@...@@@.@@@..@@.@@.@@@@...@.@.@@.@.@@.@.@@@...@@@@@@@
@..@.@@@..@@@@@@@@.@@@@@@.@@.@.@@@@@@.....@@..@@.@@..@.@.@@@..@@@@.@@@@@.@@@@@.@...@@..@@@@.@@@@@.@@@@@@...@@@@@@@@@@@@@.@@@.@@@@.@@..@@@
@@.@..@@@....@@@@.@.@@..@@..@.....@@.@.@@.@..@@@@.@..@@.@.@@...@@@@.@@@@@@...@..@@.@@@@@@.@@@@@@@@.@.@.@....@@.@@..@@@.@..@@@.@@@@@@.@@@@
@@@@@@..@@@@@...@@@.@@@@...@.@@....@.@@.....@@@@@@..@@@@@@@@@@@.@.@.@@@@..@..@@@..@.@.@@@@.@@@@@..@....@@@.@..@.@@@@@@@@@@@@.@..@.@..@@.@
@@.@@.@@@.@@.@.@@@@@@@@@@@.....@.@@..@@..@@@@@.@@..@@@@@@.@.@@.@@.@@..@@@@@.....@@@@@@@.@@.@@@@@@@@@.@.@.@@..@@@@.@@..@....@@@@.@@@..@@@@
@@@.@@..@@.@.@@@@@@.@.@@.@.@@@@@..@@.@@@@@@.@@..@@...@....@@..@.@@.@.@.@@@.@@@@..@.@@@@@@@@@..@.@@@.@.@...@@.@@@@@..@..@@@@@@@@.@.@..@@.@
@.@@@.@@..@@@@@@.@..@@@@..@.@@...@@.@..@@.@.@.@@.@@@@@@@.@@.@.@@...@@@@@@.@@.@@@@@@@@@@@@@...@@..@@@@@@@@.@@.@.@@@@@@@@@.@@@..@@@.@..@@.@
..@.@@.@..@@@.....@@@@.@@.@@@@@@@@.@..@@@@.@.@.....@@.@@..@@@@..@@.@.@@.@.@.@@@.@@@@@.@@.@@@@@@@@@@@@.@@@.@@.@@@..@@.@@..@..@.@.@..@@@@@.
.@@@.@@.@@@@.@@@@.@@..@@@@.@.@..@.@@@.@.@@@@@@@@..@@@@.@.@.@@@@.@@@@.@@...@@@@@@@@.@@.@.@@.@@@@.@@@@.@@..@.@@..@...@@@@..@@@@@@@@@.@@@@@@
@.@.@.@@@@.@.@.@@@.@@@.@@....@.@@@.@@@@@@@@@..@..@@@@....@...@@@@.@@@@..@@...@@@@@@@@@@@@@@....@.@@.@@.@@@@@@@...@.@.@@@.@@@@@@@@.@@.@.@@
@@@...@..@@@.@@...@..@@@@@@@@@.@@@@@@@.@@.@@@.@.@@@@....@@@..@.@@.@@@@@@@.@@@@..@.@@@..@.@@..@@@@@@@@@.....@.@...@@@@.@@.@@@@@@@@@@.@@@@@
..@.@@@@..@.@@.@@@.@.@@@@@.@@@@@@..@@@.@@.@.@.@.@..@@@@@@@@@@.@.@@@@.@@@.@@@@.@@@.@..@@......@@@@@@@@@@@@@@@.@@@.....@@@..@@@@@.@@.@.@@@@
@@.@@@...@....@...@..@@@@...@.@@@@@@@..@....@.@@@@.@.@.@@@@...@@.@@@@.@.@@@@.@.@@@@@..@@@@@.@@.@.@@@@@.@.@@@@@@@@@@@@@@.@....@.@@@.@@..@.
@@@.@@.@@.@@@@@@@.....@@@@.@@@..@@.@....@@.@@@@.@@@@@..@..@......@@@@@@@@.....@.@...@@@.@@@@@..@@@@...@@.@@@.@@@.@@@@.....@@.@@@@@.@..@.@
.@@.@..@@@@@@@@@@.@@@@@@.@@.@@@@@..@@@@.@@@@@@...@@.@@.@.@@...@@.@@@@@@@@@.@.@@@@.@@@@@@.@@@@.@.@@@@@.@@@.@.@.@.@@.@@@@@@@@.@@.@@.@@.@.@@
.....@.@@...@@@.@.@@.@..@.@@@...@.@@@@@@@.@@.@@@@@..@.@@@@@..@@@@@@..@@@@..@..@.@.@..@@@...@@@@@.@.@@@.@@.@@..@@@@@.@@@@@.@@@@@@@@.@..@.@
.@.@@.@.@@.@....@@@@..@@@@.@@..@.@..@@@.@@@...@@@.@@@..@.@@@@...@..@.@@.@@@@@..@@@.@@.@@..@...@.@@.@@@.@@@@...@@@@.@@@@..@@@@@@@@@@..@@.@
.@@.@.......@@@@...@.@@@..@@.@..@@@.@@@.@.@...@@..@@@@.@.@@@.@.@.@@@@@@..@@@@..@@@@.@.@@.@@@..@@..@@@@@@@@@@@@.@@@@..@@.@.@@@.@.@..@@..@.
.@@@@.@@@@@.@.@@@..@..@.@.@@.@.@@@.@@.@@@@..@.@@@@.@@@@.@.@.@@@@.@..@.@@@@.@@@.@@.@.@@@@.@@@@..@@@@@...@@@..@@@@.@@.@...@@@@@.@@@@@@.@@@.
@@@.@.@.@@@@@.@@@.@@@@@@@..@@@..@@@.@@.@.@.....@@@@@@@..@@@@.@.@.@@@.@@@@@@.@..@...@@@@@@..@...@@@@@@.@@@...@.@@..@@@..@@@.@@@@.@@.@@@@@.
@.@...@@@.@...@@@.@.@@@..@.......@.@@@@@.@@.@.@@@..@@.@@@@..@.@@@@@@@@@@@@@.@@.@@@...@@@..@@@@...@.@.@@@..@@@@...@@@@@..@@.@@.@@@@@.@@@.@
.@.@...@..@.@@@@@.@@@@@@@@.@.@@..@@....@..@@.@@..@.@@....@@@@@@@.@@@@...@@@@@@@@.@@.@..@@@@.@@@@@.@@.@..@@@@.@@...@@@@@.@@@@@@@@@@.@@.@@@
.@.@......@@@..@@@.@@.@@@@.@@@@@.@...@.@@@.@@@@@@@@@@@...@@@@.@@@@@.@@@@@.@..@@@.@.@@.@@@@.@@.@.@@@.@@.@.@@.@.@.@@.@.@@.@@.....@.@@@@..@@
@@@..@@.@@.@@..@@.....@.@....@.@@@.@@.@@@.@..@@.@@@.@@.@@@@.@.@.@@...@.@@@..@..@@.@@@@@@@@.@@@@.@@@.@@@@@..@@@.@@@...@@@..@@.@@@...@..@@@
@.@.....@@.@@.@.@@@.@@@.@.@@.@@@@......@@@@@.@.@.@@.@..@.@@@..@@@..@@@@..@@..@@@@@@.@@@.@..@@@.@@@@@@.@.@..@@@@..@@@@.@@@@@@..@@@@@@@@.@.
@@@@@@@.@.@@@@@..@.@@@.@@@.@@.@@@@@@.@@@@.@@@@@@@@@@.@@@.@@@@.@.@....@@@.@.@@@@@@@@@..@@@.@@@.@.@@@@@@@.@@@@@..@@@@@@.@.@.@@@@@@@.@..@@@.
.@@@@..@.@@@.@@@@@..@@@..@@@@.@.@.@@.@.@@@.@..@@@@@.@@.@@@.@@@@@@.@@@..@.@@.@@....@.@@@@@@@@.@@@@...@@@@@@.@..@@@@@.@.@@.@@@@..@@@@@..@@@
@@.@..@@....@@.@@.@@@.@..@@@..@@@@.@.@.@..@@@.@@@@@.@@.@@@@@.@..@@.@@@@@...@@@@@@@@@@.@@@@@@@.@@...@..@@.@@.@@@@@.@@.@@@@@.@......@@.@.@@
@@@@.@@@@@.@@@.@.@@@@@@@@@@@@@@@..@@.....@@..@@@@@.@@@@..@@@@@@@.@...@@@.@..@@@@@.@@@..@@..@..@@.@@.@@@.@@@@@@@.@..@@.@.@....@@@@@.@@.@.@
@@..@@@@.@@.@.@@...@..@@@@@.@@..@.@...@@.@@@@@.@@.@..@@@.@@@...@..@..@@...@@@@..@@@@@..@@@..@@.@@@@.@..@@@@.@@@...@@@..@@@@.@@.@.@@..@@@@
@.@@@@..@@.@@........@@@@@@.@@@@.@@@@..@@.@..@@@@@..@@...@...@..@.@@@..@.@@@..@...@.@@@.@.@.@@@@@.@.@@@@....@@@@..@..@@..@.@@@@@@@@@@@@@.
@@@.@.@@@@@@@@.@..@@..@..@@@@@@@..@@.@@@.@...@@...@@@@@@@@.@..@.@@@@@..@@@@.@.@.@@@@.@@...@@@@@@.@.@@@.@.@@@@.@@@@..@....@@.@@@@..@@@@.@.
@@@@@..@@@@.@@@...@@@@@..@@.@@@@@@@...@@.@..@@@..@@.@.@@@@@@..@.@@@@@.@@@.@@@@..@@.@@@.@..@@@...@@@@.@.@..@..@@@@..@.@@@@@@@@.@@@.@@@..@@
..@.@.@@.@@.@@@.@@.@.@@@.@@.@@@@@@@.@@.@@@.@@@@@@@@.@@@.@..@@@..@.@@..@.@@@@..@..@...@@@.@.....@@@.@@...@@..@@..@..@@@@@.....@@@@@@@@@@@@
@@@@@.@...@.@.@@@.@@@@@@@@.@@@@@..@..@..@.@....@.@.@@@@@@..@@.@@.@.@@.@@.@@@@@@..@.@@@@..@@.@@@@.@.@@@@.@..@@@@.@...@.@.@@.@@.@@@@.@@@.@@
@..@@@.@@...@@@@@.@@@.@@@.@@@@@@@@@@...@.@@@@...@..@..@.@.@@@.@.@.@@.@.@@@@@.@@@@@@@@@@@..@@.@@.@@.@@..@@@@@.@.@@@@@@.@@.@....@@.@@.@@@@@
.@@@@@@@.@@@.@@@.@@@@@@@.@.@@@@@@@@..@@....@@.@.@@..@@.@@@@@@@.@@@..@.@@@@@@@@.@@@.@...@@.@@..@.@@...@@@@.@.@.....@@@@@@@@@@@@@@@@...@@..
@@@@.@...@.@.@.@@@@.@@@.@...@@@.@@@@@.@@@@@@@@@...@@@..@..@@@.@@@@@..@.@@@@@@@.@..@.@@@@@..@@.@.@@@.@.@..@@@@@@@.@....@@@.@.@.@@....@@@..
@@@@.@@.@@@@@.@@@@@@@@@@@@@@@.@@.@@@@@@.@@@@.@.@.@@@..@@...@@@@@....@.@@.@@@@..@@.@@@...@@@@.@.@@.@@@@@.@@@@....@..@@@..@..@@@@..@@@@...@
@..@@@.@@..@@.....@@@.@.@@@@@.@.@@.@.@.@.@@@@@@@.@@@.@.@@.@@..@@@..@@@@@@.@..@@.@@.@@@@.@@@@@@@.@@@@@@...@@@..@.@@@@@.@@@.@@@.@@.@@.@@@..
...@@@.@@@@@@@@..@.@@..@@.@@...@@@@@@.@@..@..@@@@@@.@.@@@@@..@.@..@@@@@@@.@.@@@@@@@@@@@@@.@.@...@@@@@.@@.@@@@..@@@@@@@...@..@.@@@@@.@..@@
@.@.@@....@@@.@.@@..@@.@@@.@.@.@.@@@@.@@@..@..@.@..@@.@.@@@@@.@@.@.@@@@..@..@@@@@@@@@@@..@@@.@@@@.@.@@.@.@@..@@@......@@@@.@..@.@@@@@.@..
@@@@@@@@.@.@@@...@@@@@@.@@@.@@@@@@@.@@@@@@@@.@.@..@@.@@@@@@@.@@@@@.@@@@@@@..@@@@.@.@@.@.....@@@.@@@@@@@.@@.@@@@..@@@@@..@.@.@.@@.@..@@@.@
.@@..@.@..@@@.....@@@@@.@@@.@@@@@@.@.@@@.@@@@@.@@@.@..@.@@@..@..@@..@@@@..@@@...@@@@@@@@.@@@@@@@@@@..@.@@@..@@@.@@@..@@.@.@@@@.@@.@@.@.@.
.@..@..@.@@.@.@@.@.@@..@@.@@@@@@..@.@@@.@..@.@..@@.@@@..@.@@@@@@@.@@@@.@@.@@@@@.@@@.@@@@@@@@..@.@@..@.@@@@@@.@@@.@..@@@@.@@.@@@@@@..@@.@.
.@.@..@..@@@.@..@.@@.@.@.@@@@@@.@..@@@@..@..@.@@@..@...@....@@@...@@@@@.@.@@.@@.@@.@.@@.@@@@@@@..@@@@@.@@@..@@@@@@@..@.@@..@@.@@@.@@@.@.@
@@..@...@@@@@@..@.@@....@.@.@@@@@@@@....@@@@@@..@@.@@@..@..@@@@@...@@@@..@@.@@@@.@@@.@@@@@.@...@@@@@@@@.@@@.@@@@@.@@@@..@@.@.@.@.@.@.@.@@
.@@@.@@..@@..@@@.@..@@@@.@@@.@..@@@@.@@@@@@@.@@@@@.@@@@..@.@.@@@@.@@@..@@@..@@...@@@@@@.@@@.......@@...@..@.@@@..@.@@@@@..@..@...@.@@..@@
@@@.@...@@@.@@@.@...@.@@.@@.@.....@..@...@.@.@.@...@..@.@.@@@.@@@..@.@@@@.@@....@@@@.@@@.@@@@@@@..@@.@@@@@@@@@@@@@@@.@@@@..@@@....@@.@..@
@...@.@@.@.@.@@...@@@.@...@@@@@..@@@...@@@.@@.@.@@.@.@@..@.@@@@@.@@@@@.@@@@@@.@@@.@@.@.@@@.....@.@.@..@.@.@...@.@@@...@@...@...@...@@@@@.
@@@.@@@.@@..@@.@.@@...@@@....@.@@.@..@@@.@.@@@@..@@@.@....@@@@@@@@.@@.@@@@@..@@@.@.@@@..@..@..@@.@@@@@.@@.@@@.@.@..@...@@.@@.@..@.@.@@@@.
@@.@@....@@@@@.@@.@@.@.@@@@@.@@@@@.@@@.@@@.@.@@@.@@.@.@@@.@@@@..@@@@@@@@..@@...@@....@@@@.@@.@@.@@.@.@.@@@@..@@@..@@.@@.@.@@..@@@@@.@..@@
.@@@@@@.@..@.@@@@.@@...@@@.@@.@@@@@.@..@.@.....@.....@.@@@@..@@..@@.@@@@@@@...@@@@@@@....@.@@@@@@@.@@.@@....@.@@.@@.@.@@@..@.@@.@..@@@@@@
@@.@@@@.@@.@.@.@.@@@@@@@@@@.@@.@@.@@@@.@.@@....@@.@@..@@@@@@.@@@..@@@@...@@@@.@@@@@@@@@..@@@@..@@@@...@@....@.@@...@@.@@@@@@..@..@..@@@.@
..@@@@.@@@@.@@@@@@@@@@@@@@@@@.@@@@.@@...@@.@@@@@@..@@.@@@.@@.@@@.@.@..@@@@@..@.@@.@@@.@@@@@.@.@.@...@@@..@....@@...@.@@@.@.@@@@@@...@@@@@
@.@@.@@@@@@..@...@@.@@@.@@@@@@@@@...@..@.@@@...@.@@..@@@.@..@@@@@.....@@.@@...@@.@.@@@@.@..@.@..@.@@@.@..@.@..@@@@@@@@@@..@.@@.@...@@@.@.
@@.@..@.@..@@.@@...@@.@@.@@@..@@@@.@@.@@@@@@@@@@@@@.......@@@.@@@@@@.@@...@@..@.@@@@@@..@@@@@@...@@@..@@@.@@.@@@@.@..@..@..@.@@@@@.@@@@.@
@@@.@@.@.@@@..@@@@.....@@@@@@.@..@@@.@.@@@@@...@....@@@@..@...@@@@@@@@@@@..@...@@@@@..@@@@@...@.@@@.@...@.@..@@@@.@...@@.@@.@.@...@.@@@@@
@..@@@@@.@@.@@..@..@@@....@@..@..@@.@@@.@.@....@@@@@.@@@@..@@@.@.@@@@@@.@@.@.@@@@@.@@.@.@@@.@...@@@@@@@@@@@@@@@@@@@...@.@..@@@@..@@.@...@
...@@@@.@@@@@@@@@.@..@@@@..@@.@....@@@.@.@@..@..@@.@.@@....@@.@@@@.@@....@@@.@@@.@@..@..@@@@@@@....@@@@.@@@@@@.@...@@.@@@@.@@@..@@@@..@.@
..@.@@@@@..@..@@.@.@...@@@@.@.@..@@@.@@@@..@@.@@.@@@@@@...@@@@@@.@@@@@@@@.@@@.@@@@@.@.@@.@@@@..@@@@@@@@...@..@.@@@..@@@@@.......@...@@@@@
@.@@.@@@.....@@@@@@@@@@@@@@@.@.@...@.@@..@@.@@.@.@.@.@@..@@@.@...@@..@...@.@.@...@@@.@@@@@@.@@...@@..@.@@.@..@.@@.@..@@.@..@..@@@.@.@@@@.
@.@@@.@..@.@..@@@@@@@@@@.@@@@.@@@@@.@.@.@@@@@@..@@@@@@@.@@@...@@...@.@@@@@@@@...@@..@...@@@@@@.@@@.@@@@@@@@.@@.@@.@.@@@@@@@.@@@@@@.@.@@.@
@@..@@@.@..@@@@..@.@@.@@@@@.@@.@@.@@@@@@.@@@@@@.@@@@@.@@@@.@@@....@@@@.@@..@.@.@@.@.@...@@@@@@.@..@@@@@@@@.@@@.@@.@@@@@@..@..@@..@.@@.@@@
@.@@@@.@@@@..@..@@..@@@...@@@@@@@...@..@@@@....@.@@.@..@@@@.@@.@@..@@@.@@@@@.@@.@@@@@@@.@..@..@.@@@@.@@@@..@..@..@.@@@@@@.@@@@@@@..@@@.@@
..@@@..@@.@@@@@@@.@.@.@.@@@@.@@@@@.@@@...@.@@..@@@..@@@@@@@@@..@@@@......@@@@.@@@..@@.@@@@@.@@@.@@@@.@.@@@.@@.@.@@@@@.@.@@@.@@@.@.@@@..@@
@@.@@.@.@.@@..@@@@@..@....@.@@@@@.@.@@.@...@@..@@..@@@.@.@..@@@@@@..@@.@@..@@@.@..@@@@@@@@@@@@@..@@..@@..@@@@@..@.@@.@@@.@@@@.@@@@@@@@.@@
@@.@@..@@@@@...@..@@@.@..@@@@..@@.@@@@.@@.@@..@@@@@...@...@@@..@@..@@@.@...@.@@@.@@..@.@...@@..@@..@@@@.@.@.@@@..@@@@@@@@@@@..@@@@@..@@@@
@@...@.@.@@@..@..@..@.@.@@@@.@..@@@.@@@.@@@.@@.@@@@@@..@@.@.@@@..@.@.@......@@.....@@@...@@.@@@@@...@@@@...@@@@@@@@@...@@..@@@.@.@@@@.@@@
@...@.@@@..@.@@@@.@@@..@@@@@..@@@@.@.@@@@@.@..@@@@@@@.@.@@@.@...@@@@..@@@@@@.@@@@@@@@@@.@@.@@@.@.@@@...@@.@.@.@.@@.@.@@@.@.@@@.@@@..@@.@.
.@@@@@..@@.@@@@@@@.@.@@...@@@.@@@...@@.@@@@@@@.@@@@@.@@@@@@@@@@@@@@@@.@.@@@@@...@@.@.@...@@.@@@@@.@@@.@.@...@@@.@@.@@..@@@@..@@.@@@.@@@.@
@@@.@@..@@....@@@.@@@@@.@.@.@...@@@..@@.@@@@..@@@@.@.@.....@@@@@@@@@@.@.@@.@.@@.@@@.@@.@@@.@.@.@@.@@@@@.@@@.@@@@@.@.@@..@.@.@@@@@@@@.@.@@
.@@@@.@@@@@...@@@@.@@..@.@.@@.@.@..@.@@.@......@.@...@@@@@@...@...@@@@@@@..@..@@.@@@...@.@@....@@...@@@..@@@@@@@.@.@@@@@@@@@@.@.@@@@@.@@@
.@.@@@@@@@@@.@@@@.@@.@...@@@...@.@@.@@@..@.@.@.@.@@.@.@@@@@@@@....@..@.@.......@@.@@..@@@.@..@@@@@@..@.@@@..@.@@@@@@@..@.@@@@@@@@@@.@.@.@
@@@@@@@@.@...@@@.@...@...@@..@@.@@.@@.@.@.@...@@@..@..@..@@@@@@@.@@.@@@@@@@@.....@@@@@@@@.@@@@@@@@..@@.@@@@@@@@@@@.@..@....@@..@@@@@@@@@@
@.@@@@@@....@..@@..@@.@@@..@..@@@.@@@..@@@@.@.@@@@..@@@@@@@@@@.@@@.@@.@@@....@@.@@@..@@@@@@.@....@@...@.@@.@@.@..@.@@@@@@@@@@@@.@@@@..@@@
@@@@.@@@@@..@.@.@....@.@@.@@@.@@...@.@.@@.@.@@.@.@.@@@..@@@@@@@.@.@..@.@@@@@@.@@.@.@.........@.@@@..@..@@@@@@@..@.@@@@.@@@@..@@@@@.@@.@@@
@@@@.@@@.@@@@..@@..@..@@@..@.@@@@.@@@.@@..@@@..@@.@@@@@@@@@@@.@.@@.@...@.@.@@.@@@.@@@@@.@@@..@.@@@....@@.@@@@@....@@.@@@.@@.@..@.@@.@@@@.
.@@...@.@.@@.@@@.@.@.@@@@@.@.@@@@@@@..@@@@.@.@@@@@@@@.@.@@@.@..@@@.@@@.@@@@.@...@.@@.@..@.@..@@.@@..@@.@.@.@..@@@.@@@@@.@@@@@.@@@@...@@@@
@@@@@@@@.@.@....@@@@@@@@@@@@.@@@@@@@@..@..@@.@@@...@.@@..@.@@@@@...@@@@.@@@@@@.@@@@.@@@..@.@.@@@@...@@@.@@@@.@.@@@@.@.@@@.@.@.@@@@@.@.@@.
.@.@@@@@.@@@@@@@@@..@@@@...@.@@@@@@..@@@@@@@@@.@@@@@@..@.@.@@.@.@@@@@.@..@.@@....@.@@..@..@@.@@@.@@@@.@.@@.@.@..@.@.@@@@@.@@@.@@.@.@@.@.@
.@@@@.@@.@@.@.@.@.@@@@@..@@.@@@@@@..@..@@.@.@@@@.@@.@.@.@@...@@.@@@@@.@...@...@...@@@@..@.@@@@@@@@.@@.@@@@@.@.@@@.@@..@.@@.@@.@.@@@@.@@@.
@@..@@.@@.@..@@.@@@..@@@@.@.@@@@.@@@.@@@@.@@@.@@@.@@@..@@...@@@@..@..@@@@@@.@@@.@..@..@@.@@@.@@@..@..@.@@@@..@.@@@@.@@@.@@..@@.@.@..@..@.
@@.@@.@...@@..@@@@@@..@@@@@..@@@@.@@@@@@@.@@.@.@@@...@@@@@@@.@@@@.@@@.@...@@@.@@@@@@@@@@@.@.@@..@.@@@@@..@@@@@@@@.@@@@@@@.@..@@.@.@@@@..@
@@@@@@@@@.@@@..@.@@.@@...@.@@@..@.@.@@@@...@@....@@@@@@@@@@@@.@@@.@@@.@@@.@@@.@.@@.@@@.@.@@@@@@@.@.@@..@..@@@.@@.@.@.@..@@@@@.@@@.@@.@@@.
@.@@@@.@@@@@@@..@.@@...@@.@@.@...@@....@.@..@@@...@.@@.@@.@@@.@.@@@.@@@..@.@@....@@@.@.@@@@@@@@@@@...@@@.@.@@@..@@.@@@@@@.@@@.@.@@@@.@@..
@.@@@@..@.@@...@@@.@@@..@.@.@@@.@@..@@.@@@@.@.@@.@...@@@@.@.@@@..@@.@@@@@@@..@.@@@@..@@@@.@@.@@.@@.@@.@@@@..@@.@@@.@@@@@@@....@.@@.@@@@@@
.@@@@...@.@.@@@.@@.@@@@@.@..@@.@@@@.....@@.@@..@.@@.@.@.@..@.@.@.@.@.@@...@.....@@@@@@.@@@@.@.@@@.@@@@@@..@@@@.@.@@..@..@@@@@@.@@@.@.@@@@
.@@@@.@@.@.@@......@.@.@@@@....@.@@.@.@.@@@@@@..@@@.@@....@@@@@.@@@.@@@@.@@@.@@@@.@@@@@@..@@..@@.@..@@.@.....@@@@@@@@.@@.@@@@@@@..@@@@@.@
@.@@..@@..@@....@@@@.@@@@@@@.@..@@@@@@...@@.@@@@@@@..@@.@.@.@@@.@@.@@@@@.@@...@@.@@@@..@...@..@@.@..@@..@@@.@@@@@.@@@@@@@@@.@@.@@@@@@@@.@
@@@.@@@@...@@.@@@..@@.@@@@@@@@@.@@@.@@@@@@@.@@@@@..@.@.@.@@@@@@..@@..@@.@.@@.@@..@.@@@@..@@@@@..@@@.@@@@@@.@@...@@..@..@@@...@@@.@@@@.@@@
.@@@@.@.@@....@.....@@.@@@@@@@@@@.@@.@@@@@@@@.@@@@.@@..@@@...@@@.@@@@@@@.@@..@..@@@.@@.@.@....@@@.@....@.@@@.@@@.@@@@.@@.@@@@.@.@.@@@.@@@
@@.@.@.@@.@@@@.@@..@.@.@.@..@@.@@@@...@@......@.@..@.@@.@@.@@@@.@@@@.@@@@..@.@@.@@@@@@....@.@@@@@.@@..@...@@.@@....@.@@.@@@.@@.@@.@@@@@@@
@.@..@@.@..@.@.@@@@@@.@.@.@.@@@.@@@@..@@@...@@@..@@@....@.@@.@@@@@@@@@@@...@@@.@@@..@@.@@@@@....@@@.@@.@@.@@@..@@@@@@@.@@@@..@.@@..@@@..@
.@@@@@...@..@@@.@...@@@@@@@@@.@.@@.@@@.@@@@@.@@.@.@@@@@@.@..@@.@@..@@@@@@....@.@@@@@@@@@@@.@@@@@@@...@@.@@@.@@@.@.@@@@@@.@.@..@.@@@@@.@.@
@@.@.@.@.@.@@....@@@.@@@@...@.@@.@@@@@@.@@@@@@.@@@.@..@@@@@@@@@@.@@@@@.@@@@@@.@@.@....@.@@...@..@@..@..@@@@.@@.@@@@.@...@.@.@@@@@.@..@@.@
...@@@@@.@@@@.@@.@@@@.@@.@@.@@.@@@.@.@@@@@...@@@@...@@@..@.@.@.@@@@@@@@.@@@@@@@@.@@@..@..@@..@.@.@...@..@@@.@@..@.@...@.@@@@@.@.@@..@.@@@
.@@@@@.@@@@@.@@@@.@.@@@@@@@..@@@@@@@@@@.@@@.@..@@..@.@.@@@@.@@.@.@@@.@@@.@@@@@@.@@@@.@....@@.@@@@@@@.@@@@@...@@@@.@@@@@@@@@@@@.@@.@@@.@@.
@.@@@...@..@..@@.@@.@@@...@@@@@@@@@..@@...@@..@@.@@@.@@.@@@@.@@...@@@@..@@.@@@@@......@@.@@@@@..@.@@.@@..@..@...@@...@@@.@..@@.@.@@@@.@@@
.@@.@@.@@@@@@@@@@.@@@.@.@@..@@@@@.@@@.@.@.@@.@@.@.@@@.@@@@@@@@.@@..@@...@..@@.@@.@.@@@@@@@@.@@.@.@@@.@.@@@@.@@..@@@@.@...@.@.@@..@@@..@..
@@...@@.@@@@.@@@.@.@.@..@@@@@.@@@@@..@@..@@@..@..@@.@@.@@.@.@@@@@@@.@.@.@@...@@.@...@@@...@@.@@@@@.@@..@@@@@@@.@@@@.@@@.@@@@@@..@@@@@@...
@.@...@@..@@@.@.@@@@@@@@..@@@@@@@@.@@@@..@@@.@@..@.@@@@@@.@@@@@@@@.@@@.@@@@@@..@@..@.@.@.@@@.@@@@@@.@...@@@.@@.@@@.@@@.@@@.@@@@.@@...@@@@
@..@.@@@..@@.@@@@@@.@@@.@@@...@@@@..@..@@@.@.@@@...@@@@@.@@@@@@@@.@@@@.@@@@.@..@@@@....@@.@@.@@.@...@@@.@@@...@.@.@@@@@@@@..@..@@@@@@@@@.
..@@.@@..@@.@@..@@@@.@.@.@@@@..@@@@@.@...@@@@.@@...@@.@@@@@@.@@..@.@@@..@..@.@@@.@.@@@...@@@@@@@@.@@@..@@.....@@.@@.@@@..@@...@@..@@....@
.@@@@.@@@..@@@.@@@......@@@..@@@@@@@@@@.....@@@.@@@@@@.@@.@@@..@@@@@@@@@.@..@.@.@..@@@.@@@.@...@.@.@.@.@@.@@@.@@@@@@@@@..@@@@..@@@@@@@@.@
@@.@@@....@.@.@@@@@@@.@@@.@@@@.@...@@.@.@@@@@.@.@.@.@.@@.@@..@@@@.@@@@@@@..@@@.@@.@@@@.@...@@.@.@@.@.@@.@@@.@@@@@@@.@@@.@..@@@@@@.....@.@
.@.@@@@.@@.@@.@.@.@@@@@@.@@@.@@.@..@@.@@@@..@@@.@@@@@.@@.@.@@.@@@@@@@@..@.@@@@@@@@@@.@.@@@@...@@.@@@@@.@@.@@@.@@.@@.@@@@.@@@@..@@...@@@@.

126
mark/day4/part1.rb Normal file
View File

@ -0,0 +1,126 @@
require 'debug'
# Generic FileHandler
class FileHandler
def initialize(file_path)
@file_path = file_path
end
# Usage:
# FileHandler.new('/path_to_file').read_lines do |line|
# # process line
# end
def read_lines
File.foreach(@file_path, chomp: true).map do |line|
yield(line)
end
end
# Usage:
# file_content = FileHandler.new('/path_to_file').read_file(
def read_file
File.read(@file_path)
end
end
class PuzzleSolver
def initialize(file_handler, debug: false)
@handler = file_handler
@debug = debug
end
def debug(message)
puts message if @debug
end
def print_debug(message)
print message if @debug
end
def solve
raise NotImplementedError, 'Please implement this method in subclasses.'
end
end
# Puzzle-specific
class Grid
attr_reader :cells, :num_rows, :num_columns
# @param [<<Integer>>] an array of arrays of of 1's and 0's e.g., [[1, 0], [0, 1]]
def initialize(grid)
raise "Invalid grid: 0 rows in #{grid}" if grid.length == 0
raise "Invalid grid: 0 columns in #{grid}" if grid[0].length == 0
raise "Invalid grid: inconsistent number of columns in #{grid}" unless grid.all? {|column| column.length == grid[0].length}
@num_rows = grid.length
@num_columns = grid[0].length
@cells = grid
end
# @return [Grid]
def self.from_file_handler(handler)
grid = []
handler.read_lines do |line|
next if line.nil? || line.empty?
grid << line.split('').map {|cell| cell == '@' ? 1 : 0}
end
Grid.new(grid)
end
# @return [<[Integer, Integer]>] list of coordinates of adjacent cells of [x,y].
def adjacent_coordinates(x, y)
adjacent_cells = [-1, 0, 1].flat_map do |x_offset|
[-1, 0, 1].map do |y_offset|
[x + x_offset, y + y_offset]
end
end
adjacent_cells.delete([x, y])
# only return cells on the grid
adjacent_cells.select do |cell|
0 <= cell[0] && cell[0] < @num_rows &&
0 <= cell[1] && cell[1] < @num_columns
end
end
def adjacent_cell_values(x, y)
adjacent_coordinates = adjacent_coordinates(x, y)
adjacent_cell_values = adjacent_coordinates.map {|x, y| @cells[x][y]}
adjacent_cell_values
end
# @return [Integer] the number of rolls in the adjacent positions of cell (x, y)
def num_adjacent_rolls(x, y)
adjacent_cell_values = adjacent_cell_values(x, y)
adjacent_cell_values.sum
end
end
class GridSolver < PuzzleSolver
def solve
grid = Grid.from_file_handler(@handler)
num_accessible_rolls = 0
(0...grid.num_rows).each do |x|
(0...grid.num_columns).each do |y|
num_accessible_rolls += 1 if grid.num_adjacent_rolls(x, y) < 4 and grid.cells[x][y] == 1
end
end
num_accessible_rolls
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby part1.rb <file_name> [debug]"
exit 1
end
file_path = ARGV[0]
debug = (ARGV[1] == "debug")
file_handler = FileHandler.new(file_path)
## Puzzle-specific
cls = GridSolver
##
puts "The answer is: #{cls.new(file_handler, debug:).solve}"

152
mark/day4/part2.rb Normal file
View File

@ -0,0 +1,152 @@
require 'debug'
# Generic FileHandler
class FileHandler
def initialize(file_path)
@file_path = file_path
end
# Usage:
# FileHandler.new('/path_to_file').read_lines do |line|
# # process line
# end
def read_lines
File.foreach(@file_path, chomp: true).map do |line|
yield(line)
end
end
# Usage:
# file_content = FileHandler.new('/path_to_file').read_file(
def read_file
File.read(@file_path)
end
end
class PuzzleSolver
def initialize(file_handler, debug: false)
@handler = file_handler
@debug = debug
end
def debug(message)
puts message if @debug
end
def print_debug(message)
print message if @debug
end
def solve
raise NotImplementedError, 'Please implement this method in subclasses.'
end
end
# Puzzle-specific
class Grid
attr_reader :cells, :num_rows, :num_columns
# @param [<<Integer>>] an array of arrays of of 1's and 0's e.g., [[1, 0], [0, 1]]
def initialize(grid)
raise "Invalid grid: 0 rows in #{grid}" if grid.length == 0
raise "Invalid grid: 0 columns in #{grid}" if grid[0].length == 0
raise "Invalid grid: inconsistent number of columns in #{grid}" unless grid.all? {|column| column.length == grid[0].length}
@num_rows = grid.length
@num_columns = grid[0].length
@cells = grid
end
# @return [Grid]
def self.from_file_handler(handler)
grid = []
handler.read_lines do |line|
next if line.nil? || line.empty?
grid << line.split('').map {|cell| cell == '@' ? 1 : 0}
end
Grid.new(grid)
end
# @return [<[Integer, Integer]>] list of coordinates of adjacent cells of [x,y].
def adjacent_coordinates(x, y)
adjacent_cells = [-1, 0, 1].flat_map do |x_offset|
[-1, 0, 1].map do |y_offset|
[x + x_offset, y + y_offset]
end
end
adjacent_cells.delete([x, y])
# only return cells on the grid
adjacent_cells.select do |cell|
0 <= cell[0] && cell[0] < @num_rows &&
0 <= cell[1] && cell[1] < @num_columns
end
end
def adjacent_cell_values(x, y)
adjacent_coordinates = adjacent_coordinates(x, y)
adjacent_cell_values = adjacent_coordinates.map {|x, y| @cells[x][y]}
adjacent_cell_values
end
# @return [Integer] the number of rolls in the adjacent positions of cell (x, y)
def num_adjacent_rolls(x, y)
adjacent_cell_values = adjacent_cell_values(x, y)
adjacent_cell_values.sum
end
def removable_roll_coords
cells = []
(0...@num_rows).each do |x|
(0...@num_columns).each do |y|
cells << [x, y] if num_adjacent_rolls(x, y) < 4 and @cells[x][y] == 1
end
end
cells
end
def remove_rolls(coords)
coords.each do |x, y|
puts "Warning: No roll in cell (#{x},#{y})" if @cells[x][y] == 0
@cells[x][y] = 0
end
end
end
class GridSolver < PuzzleSolver
def solve
grid = Grid.from_file_handler(@handler)
total_rolls_removed = 0
removable_roll_coords = grid.removable_roll_coords
while removable_roll_coords.length != 0
total_rolls_removed += removable_roll_coords.length
grid.remove_rolls(removable_roll_coords)
removable_roll_coords = grid.removable_roll_coords
end
total_rolls_removed
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby #{__FILE__} <file_name> [debug]"
exit 1
end
file_path = ARGV[0]
debug = (ARGV[1] == "debug")
file_handler = FileHandler.new(file_path)
## Puzzle-specific
cls = GridSolver
##
puts "The answer is: #{cls.new(file_handler, debug:).solve}"
# The answer is: 9206
# real 0m8,510s
# user 0m8,450s
# sys 0m0,059s

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

@ -0,0 +1,11 @@
3-5
10-14
16-20
12-18
1
5
8
11
17
32

1175
mark/day5/input.txt Normal file

File diff suppressed because it is too large Load Diff

102
mark/day5/part1.rb Normal file
View File

@ -0,0 +1,102 @@
require 'debug'
# Generic FileHandler
class FileHandler
def initialize(file_path)
@file_path = file_path
end
# Usage:
# FileHandler.new('/path_to_file').read_lines do |line|
# # process line
# end
def read_lines
File.foreach(@file_path, chomp: true).map do |line|
yield(line)
end
end
# Usage:
# file_content = FileHandler.new('/path_to_file').read_file(
def read_file
File.read(@file_path, chomp: true)
end
end
class PuzzleSolver
def initialize(file_handler, debug: false)
@handler = file_handler
@debug = debug
end
def debug(message)
puts message if @debug
end
def print_debug(message)
print message if @debug
end
def solve
raise NotImplementedError, 'Please implement this method in subclasses.'
end
end
class FreshnessDatabase
# @param [<Range>] list of ranges of fresh IDs
def initialize(fresh_ranges)
@fresh_ranges = fresh_ranges
end
def fresh_id?(id)
@fresh_ranges.any? {|range| range.include?(id)}
end
end
class Solver < PuzzleSolver
# @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
# @param [String] full input e.g., "11-22\n33-604\n..."
# @return [<Range>] e.g., [11..22, 33..604, ...]
def parse_ranges(input)
input.split("\n").map {|range| to_range(range)}
end
# @param [String] ids_str e.g., "1\n5\n8\n11\n17\n32"
# @return [<Integer>] e.g., [1, 5, 8, 11, 17, 32]
def parse_ids(ids_str)
ids_str.split("\n").map(&:to_i)
end
def solve
two_parts = @handler.read_file.split("\n\n")
raise "Invalid file format: #{two_parts.inpsect}" if two_parts.length != 2
db = FreshnessDatabase.new(parse_ranges(two_parts[0]))
ids = parse_ids(two_parts[1])
fresh_ids = ids.select {|id| db.fresh_id?(id)}
fresh_ids.length
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby #{__FILE__} <file_name> [debug]"
exit 1
end
file_path = ARGV[0]
debug = (ARGV[1] == "debug")
file_handler = FileHandler.new(file_path)
## Puzzle-specific
cls = Solver
##
puts "The answer is: #{cls.new(file_handler, debug:).solve}"

198
mark/day5/part2.rb Normal file
View File

@ -0,0 +1,198 @@
require 'debug'
# Generic FileHandler
class FileHandler
def initialize(file_path)
@file_path = file_path
end
# Usage:
# FileHandler.new('/path_to_file').read_lines do |line|
# # process line
# end
def read_lines
File.foreach(@file_path, chomp: true).map do |line|
yield(line)
end
end
# Usage:
# file_content = FileHandler.new('/path_to_file').read_file(
def read_file
File.read(@file_path, chomp: true)
end
end
class PuzzleSolver
def initialize(file_handler, debug: false)
@handler = file_handler
@debug = debug
end
def debug(message)
puts message if @debug
end
def print_debug(message)
print message if @debug
end
def solve
raise NotImplementedError, 'Please implement this method in subclasses.'
end
end
class FreshnessDatabase
# @param [<Range>] list of ranges of fresh IDs
def initialize(fresh_ranges)
@fresh_ranges = fresh_ranges
end
def fresh_id?(id)
@fresh_ranges.any? {|range| range.include?(id)}
end
def parsed_ranges
all_ranges = []
current_min = Float::INFINITY
current_max = -Float::INFINITY
@fresh_ranges.each_with_index do |range, index|
if range.max < current_min
all_ranges.insert(0, range)
current_min = range.min
current_max = range.max if range.max > current_max
next
end
if range.min > current_max
all_ranges << range
current_max = range.max
current_min = range.min if range.min < current_min
next
end
overlapping_range_indices, closest_range_index = FreshnessDatabase.find_all_overlapping_range_indices(all_ranges, range)
if overlapping_range_indices.empty?
all_ranges.insert(closest_range_index + 1, range)
elsif overlapping_range_indices.length == 1
overlap_index = overlapping_range_indices[0]
new_range = FreshnessDatabase.max_range([all_ranges[overlap_index], range])
all_ranges[overlap_index] = new_range
range = new_range
else
min_index = overlapping_range_indices[0]
max_index = overlapping_range_indices[overlapping_range_indices.length - 1]
min_range = all_ranges[min_index]
max_range = all_ranges[max_index]
new_range = FreshnessDatabase.max_range([min_range, range, max_range])
new_ranges = []
all_ranges.each_index do |i|
if i < min_index
new_ranges << all_ranges[i]
elsif i == min_index
new_ranges << new_range
elsif min_index < i && i <= max_index
# do nothing
else
new_ranges << all_ranges[i]
end
end
all_ranges = new_ranges
range = new_range
end
current_min = range.min if range.min < current_min
current_max = range.max if range.max > current_max
end
all_ranges
end
def num_fresh_ids
ranges = parsed_ranges
puts ranges.inspect
num_ids = ranges.map {|range| range.size}.sum
num_ids
end
private
def self.overlapping_ranges?(range1, range2)
(range1.min <= range2.min && range2.min <= range1.max) ||
(range1.min <= range2.max && range2.min <= range1.max)
end
def self.max_range(ranges_array)
(0...ranges_array.length - 1).each do |index|
raise "Non overlapping ranges found on #{index}: #{ranges_array}" if !self.overlapping_ranges?(ranges_array[index], ranges_array[index+1])
end
bounds = ranges_array.flat_map {|range| [range.min, range.max]}
Range.new(bounds.min, bounds.max)
end
def self.find_all_overlapping_range_indices(all_ranges, range)
# NOTE: we assume `range` is between smallest and largest range, as those cases are handles separately in main loop
raise "Error unmet assumption: #{all_ranges}" if all_ranges.length < 2
indices = []
closest_range_index = 0 # used in case there is no overlap
all_ranges.each_with_index do |possible_range, index|
if self.overlapping_ranges?(possible_range, range)
indices << index
else
closest_range_index = index if range.min > possible_range.max
end
end
[indices, closest_range_index]
end
end
class Solver < PuzzleSolver
# @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
# @param [String] full input e.g., "11-22\n33-604\n..."
# @return [<Range>] e.g., [11..22, 33..604, ...]
def parse_ranges(input)
input.split("\n").map {|range| to_range(range)}
end
# @param [String] ids_str e.g., "1\n5\n8\n11\n17\n32"
# @return [<Integer>] e.g., [1, 5, 8, 11, 17, 32]
def parse_ids(ids_str)
ids_str.split("\n").map(&:to_i)
end
def solve
two_parts = @handler.read_file.split("\n\n")
raise "Invalid file format: #{two_parts.inpsect}" if two_parts.length != 2
db = FreshnessDatabase.new(parse_ranges(two_parts[0]))
db.num_fresh_ids
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby #{__FILE__} <file_name> [debug]"
exit 1
end
file_path = ARGV[0]
debug = (ARGV[1] == "debug")
file_handler = FileHandler.new(file_path)
## Puzzle-specific
cls = Solver
##
puts "The answer is: #{cls.new(file_handler, debug:).solve}"

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

@ -0,0 +1,4 @@
123 328 51 64
45 64 387 23
6 98 215 314
* + * +

5
mark/day6/input.txt Normal file
View File

@ -0,0 +1,5 @@
55 93 5432 82 6 699 498 878 2 54 5 14 115 683 213 338 85 377 39 738 17 5928 89 3985 79 556 87 69 82 334 857 5 96 8 19 4 93 36 19 25 245 398 8 824 17 8 55 4596 772 76 786 79 274 1 261 212 8186 49 1 299 75 94 15 96 7176 678 2 91 55 721 767 87 2 9557 234 8 44 13 7137 1 214 6586 698 176 55 18 18 7146 58 715 28 555 47 4447 116 4 33 614 9 89 1 56 495 11 22 48 1283 5 6546 825 52 35 473 25 573 528 21 7 496 79 52 787 39 5 37 41 4 48 829 3746 364 639 4545 46 4 21 556 38 9 951 24 3557 156 45 551 2 41 329 24 2379 4 91 6 5 92 35 33 39 481 956 65 92 829 14 334 666 645 93 3 56 81 97 425 749 7 7 27 943 7 365 11 5 444 69 855 356 45 8 25 3 4927 27 86 632 8 842 9 432 3 928 7616 71 66 66 75 92 1113 468 28 871 535 4 43 89 446 1 8 28 878 2 383 6 95 37 21 77 67 83 54 32 75 1944 46 4 143 528 675 27 56 9 37 79 7 12 89 91 4665 81 447 496 7 6567 36 3241 95 71 4657 6 25 19 26 97 16 23 81 76 481 37 263 94 1612 8 483 57 8 1 3464 2 632 82 81 143 95 471 4484 86 4 739 289 7 25 86 3 36 58 2899 1421 7755 68 2 532 69 79 6 1438 529 36 9 6365 15 2838 7 63 984 73 443 9169 38 9 93 515 954 6 476 31 38 1 891 196 87 26 45 352 435 34 8 27 32 4 18 57 6 1496 854 866 876 534 5 593 66 4 2 6 934 1 89 34 81 53 47 48 152 5973 94 631 44 12 218 2778 3521 25 1 8 89 523 72 97 86 7 674 7 378 32 5 76 42 5482 3134 2 26 94 638 82 77 816 382 255 84 33 4463 6 3 939 765 47 65 99 22 745 851 85 71 53 6 9 28 3 8 36 67 86 48 68 71 27 22 2 364 1 623 6692 45 34 694 95 3 17 41 48 3554 8 76 9 87 45 81 75 32 73 8 542 74 91 66 3 78 47 718 12 7467 3848 38 987 12 7 31 429 419 4 645 8 29 978 53 87 997 2646 19 4232 47 7654 379 819 73 5851 153 31 86 4 9 1 9137 41 828 89 393 34 59 3979 156 59 45 988 64 172 37 36 31 43 76 7 91 9 9 2167 7 3782 477 435 58 44 2 1 4367 547 865 6 6 19 1 957 88 69 211 24 81 5116 2 174 983 7 9 233 85 134 8 794 2291 896 111 183 91 91 44 888 89 67 4 14 5538 89 766 8 75 1195 6419 486 6817 133 19 52 9 18 26 5 92 8 4 8 1 173 2865 537 5 86 91 291 8 53 46 8841 74 221 74 226 2939 75 95 7 8 138 296 17 63 33 571 21 17 555 1932 5619 5734 813 437 66 562 1 1 34 25 899 963 8 6 592 56 48 8 34 1 73 185 11 6268 367 479 32 888 44 589 7494 32 24 16 75 4 74 851 9 8798 74 9 33 27 25 6952 42 925 181 2 8932 8 882 87 14 87 954 6 38 3 3 98 172 7 1 348 57 57 389 221 3 7 788 8 1725 888 35 9195 6451 7623 62 88 25 113 388 159 595 19 23 867 1139 188 4 4871 22 18 87 81 515 53 9 6 494 19 481 38 95 144 54 52 892 4479 92 75 2 5 23 89 6768 8411 68 6 96 91 478 51 3132 747 698 532 29 15 429 2393 467 365 1 71 1 19 12 5 868 1279 6 2685 17 37 75 384 992 4 3 93 17 175 72 243 448 45 4 5 86 285 11 48 237 78 47 2 487 8461 971 5968 57 3 76 99 37 412 541 144 9 8 11 637 55 2 1 123 92 519 8 7 13 831 486 8 4 46 55 33 3 97 96 779 62 672 4 8475 19 9 4333 96 563 77 71 7 355 93 61 18 7 31 987 78 63 78 979 5127 282 4 38 1 43 2 87 7 59 87 3 882 628 717 65 2 22 6712 76 3 57 369 1 62 35 571 3577 6 24 79 32 2 8 38 448 35 8694 5 785 32 473 3453 68 164 7137 99 347 14 875 61 72 52 6 71 97 7 85 89 635 67 56 2 164 85 48 5449 22 97 515 7 1917 84 21 834 84 361 64 3 1 97 1 592 52 92 53 8955 24 68 95 251 32 8 92 79 16 58 8612 28 4 8 269 55 84 4 26 24 84 528 665 6528 67 23 92 69 18 46 653 99 646 723 771 41 76 98 92 869 7547 69 67 1586 6 21 8 83 12 172 3 11 127 94 135 89 513 7 916 61 89 65 39 1 713 583 72 5298 87 3587 33 8669 6 64
32 71 8361 68 867 938 225 825 3 56 629 48 846 772 637 299 23 438 23 443 12 6462 68 7419 37 175 48 92 93 634 437 4 464 133 38 65 21 83 886 38 465 292 94 648 35 46 13 2893 314 95 111 611 553 3 515 216 6819 36 64 31 74 43 591 43 6896 331 75 18 76 684 837 42 34 9513 26 69 44 25 6883 5 376 1843 888 829 57 8 78 9332 13 191 51 82 78 3497 9575 68 64 428 89 566 5 55 346 78 1 55 7358 2 4577 984 288 71 452 28 788 315 14 653 311 155 22 28 39 48 336 623 41 69 167 2227 151 336 368 95 32 23 664 12 263 517 69 8425 451 74 937 4 53 462 22 444 31 55 1 849 87 748 748 54 191 651 64 95 264 99 473 354 25 86 8 59 22 61 778 284 25 96 69 822 39 771 18 6 771 53 11 297 179 28 64 14 3195 46 65 571 56 119 2 714 27 364 7653 47 12 51 512 51 727 424 939 267 697 49 31 38 794 245 67 26 668 86 444 62 111 45 2297 75 93 916 184 36 83 351 61 56 492 935 411 99 22 6 93 76 3 59 798 56 5234 61 764 943 79 4484 18 9442 25 19 8427 243 113 51 73 17 88 26 63 48 925 19 125 11 331 73 47 6 2262 37 475 17 869 236 97 624 94 514 1892 11 7 197 782 86 414 51 7 771 98 5473 151 957 54 33 378 455 857 8562 5921 755 56 1 6435 639 6311 96 7537 137 396 759 6715 45 49 23 977 553 3 196 19 41 62 386 447 64 88 8138 858 182 73 55 47 681 84 244 74 9 665 159 623 765 115 72 44 339 352 57 5 233 48 47 47 67 28 82 15 81 5159 78 155 22 754 583 7249 6817 59 5 35 61 927 15 83 15 9 592 77 984 74 4 48 99 914 8961 3 91 236 271 96 62 995 362 654 65 16 4411 9 8 448 363 96 82 243 7 116 965 98 612 7956 82 7 849 5 25 37 73 11 21 59 175 71 91 77 215 38 11 5732 64 172 528 764 36 39 486 26 483 43 83 62 924 23 59 16 856 99 12 577 36 496 971 4 763 65 159 1768 5982 333 57 364 13 4 73 257 854 69 21 2 182 131 39 333 474 6261 79 9397 77 727 872 588 59 2647 79 945 393 37 77 81 5369 81 13 61 444 34 23 3327 523 426 69 825 87 942 37 885 184 84 82 91 89 2 1 8177 7 5931 393 576 51 18 18 3 5363 148 791 55 9 86 4 748 2 96 441 56 38 4879 24 343 232 28 95 529 79 684 5 669 4442 785 225 753 93 33 94 488 628 68 42 491 7613 17 37 8 22 3578 1484 879 7485 226 76 443 5 24 48 56 95 85 87 91 7 36 1763 855 46 866 46 667 55 11 65 122 26 513 22 627 931 52 4 96 4 626 888 73 24 1 125 85 63 233 3713 342 4258 197 822 73 473 784 9 668 866 126 769 91 8 468 538 55 1 48 16 19 434 98 1897 332 877 29 512 28 267 696 75 43 48 42 46 8 857 467 7672 57 17 49 99 685 2148 84 454 445 75 5674 6 237 927 13 928 515 281 62 959 7 75 266 8 8 423 964 89 877 798 2 48 836 85 4959 976 64 9354 5141 144 151 25 62 491 151 338 395 97 867 64 783 645 4 6233 27 77 85 65 567 236 8 729 16 759 764 6 39 586 5 35 792 8582 93 58 77 3255 61 757 5789 5449 2749 988 88 33 546 25 3968 112 56 218 29 2 855 1719 373 553 15 19 412 42 22 54 657 8791 4 729 76 36 861 617 99 6 9 99 35 649 71 67 8412 16 15 71 36 86 924 42 356 86 767 58 48 8641 585 9112 68 72 8 41 69 769 646 998 5 75 99 84 6576 249 89 693 5 954 52 34 15 377 533 5 1 54 14 87 2 17 52 81 32 543 934 2847 55 243 4352 11 754 154 52 55 477 67 913 31 15 329 947 792 89 62 576 4633 999 9 681 61 28 4 52 44 38 79 5999 145 36 46 1431 5 48 3665 627 9 56 573 981 5423 46 257 229 1 74 14 38 7 32 46 995 31 5633 945 476 81 322 9917 62 446 8456 217 826 97 229 125 924 58 11 878 57 35 89 28 651 5285 56 46 431 39 671 8321 65 39 8519 283 5727 62 62 231 874 385 4 873 5 272 18 394 85 47 68 9448 521 743 37 299 66 67 78 14 386 56 762 27 4 1 948 898 15 36 52 165 794 783 884 286 19 45 692 43 59 98 925 24 67 533 982 27 84 2 31 798 9133 58 44 8531 6 93 28 567 64 947 3 413 838 62 351 11 486 37 291 911 87 87 15 41 328 717 44 9386 46 8589 66 4282 82 28
38 61 15 15 119 42 297 861 94 23 171 883 332 94 884 231 54 965 3 489 74 5188 575 266 96 53 44 835 1351 544 65 64 792 513 12 546 11 64 165 698 561 856 74 815 65 94 53 8358 39 95 165 565 823 412 538 47 415 287 192 15 15 981 272 95 62 256 55 6 77 995 546 39 43 2732 24 187 23 7 265 85 98 8165 495 87 5 6 21 9695 21 233 72 48 24 1398 3795 15 6 763 35 564 919 8 51 99 7 84 122 82 12 63 265 61 5477 88 575 324 1 892 9331 546 76 13 96 47 238 825 66 81 776 6454 77 496 928 1 59 34 321 7 262 27 9 6827 838 71 4 151 31 59 45 348 922 19 4 885 32 6833 154 2 552 751 952 66 456 759 572 688 9 81 476 92 78 87 612 1685 82 95 377 497 31 139 23 5 976 58 8 11 229 9169 89 37 762 61 58 698 72 241 197 592 42 486 379 68 24 84 272 98 98 15 765 718 772 82 44 49 235 535 62 52 312 92 19 38 5439 52 8976 57 18 355 1113 3 23 652 11 494 781 148 713 38 27 428 78 28 6 6 5863 29 4914 96 658 39 22 285 634 3357 26 4 8718 772 469 61 83 81 74 9655 456 9 97 767 932 49 81 6343 3 3 9129 51 55 53 264 929 68 971 5 612 741 4 75 688 825 21 537 243 16 848 41 8273 346 46 92 74 681 995 365 6724 3478 328 45 41 563 2141 667 47 3645 799 715 923 9527 54 4374 94 534 769 9 14 38 89 415 137 596 16 9 2619 963 61 13 97 979 322 77 351 63 72 533 995 361 86 934 11 41 5396 336 59 74 755 85 35 31 82 76 6 74 16 6725 73 37 538 2953 751 8634 67 24 78 17 74 49 65 18 82 35 2552 96 343 66 42 21 6619 38 797 1 18 813 33 89 13 868 935 6197 17 654 996 58 26 987 29 51 8467 858 2 353 478 98 384 9755 9551 77 547 38 713 15 87 5 5678 2 4485 631 65 53 724 87 41 3986 42 528 439 5119 98 18 3597 51 818 75 98 876 518 93 6364 39 262 6883 73 848 92 321 862 678 557 4 313 2413 622 821 73 781 38 6 58 693 466 75 22 898 149 27 77 493 838 969 32 1916 61 54 323 51 35 5143 63 1745 113 243 65 71 4853 56 26 32 847 44 16 2129 232 116 4 326 99 223 96 725 272 86 99 38 27 51 26 4681 64 7573 798 667 48 17 69 44 769 121 969 35 65 16 6 68 1 23 211 5 17 6824 12 14 5576 45 81 582 88 18 2 972 4497 347 273 933 25 46 56 74 952 22 81 484 9832 18 7 226 154 7282 6121 898 3448 245 894 3422 93 4568 53 164 78 18 69 65 9 15 6511 68 51 557 66 62 75 85 95 612 13 839 61 12 958 53 8 32 87 931 229 95 4 6 67 66 38 85 61 79 4758 697 78 396 487 7169 29 717 168 789 191 46 63 2 761 37 51 58 13 31 297 33 3445 2522 577 43 621 69 726 539 63 72 33 96 255 3 774 486 138 9 57 23 68 275 2823 99 842 893 59 377 7 576 623 367 3293 89 2728 57 976 17 86 919 36 82 126 333 49 119 423 79 731 311 79 1374 858 96 7795 8163 87 546 84 99 22 52 531 27 63 716 12 745 247 59 1524 99 56 25 98 35 155 17 238 14 7731 37 5 8 89 1 31 72 5633 13 23 98 9797 88 726 6955 596 2139 895 95 55 396 43 4844 69 32 14 28 9 82 5733 774 331 818 19 554 95 72 728 946 42 62 524 13 71 752 465 53 44 9 98 84 829 47 52 7569 25 44 253 59 57 638 63 952 97 554 54 1 941 933 326 49 279 5 64 25 117 88 597 55 586 47 1 5391 314 94 44 5 992 82 9562 52 295 474 9 76 77 725 81 95 51 32 89 23 44 419 2324 13 691 634 26 675 339 23 265 6528 5 829 1 466 442 6 6167 87 79 958 1242 597 36 687 792 31 21 59 887 75 52 1217 591 3 2 6177 756 22 9443 621 11 85 494 664 5494 35 197 71 2 63 99 73 73 9414 41 135 89 988 211 944 26 85 57 46 763 9118 395 757 52 252 785 743 14 939 136 21 65 11 22 846 3414 46 348 35 62 249 2529 76 62 8429 899 878 74 679 354 252 87 5 588 74 172 224 91 34 27 6 3231 885 339 52 447 39 83 41 5 768 94 24 4 97 4 874 719 16 28 74 471 281 588 849 395 924 23 529 476 8 63 318 81 28 948 688 12 216 4 88 693 7758 5 31 394 21 43 72 324 7 35 98 815 873 19 576 55 78 13 377 598 55 494 18 89 42 995 11 738 6 31 92 179 87 97
99 15 7 75 286 7 455 956 624 4 364 935 343 2 725 64 75 29 8 52 83 4886 313 74 45 9 37 8153 1982 33 84 31 627 755 46 997 2 86 199 593 1789 81 32 357 1 96 6 521 1 46 197 441 84 951 925 52 45 986 8837 86 8 692 242 5 41 58 31 9 39 34 81 84 84 7636 1 745 3 5 14 72 9 94 56 7 1 2 84 638 96 47 96 68 34 2691 4191 24 2 14 76 498 921 5 9 3 6 78 367 291 36 93 597 26 6778 29 745 774 4 368 2461 332 34 71 41 16 184 142 58 12 893 42 91 541 81 4 32 64 58 8 164 81 5 12 581 55 8 268 87 7 6 963 959 4 83 441 389 7181 921 3 88 512 8989 34 24 638 344 737 5 24 574 37 83 4 746 8794 62 68 727 931 62 34 96 47 22 4 2 45 865 1553 27 28 885 7 96 369 57 228 722 416 97 3 625 17 47 76 794 45 3 2 241 33 976 27 4 35 74 677 23 18 14 58 4 42 7235 84 5125 17 43 167 3742 9 3 463 62 848 89 44 891 114 67 879 58 34 38 7 8453 71 85 11 96 88 37 94 543 66 12 1 68 428 1825 6 6 34 5 3149 678 3 2 761 7 37 5 7391 4 6 9686 64 33 24 787 9598 98 91 3 298 995 7 78 95 684 16 282 398 291 481 13 897 747 43 2 38 192 3394 615 9786 48 19 33 75 76 1228 54 64 1224 598 8479 849 36 65 1355 54 56 778 42 59 328 79 285 627 915 99 1 3959 57 2 27 92 129 612 72 783 6 37 48 929 992 7 948 71 56 3813 996 57 68 49 63 99 73 73 86 6 1 53 3541 48 63 252 7953 982 972 44 75 16 35 67 78 28 8 65 97 1917 44 19 13 86 96 9813 4 58 29 68 967 6 78 8 549 9 6874 51 771 461 43 26 967 91 26 4983 381 8 593 889 9 614 2639 1252 19 227 59 537 5 48 7 9199 2 1572 767 55 15 36 818 47 89 71 377 295 5766 37 86 8998 32 51 47 4 647 415 64 4428 4 249 2635 28 66 2 124 748 314 996 6 756 3146 388 16 88 314 3 66 57 569 9 96 64 142 155 3 377 718 665 48 94 871 29 5 356 9 2 7795 34 4357 457 413 38 71 264 14 88 25 914 96 52 472 8 433 2 221 67 7 62 998 577 91 8 36 47 89 91 19 73 528 711 16 56 61 55 64 2 148 562 81 13 91 64 6 2 33 73 4 18 3633 36 6 1244 15 28 116 7 25 73 54 28 27 2 237 94 33 28 8 548 9 196 447 34 38 2 313 514 921 4522 13 2671 16 732 7985 65 2387 7 967 76 91 48 493 87 62 5618 2 33 4629 2 8 82 95 94 31 92 98 31 74 576 83 9 88 17 371 453 94 7 9 4 43 63 9 57 9 377 176 28 912 51 9584 377 352 637 75 11 26 71 2 369 46 73 4 977 6 62 31 7633 3718 395 12 438 59 464 55 5 35 76 5 393 4 24 242 345 3 81 29 3 872 461 22 63 21 47 23 62 9 866 274 6896 47 1219 51 459 41 5 152 69 83 15 115 726 614 4 57 395 561 91 6328 131 81 4488 4116 29 557 8 63 31 11 22 89 41 924 47 89 54 24 593 74 25 83 49 8 826 22 193 85 9885 46 9 8 8 1 74 5 821 4 52 33 6131 45 266 898 38 3873 933 6 67 416 86 5769 3 6 93 82 9 7 412 24 99 444 14 212 17 65 9352 2 65 53 8 23 8 512 486 12 79 39 19 62 276 29 75 8335 92 58 7279 51 64 521 67 29 62 386 43 7 53 3 25 6 432 1 18 39 51 35 438 814 581 91 8 7287 919 36 14 9 66 22 3733 81 935 15 85 43 44 261 89 84 15 85 45 79 9 7777 7425 49 324 644 82 624 8345 39 621 1632 3 513 6 997 154 6 3369 54 96 3547 494 12 58 367 127 1 85 98 374 76 75 3471 4 7 8 3861 756 4 52 556 27 14 55 244 4334 32 96 9 62 8 52 79 94 6219 25 157 98 6 515 13 71 51 8 1 342 74 914 1 6 727 163 165 9 6434 477 344 422 2 91 85 4278 9 861 2 54 955 7378 9 35 2115 821 436 31 949 848 515 72 1 517 31 5668 235 13 94 69 2 2646 127 274 66 27 5891 52 3 8 737 26 77 9 15 92 338 834 69 21 22 752 842 579 926 23 874 34 6273 781 5 37 48 868 33 887 68 39 197 6 69 348 76 9 84 16 78 92 93 233 5 49 19 872 279 13 548 9 6 82 412 2441 76 561 4 53 3 124 91 5 5 5 99 93 78 7
* * + * * + + * + + * + * * * * * * * + + + + + + * + + + + + * * + + * + * * * + * * * * * + + * * + * + * * * + * + * * + * * + * + * * + * + * + * * * * + + + + * * + * * + * * * + + + + + * * * * + + + * * * + + + * * + + + * + + + + + + * + * * * * + * + * * + * * * + * * * * + + * + * + * * + + + + * * + + * + + + + * + * * * + + * * + * + * * * * * * * * * * * + * + + * + + * + * + * * * * + + * * * + + * * + + * + * * * * + * * * * + * + + * + + * * + + * * + + + + + + + + * + * + + * * * + + + * * + * + * + * + + + * * + * + + + + + + + + * + + * + + + + + * + + * + * * * * + + + * + * + + + + + * * + + + * + + + * + * + * * * + * * * * * + + + + * + + * * * + + + + + * + + + * * + * + + * * + * + * + * * + * * * + + + + * * + * * + + + + + + + * * * + + + * * + * * * * * + + + + + + * * * + * + + * + * + + + * * + * + * + + + * * + * + * + * * * + * + + * + + * * * * + * + + * + + + + + * * * + + + * + * + * + + + * * * + * * * + * + + + + * * + * + * + + * + * + + * * * + + * * + * * * + + + * * * * * + * + * * + + * + + * * + + + * + * * * + + + * * + * + + + + * * + * * * * * * + * * + + + * * * * + + * + + + + + + * + * * * * * * + * * + + + * * * + + + * * + + + * * * * * + * + + + * + + + * * * * + * * * * * + * * + * * + * * * * + + * + * * * + + * + * * + + + + * * * + * + + * + + + * * * * + * + + * * * * * * * * + * * + + * + + * + + + + + + * + + * + * + * + * * + + * * + + + + + + + + + * * * * * + * + * + * * + + + * * * * + + * + + * * * + * + * * + + + + + + * + + * + * * * * + + + * + + * + + + * + * + * * + + + * + + + + + * + * * * * * * + * * * * * * + + * * * * * * + + * + + * + + + * + + * * + + * + * * + * + + + + + + + + + + + * * + * + * + * * * + + * + * * + * * + * * + + + + * + + + * + + * + * * + + + + * * + * * * * + * + + * * + + * + + * * + * + + + + * * * * * * * + + * * * * * + * + * * + + * * * + + * * * * * + * * * * * * + + * + + * * + * * + * * + * * * + * * + * + * + + + * * + + + * * * * + + * * * * + * + + + + + * *

77
mark/day6/part1.rb Normal file
View File

@ -0,0 +1,77 @@
require 'debug'
# Generic FileHandler
class FileHandler
def initialize(file_path)
@file_path = file_path
end
# Usage:
# FileHandler.new('/path_to_file').read_lines do |line|
# # process line
# end
def read_lines
File.foreach(@file_path, chomp: true).map do |line|
yield(line)
end
end
# Usage:
# file_content = FileHandler.new('/path_to_file').read_file(
def read_file
File.read(@file_path)
end
end
class PuzzleSolver
def initialize(file_handler, debug: false)
@handler = file_handler
@debug = debug
end
def debug(message)
puts message if @debug
end
def print_debug(message)
print message if @debug
end
def solve
raise NotImplementedError, 'Please implement this method in subclasses.'
end
end
class HomeworkSolver < PuzzleSolver
def calculate(operator, int_array)
int_array.reduce(operator)
end
def solve
parsed_lines = @handler.read_file.split("\n").map {|line| line.split(" ")}
number_lines = parsed_lines[0...-1].map {|numbers| numbers.map(&:to_i)}
operator_line = parsed_lines[-1].map(&:to_sym)
solutions = operator_line.map.with_index do |operator, i|
ints = number_lines.map {|numbers| numbers[i]}
calculate(operator, ints)
end
solutions.sum
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby #{__FILE__} <file_name> [debug]"
exit 1
end
file_path = ARGV[0]
debug = (ARGV[1] == "debug")
file_handler = FileHandler.new(file_path)
## Puzzle-specific
cls = HomeworkSolver
##
puts "The answer is: #{cls.new(file_handler, debug:).solve}"

90
mark/day6/part2.rb Normal file
View File

@ -0,0 +1,90 @@
require 'debug'
# Generic FileHandler
class FileHandler
def initialize(file_path)
@file_path = file_path
end
# Usage:
# FileHandler.new('/path_to_file').read_lines do |line|
# # process line
# end
def read_lines
File.foreach(@file_path, chomp: true).map do |line|
yield(line)
end
end
# Usage:
# file_content = FileHandler.new('/path_to_file').read_file(
def read_file
File.read(@file_path)
end
end
class PuzzleSolver
def initialize(file_handler, debug: false)
@handler = file_handler
@debug = debug
end
def debug(message)
puts message if @debug
end
def print_debug(message)
print message if @debug
end
def solve
raise NotImplementedError, 'Please implement this method in subclasses.'
end
end
class HomeworkSolver < PuzzleSolver
def calculate(operator, int_array)
int_array.reduce(operator)
end
def solve
lines = @handler.read_file.split("\n")
operator_line = lines[-1].split(' ').map(&:to_sym)
number_lines = lines[0...-1].map(&:chars)
start_indices = lines[-1].chars.map.with_index {|x, i| x == ' ' ? nil : i }.reject {|x| x.nil?}
solutions = start_indices.map.with_index do |index, i|
start = index
final = (i < start_indices.length - 1) ? start_indices[i + 1] - 2 : number_lines[0].length - 1
operator = operator_line[i]
numbers = number_lines.map {|x| x[start..final]}
integers = []
total_nums = numbers[0].length
curr_index = total_nums - 1
while curr_index >= 0
integers << numbers.map {|digits| digits[curr_index]}.join.to_i
curr_index -= 1
end
calculate(operator, integers)
end
solutions.sum
end
end
if ARGV[0].nil? || ARGV[0].empty?
puts "Usage: ruby #{__FILE__} <file_name> [debug]"
exit 1
end
file_path = ARGV[0]
debug = (ARGV[1] == "debug")
file_handler = FileHandler.new(file_path)
## Puzzle-specific
cls = HomeworkSolver
##
puts "The answer is: #{cls.new(file_handler, debug:).solve}"