Skip to content

Commit c5318cf

Browse files
author
bbb
committed
feat(2023): Day 13 part 1
1 parent d924c82 commit c5318cf

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

typescript/2023/13/example

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#.##..##.
2+
..#.##.#.
3+
##......#
4+
##......#
5+
..#.##.#.
6+
..##..##.
7+
#.#.##.#.
8+
9+
#...##..#
10+
#....#..#
11+
..##..###
12+
#####.##.
13+
#####.##.
14+
..##..###
15+
#....#..#

typescript/2023/13/main.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const handlePattern = (pattern: string, part2 = false) => {
2+
const rows = pattern.split('\n')
3+
4+
let cols: string[] = Array(rows[0].length).fill('')
5+
for (const row of rows) {
6+
for (const [j, char] of [...row].entries()) {
7+
cols[j] += char
8+
}
9+
}
10+
11+
// Check horizontally
12+
for (let i = 1; i < rows.length; i++) {
13+
if (rows[i - 1] === rows[i]) {
14+
let x = 0
15+
while (rows[i-1 - x] === rows[i + x]) {
16+
x++
17+
if (i-1 - x < 0 || i + x > rows.length -1) {
18+
return i * 100
19+
}
20+
}
21+
}
22+
}
23+
24+
// Check vertically
25+
for (let i = 1; i < cols.length; i++) {
26+
if (cols[i - 1] === cols[i]) {
27+
let x = 0
28+
while (cols[i-1 - x] === cols[i + x]) {
29+
x++
30+
if (i-1 - x < 0 || i + x > cols.length -1) {
31+
return i
32+
}
33+
}
34+
}
35+
}
36+
37+
throw new Error("shouldn't happened...")
38+
}
39+
40+
const file = Bun.file('input');
41+
const input = await file.text();
42+
const patterns = input.trimEnd().split('\n\n')
43+
44+
const total1 = patterns.reduce((acc, curr) => acc + handlePattern(curr), 0)
45+
console.log(`Result for part 1 is : ${total1}`)
46+
47+
const total2 = 0
48+
console.log(`Result for part 2 is : ${total2}`)

0 commit comments

Comments
 (0)