Skip to content

Commit a8d694e

Browse files
committed
Day 10 - Puzzle 1
1 parent 11d1cc6 commit a8d694e

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

β€Žlib/input/day_10.txtβ€Ž

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
543213210349876210129890176034034565966563210587109092121
2+
656104101236762121456761789125120677877432101498218187030
3+
787215678945643012369852652196541986778943452387367256541
4+
898764101223454921058943543087632345657654363456456349654
5+
234459814312367838901012634678983954108932276545234438743
6+
105308765101298987632108768901017873217841189034105698894
7+
076218965290345676543289457632124561678750025121236787101
8+
987987874387654989123498398543401410589867014240901223218
9+
128876210260123478004567212898532323475678923457816314509
10+
019564320178874565213216500187645654129854503966765405678
11+
323469834569982104304307431234578765036763212875896876989
12+
767878706521676235425498120110569834545678923214387932376
13+
876967017430501543216326013223432728901298012103098541205
14+
985054328941432652107817154370121112854347543892107670314
15+
834123217632965469234908965987430004761256556701256789323
16+
729654306541874678945699874876548123450236789210345430110
17+
618783210930123012876789103765659874210101654345014521221
18+
105690129850019823498632212014567865321256789765623012434
19+
104941010761236710567541003423438978762349809854789923455
20+
203832123450145623456410212789829569451056012343210854396
21+
312745898567876514589321345657810438342347898901345765987
22+
405656776343998105679430103456910123239897687432216705476
23+
512346785434867234578345612765878762101798576521009812345
24+
694567698123452105507656709894349656123603401098967826565
25+
783878981030143245412349814763234341014512032367654983456
26+
012969976543201236093218923452123210012432145456543452187
27+
121001876124210187187108765421005391143465434505412341093
28+
321232565035303298296089012321016789834874328212303433282
29+
210543458749456334345676521056525470765966019301601214101
30+
109851239658765478034785435437434321014987458456732105010
31+
238760548789678969123699876348945610523456367234845256723
32+
321017656698767056782176501267876525676789210105996367894
33+
438998765589850145891089437678801234989654101986087458965
34+
567321996434743234106543228769960145676543243877124349896
35+
675490887525612103257890119454877658983450112568233210787
36+
587586716014101678965432001323768943232162103499545012345
37+
896675105003234567876721017012057890123078213487636776596
38+
745564234123098656989830398701146321265459812345629889487
39+
932213047894187765216541235610235430876343501676712012376
40+
871302120765276894307890344320145210901265430989800193401
41+
560456961294345653210787653410236389810178125476543287632
42+
410367854386543464678876544567107458103269076398389326543
43+
321298765677812104589965433218998567234387681267276410014
44+
123457654308903243218760129809889234985898790354105569123
45+
016534567210211038909678978321010125676787063203234678874
46+
105673898323302347874541065410981589094543154112356787965
47+
234982765432423456743232456723873672187612267053543298874
48+
122801894541510161250101365834712543870101348765632107985
49+
021289843690678870367010212945603434983289659054901001276
50+
130126732784569965478101204988914301874378778123892104345
51+
245035011098430156789678345677765210165134589104743077656
52+
356544324567821025898569101056879321051021678201654988745
53+
987676543056932110185430202346978432896120214312345679034
54+
678989432108945523679021312567566543787034305478943456123
55+
549034549087876654578110453498754694986545456967012987101
56+
432123678896521783063234569787103785675676567852173985432
57+
101210510123430192154345478776212656548989656743089876501

β€Žlib/solutions/day_10.rbβ€Ž

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Day10
2+
def part_one(input)
3+
thf = TrailheadFinder.new(input)
4+
thf.find_trailheads
5+
end
6+
7+
def part_two(input)
8+
0
9+
end
10+
end
11+
12+
class TrailheadFinder
13+
attr_accessor :grid, :w, :h
14+
15+
def initialize(input)
16+
# Parse grid
17+
@grid = input.split("\n").map { |line| line.chars.map(&:to_i) }
18+
@w = @grid[0].size
19+
@h = @grid.size
20+
end
21+
22+
def find_trailheads
23+
total_nines = 0
24+
25+
@grid.each_with_index do |row, y|
26+
row.each_with_index do |cell, x|
27+
total_nines += count_reachable_nines(x, y) if cell == 0
28+
end
29+
end
30+
31+
total_nines
32+
end
33+
34+
def count_reachable_nines(x, y)
35+
visited = Array.new(@h) { Array.new(@w, false) }
36+
queue = [[x, y, 0]]
37+
reachable_nines = 0
38+
39+
until queue.empty?
40+
cx, cy, current_value = queue.shift
41+
next if visited[cy][cx]
42+
43+
visited[cy][cx] = true
44+
45+
if @grid[cy][cx] == 9
46+
reachable_nines += 1
47+
next
48+
end
49+
50+
[[0, 1], [1, 0], [0, -1], [-1, 0]].each do |dx, dy|
51+
nx = cx + dx
52+
ny = cy + dy
53+
if nx.between?(0, @w - 1) && ny.between?(0, @h - 1) && !visited[ny][nx] && @grid[ny][nx] == current_value + 1
54+
queue << [nx, ny, current_value + 1]
55+
end
56+
end
57+
end
58+
59+
reachable_nines
60+
end
61+
end

β€Žspec/input/day_10_test.txtβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
89010123
2+
78121874
3+
87430965
4+
96549874
5+
45678903
6+
32019012
7+
01329801
8+
10456732
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
require 'solutions/day_10'
3+
4+
RSpec.describe Day10 do
5+
let(:input) { File.read(File.join(__dir__, '..', '..', 'spec', 'input', 'day_10_test.txt')).strip }
6+
7+
describe '#part_one' do
8+
it 'small example' do
9+
input = <<~INPUT
10+
0123
11+
1234
12+
8765
13+
9876
14+
INPUT
15+
expect(subject.part_one(input)).to eq(1)
16+
end
17+
18+
it 'calculates the correct solutions for part one' do
19+
expect(subject.part_one(input)).to eq(36)
20+
end
21+
end
22+
23+
describe '#part_two' do
24+
it 'calculates the correct solutions for part two' do
25+
expect(subject.part_two(input)).to eq(0)
26+
end
27+
end
28+
end

0 commit comments

Comments
Β (0)