Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions inputs/day_08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
120121010220011210213341221400410032024200305455123332224004344232032410401431102230313221032220201
201110121023013230302330021311131013421112311431532122324332345241340200121101343122032000302222002
102002033003222123334224022231014024154332332522532413152443343521420024142120221020312130000012220
022021002022122234142411233442032215244333211545534421223521213425452321323211314230012132230101111
000001213311312040044040324004345251313532532244214111234445435523443223231014031402202003220202022
120012020112324402101203201012524422234555211555441254543532525542455422431100123100134112211321001
201331122323231022322414132513445443542225314522442122134451213343255351243403333214100001221310002
202213230323304224323401211524355443322423124143363225331551434545141211212254401343122332131333320
121211210204211420223411323113111133452563332665665366256342425412125353114131102200130004332132023
112113312123342311122121342553114544542544233526256565633442525642534431424122340140222330201020323
212220201231231331225325433524212465354244363465433323422435453425443544233314142214213330210013003
323312311034244021124115523324235453334553635453645456346262262243535541114542314510442100141301122
322110024020203105422444554225662245423642666363652244366624233565255245323234242444442021114003131
020022410424334054535554351356634265526252563433644446436343335353246322341442512355214000224142031
113011113224033331332141536246552466454462445665475466223436433443654356455435321342422024224240233
122102233311322113321454362252243234624432447544375657355353324635433643234333542125422313433332321
233120110314034111141332425232564555665674743363443764643543745663655355633235321453124432104042231
100223100402324212124236262364446355557657673733745547363557734565242545543336131354254512432331033
220330034134143234154666534352252567443643755654677737776567374445733425452244254511151514234122213
223443144015521314236554452665635663377377657447643476573534645467643254356634655544313443004430033
101024341052411454165252566424674556674773657353656433343654447535674354245262332333523555242331132
220222110223533215564454533347744755673537673333455433773653447746564747422325642363122431352422211
004240214354322434225324663557754746553337448764574547564453643767763554322626552333144243243100443
244002112413445223342664642343745646463566545585488848785555544433334435346654656363551511425432311
304400052221222432253436664545656765657858858457776876475466485467576564653534555232555135554512103
301034122214253266644646654774435447685475487674646855754787758675544354374563635536335552414541214
023143512353534433525535355774437554848684458474566677886556568485755464753466263454354215342330021
114124535331116466652546536664743784557757457584466558864655558574887544735673255226463153414244423
321342532351563246464464534353744766487574566878456865566554674786854545747634762245326241111545302
222324535212433454342577763447374686575568646767976677984686747686474557575435676242252334324424133
041455115451655624645633347335876745564448989976988776878785466666874474733445456453534561112433131
234035233445234443233733563648557487657498899968897866857865785856886577346464734446452554244252101
042322443436343444646433663564848665688656875758665978859999876545768558637766334653546264321513150
311341431232552555557665566457765567779965958979678879768989766764576764655455375556543222315153443
432422413526624252456643546576745546967687555668559776976867857598647675565463476444325456215124324
141355224535323342657744536454664777877655975965957569957665675698786568684357375345633653521322134
141122432443266326344466678445475489879775756757768969966588589795858867477644375464354434554334542
404524251535424526674473367658588859789659999696879679797895959568796848845577655453633456432522251
052555241454534576467457557666865778585785777799889798799796659588785886875774465345553622342424344
431231215564564335455644575547778969999599889767689766876767676769986454648577766665523563621423244
015225326665656474355776784465595855559969779697867888768786867958987787865555557547456236256414443
342141412264436237555367786748757775996887869887989697887868985969768974878458377746343563334112432
245424112423634535653674854786759556887899698796699686769888766789698865458448564436356633653255423
152344536556354376777665885575687899789896978768778768999866666875968688488776547446465633626145213
143323326635362775675356868684997875878998968799998887969968886965597768474646637337765355426415522
233135153446345733457544575578758698668698796778899989988686677699869766655775463667634536224515421
442322432622254677457747586645559867668696769899897879878696967979757585566766656433333444234655425
211425433422356366455578477849687667769687699987878799877668866765868769745448755533566453422341433
142142345456557463367644747779979677698996689878999779799897968888567959674764537364645446523214543
112243356444435356743678858455568578689876999787888879899876878888997787884868645546363254345642533
341213364343435537345688745769797686679769679778778788899769986867599798667874534334444664434313333
243532164222666764774486748579698698876689888987878887998777687869755585776544863447444553624622141
341332522665223377644645444775976687977778897889989999977786997965895889866888467355775645335253141
451243166652422747755745765448975967698978698799997898999688997896678767488644763664752333224424553
343541133453346465373775878855568559988786799997787988999967996859859695684886756457573444332411142
314533526655524646634488455785868658876797968989787988796698776878757988467468647665474336362332241
442414135564436647537445687767866699679676668888999999798976797688656765447854434566735624532545551
032213154226544776557438554788789576777666889966978788867789879775988668444584736553534343622315541
255412122466665773547546685885987799779967977989879687777878699876566957766566647736545453255421212
211412336445563474775766856667577998798997678997686866778969879788687858487546455537653524366143412
051255123462256533566636777744877655896767699778989868868796978589575976844685335654756444462141342
322542554464342265336333465776645696986799889779679986879686787697697647574587563753453625343545521
033123425643433264635737446877447585887867896898797976879886786698688684876443473335322466665351333
133544314253462427775743366646484968555695958697698797876679667978854648774634475773653542364235421
224252431244343247466566786445767556659899789679866787689999766568777856867437767535434222445521233
125411234266255436753533664888768586696657765695958586799598696979557774846357447354325235425345222
321143533344454465635634634548656884666775656786776897656977675678544868467454563642465352614111534
324314254344466535257774447774685768796778758589556968777657876755454678874457376763662322355442241
120211113135325666447367553357474668876977665968588879588965668457446445756555755344632353524213111
311224554131565262327577435344586647744599595585679959557779868848845756333753447342232421323535422
114341442141636522627743367646645776654857585789589599759987575646778664434443566622565323551144104
210244242413152456645637357357684777785854766967688795794848665476646666547365474664225335235523134
124135421531242254525555475654745664788867846468845666746887888854654564764543562353564341341354241
232040414531142663343623666666355755588686458756864548766766867588477574653643266543552344453423142
241410112355426236262654356375777777866756675768668766578744448586537335565654443353226124123350230
140311131221515422466665656675435767464774857745654575455565568556777765663432626225661213413111223
110443321215553263625546467777476774558756864846764586756488688436634346356345662654634352234124332
221343332131351442346665233764775367674745555446558654675758467463467434543336344234234541352014440
133230443252342342426444235644376747467676654877885765875675433747574653525324245321411114352213140
040240221445511125623332465444547333774437463744467533446475535636346363436524235541352245340433401
204040003033154341135523356453644337774767676777576456764567576775643742526424535545245441222304411
304042224012435531346652244635426465733436576763444573475575775466734463356433425322423121010012441
020411330415234333353346655465454535766463345353547737546577344756646645346366361232312212001341120
222312310221442455555216363452342336634374577563373335557775555536643536565552525532355153044340022
321033011221232232531512333546323244633765663543646734377575566264364252534433124442412113401004401
221333321104001131445331226542565353624544444743335737563556662324243523642455442214142202003030231
031021212444432454341232542543223433534244654624443452522654546656624352422154453544520212142411231
000032101021330433425441534355232524355526522443326623545256546525633243254355513511514204131400330
010223333430411314211115534412532344324626322226423546334526362632342264344512422433334011314221121
030012220233010021431154541112223663556346352526365422265452334332256431412154223121433241032331112
221110003130033344202155154114455435426433642224264344325523445244211424335321121000334310001120112
010013223234422000001143241135132415264644343446443425245345324221223223212325552323312333412330222
032103213130121140030443255125332243343336336555656433255235254313433424224542501100302101221320301
112201332000400003333114425525244134115212434522644332332513434523545134353120230443131202022212112
212133330330243143424430334431513443235234452344525122433141135525522345433024123302104122132331020
110220302112033431311324214242235434551543155442332335242312153314452535320343340301332123320021320
121223310122302143024320402120433335414432343211234251415554145543214421134023034143302200002123012
221212001102321134443014113311424542341431224511531255544142141422243112400414323421322323011122100
102101010031312320301133133420124425352551441553552411331314122515423420100020312041230123102220212
Empty file added problems/day_08.md
Empty file.
79 changes: 79 additions & 0 deletions solutions/day_08.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class Day08
attr_accessor :trees, :part, :visibility_map

