Skip to content

Commit 5c0f572

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

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed

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

2024/day6/part2.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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?(x, y, board)
12+
x.between?(0, board.length - 1) && 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+
end
22+
23+
def has_loop?(start_x, start_y, direction, board)
24+
x, y, d = start_x, start_y, direction
25+
states = Set.new(["#{x},#{y},#{d}"]) # set of string is a bit faster than set of array
26+
27+
while inside?(x, y, board)
28+
dx, dy = DELTAS[d]
29+
next_x, next_y = x + dx, y + dy
30+
31+
return false unless inside?(next_x, next_y, board)
32+
33+
next_cell = board[next_x][next_y]
34+
while next_cell == '#'
35+
d = (d + 1) % 4
36+
dx, dy = DELTAS[d]
37+
next_x, next_y = x + dx, y + dy
38+
next_cell = board[next_x][next_y]
39+
end
40+
41+
next_state = "#{next_x},#{next_y},#{d}"
42+
return true if states.include?(next_state)
43+
states << next_state
44+
x, y = next_x, next_y
45+
end
46+
end
47+
48+
def solution(input)
49+
board = input.split("\n").map(&:chars)
50+
start_x, start_y, direction = find_start_position(board)
51+
52+
visited = Set.new([[start_x, start_y]])
53+
x, y, d = start_x, start_y, direction
54+
55+
while inside?(x, y, board)
56+
dx, dy = DELTAS[d]
57+
next_x, next_y = x + dx, y + dy
58+
59+
break unless inside?(next_x, next_y, board)
60+
61+
next_cell = board[next_x][next_y]
62+
if next_cell == '#'
63+
d = (d + 1) % 4
64+
dx, dy = DELTAS[d]
65+
next_x, next_y = x + dx, y + dy
66+
next_cell = board[next_x][next_y]
67+
end
68+
69+
visited << [next_x, next_y]
70+
x, y = next_x, next_y
71+
end
72+
73+
visited.delete([start_x, start_y])
74+
visited.count do |x, y|
75+
board[x][y] = '#'
76+
has_loop?(start_x, start_y, direction, board).tap do
77+
board[x][y] = '.'
78+
end
79+
end
80+
end

0 commit comments

Comments
 (0)