Skip to content

Commit 49ad96f

Browse files
committed
solve: year 2024 day 6 in ruby
1 parent 84046a6 commit 49ad96f

File tree

6 files changed

+298
-1
lines changed

6 files changed

+298
-1
lines changed

.github/workflows/ruby-style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
2727
- name: Run RuboCop
2828
continue-on-error: true
29-
run: rubocop
29+
run: rubocop $(git diff --name-only HEAD~1 HEAD -- '*.rb')

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
2+
logs/*
23
.env
34
.DS_Store
45
golang.so

2024/day6/input.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
..................#................................................................#........#.....................................
2+
...#...........#...................................................#........................................#.................#...
3+
...................................#................#.#...............#.................................................#.........
4+
.....#......#................#.....................................................................#..........#........#......#...
5+
............................................................................................#.....................#...............
6+
.....................#..##...#........................#.................#.......................#..#.........#.......#...#......#.
7+
............##....................................##..................#...............................#....#......................
8+
.............#............#.#..#.......#...........#..............#...............#.....#.........................................
9+
....................#..........##.........#.........#................#............................................................
10+
....#.......#................................................................#...#.#..........................................#.#.
11+
..#..............#.....................................#..........#.#....#..............#..................#.#............#.......
12+
.#.......#.........................#........................#..#.....#............................................................
13+
........#...#......................#...........#......................................#....................#......................
14+
.............................#..............#........##....#....................#......#....................#............#......#.
15+
..............#..............#............#.......................##....#..........................................#.........#....
16+
.............#............#..........#..........#...#.....................................................#.......................
17+
..#........#.....#..........................................#................................................#...#.#...#..........
18+
........#.....................#...#..#......#.........................................................#.....##.........#..........
19+
..............................................................................................#...#...............................
20+
..#.......................#..........................#......#...................................................................#.
21+
.......#.........................#..............#.........#.............#.......................................#...#.............
22+
..................#.................#.......................#....................#....................................#...........
23+
...#...#.......#......##.#...............#....#..............#..........................................#....#..#.................
24+
#................................#.......#.....#.......#.............#..................#........#................................
25+
...........#..................................#.......................................................................#.#..#......
26+
.......#........#.........................................#..........................#...........................#.....#..........
27+
................#..#.....................#..#...........................#...........................................#.............
28+
#...#.......................#................................................#................#................................#..
29+
..........#...........................#.......#............................................................................#.....#
30+
..................................#............................................................#..................................
31+
......................#........#.............#....#.....#.......#..........................#..........................#...........
32+
...........................##.................#.............#........................#...#..................#.....................
33+
.............................................................................................................#....#..........#....
34+
................#...................................#...........#..........#....................#............................#....
35+
........#....#................................................#................##..#..................................#...........
36+
...........#....................................................#...#.#......#....................................................
37+
.......................#.........................................................^....................................#...........
38+
..........................#.............##..#........#.#....#.......#....................#...........#.........#.....#............
39+
........#...........................#..#..........................................................#...............................
40+
....#....................................#....#.........................................#.........#...............................
41+
...............#...................................#.....#.................................#....................................#.
42+
......#...............................................................#.............................#........................#....
43+
.........................#.....#.........................#.#...............#.........#.....#..................#..#.........#......
44+
.........................................................#......#........##.......................#...#......#................#...
45+
.......................................#.....#.................................#..................................................
46+
..................#..................................#..........................................................................#.
47+
...................#....#.........#.......#................#....................#.........................#......#.....#..#.......
48+
................................#..............................................................................#....#.............
49+
.....................#.#.............#.................#..........#.......#.........................................#.............
50+
......#....................#....................................................................#...#.............................
51+
........#........#...........#.##.............#........................#............#..............#.........#....#........#......
52+
........................#.................................................................#.......................................
53+
............................................................#.....#....................#............#.....#....#............#.....
54+
...........#............#....#........................................................##............#..............#..............
55+
...........#...........#.....#..............#..............##....#........#......#................................................
56+
..#................................................................................#......................#......#................
57+
....................#.........#......................#.............................#.......#.......................#.............#
58+
..#...#....#......................................................................................................#...............
59+
...........#................................................................................#...............................#.....
60+
....................................#............#..........#.........................#..................................#.......#
61+
...................#......#...................#............#......#........#................#.............#...........#.........#.
62+
........................#..................................#........#..................#..........................................
63+
............#..#...........#..................##....................#...........................................#.................
64+
.......................................................................................#.....#............#.......................
65+
...............................................#....#................................#..#..........................#..#...........
66+
..............#..#.....#.........#....................#.......#..#..............#...............................#.....#...........
67+
..........#............................#..........................................................................................
68+
........................................#........#........................................................#.#.....................
69+
#................#......................#..................................................................................#......
70+
...............................................................................#................................#.................
71+
...................................................................................................................#.#........#...
72+
#........................................................................#......................................#.#...............
73+
...#......#............#...................#.............#........#..........................................#......#.............
74+
..............##....#...................................................#......................#..................................
75+
...............#........................................................................#...........#...#.#.......................
76+
..........#.#.....#................................#...........#........#.......#.................................................
77+
#....................#......................................................................................#.....................
78+
..............#...##...........................................#.............#.......#..................#..............#..........
79+
.#.............................................#..............................................................................#...
80+
...#................................#............................................#............................#...................
81+
..............................#......................................................#..........................................#.
82+
...................#.....................................#..................................#......#..............................
83+
....#.#..........##.............#.......#........................##..........#.................#..................................
84+
#..#................#........##....................................................................................#..............
85+
........#...#.........................................................................#...........................................
86+
......................................................#....#...................#..#.................................#.............
87+
........#...............#........#..............................................#............................#....................
88+
...#..............................#.........................................##.......................................#....#.......
89+
...#................#..............#.......#......................................................................................
90+
........#..................#.............................................................#...........................#....#.......
91+
......................#.#..........#.......#..................................#..#...#............#...............#...............
92+
.....#..................#....#..#......#.......................#.....................................................#............
93+
.....#................................#.........................#..................#.#..........#...#............#.....#..........
94+
....#...................#....#.#.........#........#..................................................................#....#..#....
95+
........#...............#......................................#........#..............................#..........................
96+
....#...#.#.....#..............#..................#..........#...#..#......................#...#..........#......................#
97+
...........#...................................................................................................#................#.
98+
.................................##..............#.............................#...............................#..................
99+
...........#.................#.............##...................................#.......................#.................##......
100+
.....#..................#......#........#.................................................#..#........................#..#........
101+
......#.......#......................................................................#.#.........#....#...........................
102+
..................#.#........................#......#................................................................#......#.....
103+
.....................#............#...................#.....#....................................#.......#..........#.......#.....
104+
#.............................#...............................#...#...............................................................
105+
..##.........#........#......................................#.....................#...#.............#............#..#............
106+
..........#.....................#..#....................#...#....................#....................#.....................#.....
107+
...........#..........#...............#..#..........#..............................................#.#............................
108+
............#...#...#.............#.........#.............................#...............#.......................................
109+
.........#...........#...............................................................#............................................
110+
.#..#.................................................................#............#.#.......................##...................
111+
........##............#...................#................................#......................................................
112+
............................................#..........................#..........................................................
113+
#...................#...........#..............#...#............#.#........#.................#......#.............................
114+
........#....................................#....................................................................................
115+
...........#.........#..#...............#...........#.....##.#......#....##............#....#....#.#.........##.........#.........
116+
..#......................#..........#.................#.................#......#.............................#........#...........
117+
....................#.................#....#..............................#....#.............#....#....#.....................#....
118+
............#.................#.................................#...............................#..#...........................#..
119+
...................................................#.....#..............#.......#....................#.#..........................
120+
..#.........##..#........#...................................................................................#............#.......
121+
.....#..............................#......................................................................#........#.............
122+
................................#........##.......#...............#..............#.......#...#.#........#.........................
123+
..............................#.....................#.........................................#..................................#
124+
...........#...................##.................#.................................#...............................#........#....
125+
..............................#...##......#..................#...#................................#......#.......#...............#
126+
.....................##......#......#.#.......#..............#......#.................#.........#...................#.............
127+
..........................#................................#...........#..........................................................
128+
...#.....#....................#.....................#...#.....#.............................#.#....#.....#.#.................#....
129+
............................#...#........#......................................................................#.....#...........
130+
............#..........##..................#.............................................................#.....#..#...............

2024/day6/part1.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
DIRECTIONS = ['>', 'v', '<', '^'].freeze
4+
DELTAS = [
5+
[0, 1], # >
6+
[1, 0], # v
7+
[0, -1], # <
8+
[-1, 0] # ^
9+
].freeze
10+
11+
def inside?(pos_x, pos_y, board)
12+
pos_x.between?(0, board.length - 1) && pos_y.between?(0, board[0].length - 1)
13+
end
14+
15+
def find_start_position(board)
16+
board.each_with_index do |row, i|
17+
row.each_with_index do |cell, j|
18+
return [i, j, DIRECTIONS.index(cell)] if DIRECTIONS.include?(cell)
19+
end
20+
end
21+
nil
22+
end
23+
24+
def solution(input)
25+
board = input.split("\n").map(&:chars)
26+
start_x, start_y, direction = find_start_position(board)
27+
28+
count = 1
29+
pos_x = start_x
30+
pos_y = start_y
31+
32+
while inside?(pos_x, pos_y, board)
33+
dx = DELTAS[direction][0]
34+
dy = DELTAS[direction][1]
35+
next_x = pos_x + dx
36+
next_y = pos_y + dy
37+
38+
return count unless inside?(next_x, next_y, board)
39+
40+
next_cell = board[next_x][next_y]
41+
if next_cell == '#'
42+
direction = (direction + 1) % 4
43+
dx = DELTAS[direction][0]
44+
dy = DELTAS[direction][1]
45+
next_x = pos_x + dx
46+
next_y = pos_y + dy
47+
next_cell = board[next_x][next_y]
48+
end
49+
50+
if next_cell == '.'
51+
count += 1
52+
board[next_x][next_y] = '*'
53+
end
54+
55+
pos_x = next_x
56+
pos_y = next_y
57+
end
58+
59+
count
60+
end

0 commit comments

Comments
 (0)