1
0
Fork 0

Day eight

This commit is contained in:
Andrew Tomaka 2024-12-08 19:20:48 -05:00
parent c81ea4f786
commit 68238609db
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
3 changed files with 222 additions and 0 deletions

80
8-1.rb Executable file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env ruby
problem = 8
input = File.readlines("#{problem}.input").map(&:strip)
#input = DATA.read.split("\n")
# Find diferent antennas
signals = input.join.chars.uniq
.reject { _1 == "." }
.map { [_1, []] }
.to_h
grid = input.map(&:chars)
# map antenna locations
signals.keys.each do |signal|
grid.each_with_index do |row, x|
row.each_with_index do |val, y|
signals[val] << [x, y] if val == signal
end
end
end
# calculate antinodes between all node pairs
antinodes = []
signals.each do |(signal, locations)|
locations.combination(2).each do |(first, second)|
x = (first.first - second.first).abs
y = (first.last - second.last).abs
if first.first < second.first
x1 = first.first - x
x2 = second.first + x
else
x1 = first.first + x
x2 = second.first - x
end
if first.last < second.last
y1 = first.last - y
y2 = second.last + y
else
y1 = first.last + y
y2 = second.last - y
end
antinode_1 = [x1, y1]
antinode_2 = [x2, y2]
#require "debug";debugger
antinodes << antinode_1 if x1 >= 0 && y1 >= 0 && x1 < grid.size && y1 < grid.first.size
antinodes << antinode_2 if x2 >= 0 && y2 >= 0 && x2 < grid.size && y2 < grid.first.size
end
end
p antinodes.uniq.size
grid.each_with_index do |row, x|
row.each_with_index do |val, y|
if antinodes.index([x,y])
print "#"
else
print grid[x][y]
end
end
puts
end
__END__
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

92
8-2.rb Executable file
View file

@ -0,0 +1,92 @@
#!/usr/bin/env ruby
problem = 8
input = File.readlines("#{problem}.input").map(&:strip)
#input = DATA.read.split("\n")
# Find diferent antennas
signals = input.join.chars.uniq
.reject { _1 == "." }
.map { [_1, []] }
.to_h
grid = input.map(&:chars)
# map antenna locations
signals.keys.each do |signal|
grid.each_with_index do |row, x|
row.each_with_index do |val, y|
signals[val] << [x, y] if val == signal
end
end
end
# calculate antinodes between all node pairs
antinodes = []
signals.each do |(signal, locations)|
locations.each { antinodes << _1 }
locations.combination(2).each do |(first, second)|
x = (first.first - second.first).abs
y = (first.last - second.last).abs
node1, node2 = first, second
loop do
if first.first < second.first
x1 = node1.first - x
else
x1 = node1.first + x
end
if first.last < second.last
y1 = node1.last - y
else
y1 = node1.last + y
end
node1 = [x1, y1]
break unless x1 >= 0 && y1 >= 0 && x1 < grid.size && y1 < grid.first.size
antinodes << node1
end
loop do
if first.first < second.first
x2 = node2.first + x
else
x2 = node2.first - x
end
if first.last < second.last
y2 = node2.last + y
else
y2 = node2.last - y
end
node2 = [x2, y2]
break unless x2 >= 0 && y2 >= 0 && x2 < grid.size && y2 < grid.first.size
antinodes << node2
end
end
end
p antinodes.uniq.size
grid.each_with_index do |row, x|
row.each_with_index do |val, y|
if antinodes.index([x,y])
print "#"
else
print grid[x][y]
end
end
puts
end
__END__
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

50
8.input Normal file
View file

@ -0,0 +1,50 @@
.......................O......T.....d......M......
..............................F...................
..........V..................R....................
............B..t..........T..........d............
.....................B.........T................M.
..V.................................2.......M.....
.......V........................F.O..........2....
...................................T..............
..................................................
......r..........B......................c.........
.....o3.B.............................2...........
..................1...m..o....d..c.....M..........
......Qr....o............F....0............1......
....Q.......................0....................2
......t..........0................................
.............R.................................mL.
....r..............3.....................c..1.....
.........Q.........................1..............
................x...R.............................
...x........8.R...................................
..................8...............................
........x.u.................Z.....................
...........................X...............d......
....................30.....................f......
......q...............v...................c.......
..........t8.........D.3..........................
.......t.......4.............8....................
...b..................C...........D...............
.........................v..ND4..........K........
.......F.........u...........C..............fZ....
........X..9...........N.........Z..........k.....
.............X.6...q..........................k...
..............................C.Z...........m....k
...................4.v..............N.............
....................u.......D..............m......
............................vl.....UK.............
............................l..6.......f..........
..................q.4............N................
..........b....x..............fu..................
.9..................................U.......l.....
....w......b.........L......6.....z.5.............
..........X..........W6........5............z.....
...........q..........L............z........n...W.
............................5.........n...W..z....
........9........w................7....n..........
............w......................7...K.....n....
.........................U....K......W............
.........w.....L.................k....7...........
...................7.............l.............5..
..............9...................................