def initialize(part: 1)
self.trees = inputs
self.visibility_map = Array.new(99) { Array.new(99, '') }
self.part = part
end

def inputs
day_match = self.class.name.match(/Day(?<number>\d+)/)
File.readlines("./inputs/day_#{day_match[:number]}.txt", chomp: true).map(&:chars)
end

def parse_tree(tree)
tree.match(/(?<visible>[vi]?)(?<height>\d)/)
end

def tree_visibility
# West
trees.each.with_index do |tree_stand, stand_index|
tallest_tree = -1
tree_stand.each.with_index do |tree, tree_index|
if tree.to_i > tallest_tree
visibility_map[stand_index][tree_index] = 'v'
tallest_tree = tree.to_i
end
end
end

# East
trees.each.with_index do |tree_stand, stand_index|
tallest_tree = -1
tree_stand.reverse.each.with_index do |tree, tree_index|
if tree.to_i > tallest_tree
visibility_map[stand_index][-tree_index - 1] = 'v'
tallest_tree = tree.to_i
end
end
end

(0...trees.first.length).each do |tree_index|
vertical = trees.map { _1[tree_index] }
# South
tallest_tree = -1
vertical.each.with_index do |tree, stand_index|
if tree.to_i > tallest_tree
visibility_map[stand_index][tree_index] = 'v'
tallest_tree = tree.to_i
end
end
# North
tallest_tree = -1
vertical.reverse.each.with_index do |tree, stand_index|
if tree.to_i > tallest_tree
visibility_map[-stand_index - 1][tree_index] = 'v'
tallest_tree = tree.to_i
end
end
end
end

