mirror of
https://github.com/seigler/aoc2023
synced 2025-07-26 06:16:10 +00:00
day 3
This commit is contained in:
parent
a71ae9c4d7
commit
a9a646b1e1
4 changed files with 112 additions and 8 deletions
|
@ -33,10 +33,10 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "539590",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 2.532209
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
|
|
10
README.md
10
README.md
|
@ -13,7 +13,7 @@
|
|||
|
||||
[](src/day01)
|
||||
[](src/day02)
|
||||

|
||||
[](src/day03)
|
||||

|
||||

|
||||

|
||||
|
@ -83,9 +83,9 @@ Both parts: 1.293ms
|
|||
|
||||
```
|
||||
Day 03
|
||||
Time part 1: -
|
||||
Time part 1: 2.529ms
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Both parts: 2.529ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 4/50
|
||||
Total time: 3.173ms
|
||||
Total stars: 5/50
|
||||
Total time: 5.703ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day03/README.md
Normal file
9
src/day03/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2023 - day 3 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2023/day/3)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
95
src/day03/index.ts
Normal file
95
src/day03/index.ts
Normal file
|
@ -0,0 +1,95 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => rawInput.split("\n")
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let total = 0
|
||||
input.forEach((line, row) => {
|
||||
let numString = ""
|
||||
line.split("").forEach((char, col) => {
|
||||
if (/\d/.test(char)) {
|
||||
numString += char
|
||||
}
|
||||
if (!/\d/.test(line[col + 1] ?? "")) {
|
||||
// time to test current string
|
||||
if (numString === "") {
|
||||
return
|
||||
}
|
||||
for (
|
||||
let r = Math.max(row - 1, 0);
|
||||
r <= Math.min(row + 1, input.length - 1);
|
||||
r++
|
||||
) {
|
||||
for (
|
||||
let c = Math.max(col - numString.length, 0);
|
||||
c <= Math.min(col + 1, line.length - 1);
|
||||
c++
|
||||
) {
|
||||
if (!/\d|\./.test(input[r].charAt(c))) {
|
||||
total += parseInt(numString)
|
||||
numString = ""
|
||||
return // done with this char
|
||||
}
|
||||
}
|
||||
}
|
||||
numString = ""
|
||||
}
|
||||
})
|
||||
})
|
||||
return total
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
input.forEach((line, row) => {
|
||||
line.split("").forEach((char, col) => {
|
||||
if (char === "*") {
|
||||
// ask every found number if it touches a star
|
||||
// add to total the product of all the pairs
|
||||
}
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..`,
|
||||
expected: 4361,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..`,
|
||||
expected: 467835,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue