Skip to content

Commit 0d40326

Browse files
committed
Day 4
1 parent a34dbe9 commit 0d40326

14 files changed

+988
-0
lines changed

3/.gitignore

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

3/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 3
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run index.ts
13+
```
14+
15+
This project was created using `bun init` in bun v1.0.14. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

3/bun.lockb

1.64 KB
Binary file not shown.

3/index.ts

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
type Matrix = Array<Array<string | number>>;
2+
3+
const data = await Bun.file("input.txt").text();
4+
const lines = data.split("\n");
5+
const matrix = lines.map((line) =>
6+
line.split("").map((char) => {
7+
if (char === "0") return 0;
8+
if (parseInt(char)) return parseInt(char);
9+
return char;
10+
})
11+
);
12+
13+
const isEqualToSymbol = (input: string | number) => {
14+
// return true if input is different from number and "."
15+
if (typeof input === "number") return false;
16+
if (input === ".") return false;
17+
return true;
18+
};
19+
20+
const hasSymbolNear = (matrix: Matrix, i: number, j: number): boolean => {
21+
if (matrix[i][j + 1] !== undefined) {
22+
const right = matrix[i][j + 1];
23+
24+
if (isEqualToSymbol(right)) {
25+
return true;
26+
}
27+
}
28+
29+
if (matrix[i][j - 1] !== undefined) {
30+
const left = matrix[i][j - 1];
31+
32+
if (isEqualToSymbol(left)) {
33+
return true;
34+
}
35+
}
36+
37+
if (matrix[i - 1] !== undefined) {
38+
const topLeft = matrix[i - 1][j - 1];
39+
const top = matrix[i - 1][j];
40+
const topRight = matrix[i - 1][j + 1];
41+
42+
if (isEqualToSymbol(topLeft)) {
43+
return true;
44+
}
45+
46+
if (isEqualToSymbol(top)) {
47+
return true;
48+
}
49+
50+
if (isEqualToSymbol(topRight)) {
51+
return true;
52+
}
53+
}
54+
55+
if (matrix[i + 1] !== undefined) {
56+
const bottomLeft = matrix[i + 1][j - 1];
57+
const bottom = matrix[i + 1][j];
58+
const bottomRight = matrix[i + 1][j + 1];
59+
60+
if (isEqualToSymbol(bottomLeft)) {
61+
return true;
62+
}
63+
64+
if (isEqualToSymbol(bottom)) {
65+
return true;
66+
}
67+
68+
if (isEqualToSymbol(bottomRight)) {
69+
return true;
70+
}
71+
}
72+
73+
return false;
74+
};
75+
76+
const buildNumber = (matrix: Matrix, i: number, j: number) => {
77+
let peekPosition = j;
78+
let number = "";
79+
while (
80+
matrix[i][peekPosition] !== "." &&
81+
!isEqualToSymbol(matrix[i][peekPosition])
82+
) {
83+
const char = matrix[i][peekPosition].toString();
84+
number += char;
85+
peekPosition++;
86+
}
87+
return number;
88+
};
89+
90+
let numbers = [];
91+
for (let i = 0; i < matrix.length; i++) {
92+
const line = matrix[i];
93+
for (let j = 0; j < line.length; j++) {
94+
const char = line[j];
95+
if (typeof char === "number") {
96+
const res = hasSymbolNear(matrix, i, j);
97+
if (res) {
98+
// has symbol near
99+
let number = buildNumber(matrix, i, j);
100+
numbers.push(number);
101+
j += number.length;
102+
} else {
103+
// no symbol near
104+
let peekPosition = j + 1;
105+
let hasSymbolAfter = false;
106+
while (typeof matrix[i][peekPosition] === "number") {
107+
if (matrix[i][peekPosition] === ".") break;
108+
if (hasSymbolNear(matrix, i, peekPosition)) {
109+
hasSymbolAfter = true;
110+
let number = buildNumber(matrix, i, j);
111+
numbers.push(number);
112+
j += number.length;
113+
break;
114+
}
115+
peekPosition++;
116+
}
117+
}
118+
}
119+
}
120+
}
121+
122+
console.log(numbers.reduce((acc, curr) => acc + parseInt(curr), 0));
123+
console.log(numbers);
124+
125+
const data2 = await Bun.file("input.txt").text();
126+
const lines2 = data2.split("\n");
127+
const grid = lines2.map((line) => line.split("."));
128+
console.log(grid);

0 commit comments

Comments
 (0)