def solve_part_1
tree_visibility
visibility_map.sum { |row| row.count('v')}
end

def solve_part_2
records.count
end

def solve
if part == 1
solve_part_1
else
solve_part_2
end
end
end
39 changes: 39 additions & 0 deletions spec/day_08_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
RSpec.describe Day08 do
let(:solution) { described_class.new }

describe "#inputs" do
subject { solution.inputs }

it { expect(subject.first).to eq %w[1 2 0 1 2 1 0 1 0 2 2 0 0 1 1 2 1 0 2 1 3 3 4 1 2 2 1 4 0 0 4 1 0 0 3 2 0 2 4 2 0 0 3 0 5 4 5 5 1 2 3 3 3 2 2 2 4 0 0 4 3 4 4 2 3 2 0 3 2 4 1 0 4 0 1 4 3 1 1 0 2 2 3 0 3 1 3 2 2 1 0 3 2 2 2 0 2 0 1] }
it { expect(subject.last).to eq %w[1 0 2 1 0 1 0 1 0 0 3 1 3 1 2 3 2 0 3 0 1 1 3 3 1 3 3 4 2 0 1 2 4 4 2 5 3 5 2 5 5 1 4 4 1 5 5 3 5 5 2 4 1 1 3 3 1 3 1 4 1 2 2 5 1 5 4 2 3 4 2 0 1 0 0 0 2 0 3 1 2 0 4 1 2 3 0 1 2 3 1 0 2 2 2 0 2 1 2] }
it { expect(subject.first.count).to eq 99 }
it { expect(subject.count).to eq 99 }
end

describe "#solve" do
subject { solution.solve }

context "for part 1" do
it { is_expected.to eq 1816 }

context "with sample inputs" do
before do
solution.trees = %w[30373
25512
65332
33549
35390].map(&:chars)
solution.visibility_map = solution.trees.map(&:dup)
end

it { is_expected.to eq 21 }
end
end

xcontext "for part 2" do
let(:solution) { described_class.new(part: 2) }

it { is_expected.to eq 0 }
end
end
end