Skip to content

Commit 2997c0b

Browse files
committed
Day 8 - Puzzle 1
1 parent ebbe992 commit 2997c0b

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

β€ŽGemfileβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ source 'https://rubygems.org'
22

33
ruby '3.3.4'
44

5+
gem 'matrix'
56
gem 'parallel', '~> 1.11', '>= 1.11.2'
67
gem 'rspec'

β€ŽGemfile.lockβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ GEM
22
remote: https://rubygems.org/
33
specs:
44
diff-lcs (1.5.1)
5+
matrix (0.4.2)
56
parallel (1.26.3)
67
rspec (3.13.0)
78
rspec-core (~> 3.13.0)
@@ -22,6 +23,7 @@ PLATFORMS
2223
ruby
2324

2425
DEPENDENCIES
26+
matrix
2527
parallel (~> 1.11, >= 1.11.2)
2628
rspec
2729

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
..................................................
2+
.......................x.................N........
3+
..................x...............................
4+
............x................w.........D.....a....
5+
.........6...........i.........D..............u...
6+
........6.......................q................u
7+
........................i....u.w...........a.2....
8+
....................................u.12..........
9+
..................................f.....D....0a...
10+
.............................Q..D......c..N..f....
11+
..............w..................................f
12+
.........Y.............i...............q..a.......
13+
............................2..........O...q......
14+
.....6..G.....................R...................
15+
..............................N...................
16+
.U.......G................i...J.............0.....
17+
Y..U................F......N......................
18+
.T......Y.............H.................2.P.......
19+
...............T.........F.8........H.............
20+
..T...............F...............l..............0
21+
................G.....e........18...Q.............
22+
.......................F....................O.....
23+
.....Y....U...................l....g..............
24+
U........9........................................
25+
.....................e..q..Q......................
26+
.......X........e......................1Q..O......
27+
............T.................gx......0..........t
28+
...................l......9........P..............
29+
.y...........9...............r.5.......j.P........
30+
..z.........d.........g......................H....
31+
................6.......r...........P.........O...
32+
.A........................8...r...................
33+
.4....W...Z...........9..................s..j.....
34+
.z..W.........y...........og......................
35+
..3.z.....R.....L....o.........................H..
36+
.......yZ.c..W.......p..............s.............
37+
............1..3.........L.........S..............
38+
.......Z..4............o.....S...........5.......s
39+
............c........l......7.....................
40+
.....4....p.........I.......t...........5........j
41+
.......c....h...........C..d......................
42+
......n..........C......L............E....j.......
43+
.X.W..........n....R......d.I...............5.....
44+
3.........Cn.........L...r.............e..........
45+
...A...........Z.p.....I..S.............s.......J.
46+
....................7.............S...X....J......
47+
........X.............o...........................
48+
........A....h.R.....7.t...I......................
49+
..A.4z......y.p..h.7...........Et.................
50+
................h........3..E..d.8................

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'matrix'
2+
3+
class Day08
4+
attr_accessor :grid, :w, :h, :nodes, :antinodes
5+
6+
def part_one(input)
7+
parse_input(input)
8+
9+
@nodes.each_pair do |_node, coords|
10+
find_antinodes(coords)
11+
end
12+
13+
@antinodes.flatten.count(true)
14+
end
15+
16+
def part_two(_input)
17+
0
18+
end
19+
20+
def mark_antinode(x, y)
21+
@counter ||= 0
22+
@counter += 1
23+
24+
# print "M #{@counter}: #{x}, #{y} (#{@w}x#{@h})"
25+
26+
if x < 0 || x >= @w || y < 0 || y >= @h
27+
# puts '- OUT'
28+
return
29+
end
30+
31+
# puts ' - OK'
32+
33+
@antinodes[y][x] = true
34+
end
35+
36+
def find_antinodes(coords)
37+
# Loop through all coordinate pemutations
38+
coords.permutation(2).to_a.each do |a, b|
39+
dx = b[0] - a[0]
40+
dy = b[1] - a[1]
41+
42+
mark_antinode(a[0] - dx, a[1] - dy)
43+
end
44+
end
45+
46+
def parse_input(input)
47+
# Parse grid
48+
@grid = input.split("\n").map(&:chars)
49+
@w = @grid[0].size
50+
@h = @grid.size
51+
52+
# Track visisted nodes
53+
@visited = Array.new(@h) { Array.new(@w, false) }
54+
55+
# Find antenna locations
56+
@nodes = {}
57+
@grid.each_with_index do |row, y|
58+
row.each_with_index do |cell, x|
59+
if cell != '.'
60+
@nodes[cell] ||= []
61+
@nodes[cell] << [x, y]
62+
end
63+
end
64+
end
65+
66+
@antinodes = Array.new(@h) { Array.new(@w, false) }
67+
end
68+
end

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'spec_helper'
2+
require 'solutions/day_08'
3+
4+
RSpec.describe Day08 do
5+
let(:input) { File.read(File.join(__dir__, '..', '..', 'spec', 'input', 'day_08_test.txt')).strip }
6+
7+
describe '#part_one' do
8+
it 'calculates the correct solutions for part one' do
9+
expect(subject.part_one(input)).to eq(14)
10+
end
11+
end
12+
13+
describe '#part_two' do
14+
it 'calculates the correct solutions for part two' do
15+
expect(subject.part_two(input)).to eq(0)
16+
end
17+
end
18+
end

0 commit comments

Comments
Β (0)