Add solution for 2015 day 03
This commit is contained in:
parent
45659f2ca8
commit
0be77a51b1
@ -3,7 +3,7 @@
|
|||||||
## Progress
|
## Progress
|
||||||
- [x] Day 01
|
- [x] Day 01
|
||||||
- [x] Day 02
|
- [x] Day 02
|
||||||
- [ ] Day 03
|
- [x] Day 03
|
||||||
- [ ] Day 04
|
- [ ] Day 04
|
||||||
- [ ] Day 05
|
- [ ] Day 05
|
||||||
- [ ] Day 06
|
- [ ] Day 06
|
||||||
|
1
2015/day-03/inputs/puzzle.txt
Normal file
1
2015/day-03/inputs/puzzle.txt
Normal file
File diff suppressed because one or more lines are too long
6
2015/day-03/inputs/test.txt
Normal file
6
2015/day-03/inputs/test.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
>
|
||||||
|
^>v<
|
||||||
|
^v^v^v^v^v
|
||||||
|
^v
|
||||||
|
^>v<
|
||||||
|
^v^v^v^v^v
|
78
2015/day-03/main.rb
Executable file
78
2015/day-03/main.rb
Executable file
@ -0,0 +1,78 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require "pathname"
|
||||||
|
require "set"
|
||||||
|
|
||||||
|
INPUTS = [
|
||||||
|
Pathname("inputs/test.txt"),
|
||||||
|
Pathname("inputs/puzzle.txt"),
|
||||||
|
].freeze
|
||||||
|
|
||||||
|
def position_modifier(direction)
|
||||||
|
case direction
|
||||||
|
when ">" then [1, 0]
|
||||||
|
when "<" then [-1, 0]
|
||||||
|
when "^" then [0, 1]
|
||||||
|
when "v" then [0, -1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_direction(position, change)
|
||||||
|
position.map.with_index { |v, i| v + change[i] }
|
||||||
|
end
|
||||||
|
|
||||||
|
def next_position(position, direction)
|
||||||
|
apply_direction(position, position_modifier(direction))
|
||||||
|
end
|
||||||
|
|
||||||
|
def solve_part_1(input)
|
||||||
|
input.lines.map do |line|
|
||||||
|
puts " " + "-" * 10
|
||||||
|
|
||||||
|
position = [0, 0]
|
||||||
|
|
||||||
|
houses =
|
||||||
|
line.chomp.each_char.each_with_object(Set[position]) do |direction, visited|
|
||||||
|
next_pos = next_position(position, direction)
|
||||||
|
visited.add(next_pos)
|
||||||
|
position = next_pos
|
||||||
|
end
|
||||||
|
|
||||||
|
puts " Houses: #{houses.size}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def solve_part_2(input)
|
||||||
|
input.lines.map do |line|
|
||||||
|
puts " " + "-" * 10
|
||||||
|
|
||||||
|
position_s = [0, 0]
|
||||||
|
position_r = position_s.dup
|
||||||
|
|
||||||
|
houses =
|
||||||
|
line.chomp.each_char.each_with_object(Set[position_s.dup]).with_index(1) do |(direction, visited), index|
|
||||||
|
if index.even?
|
||||||
|
next_pos = next_position(position_s, direction)
|
||||||
|
visited.add(next_pos)
|
||||||
|
position_s = next_pos
|
||||||
|
else
|
||||||
|
next_pos = next_position(position_r, direction)
|
||||||
|
visited.add(next_pos)
|
||||||
|
position_r = next_pos
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts " Houses: #{houses.size}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def main(files)
|
||||||
|
files.each do |file|
|
||||||
|
puts "File: #{file}"
|
||||||
|
solve_part_1(file.read.chomp)
|
||||||
|
puts "=" * 15
|
||||||
|
solve_part_2(file.read.chomp)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
main(INPUTS)
|
Loading…
Reference in New Issue
Block a user