mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 00:56:10 +00:00
day 6, buggy part 2
This commit is contained in:
parent
0aa40e557a
commit
ad6829def6
4 changed files with 143 additions and 12 deletions
|
@ -77,16 +77,18 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": null,
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": null
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": false,
|
||||||
"result": null,
|
"result": "1878",
|
||||||
"attempts": [],
|
"attempts": [
|
||||||
"time": null
|
"1878"
|
||||||
|
],
|
||||||
|
"time": 2128.985844
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
16
README.md
16
README.md
|
@ -16,7 +16,7 @@
|
||||||
[](src/day03)
|
[](src/day03)
|
||||||
[](src/day04)
|
[](src/day04)
|
||||||
[](src/day05)
|
[](src/day05)
|
||||||

|
[](src/day06)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -97,16 +97,16 @@ Both parts: 250.695ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 05
|
Day 05
|
||||||
Time part 1: 29.068ms
|
Time part 1: 28.307ms
|
||||||
Time part 2: 53.891ms
|
Time part 2: 35.288ms
|
||||||
Both parts: 82.959ms
|
Both parts: 63.594ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 06
|
Day 06
|
||||||
Time part 1: -
|
Time part 1: 1.668ms
|
||||||
Time part 2: -
|
Time part 2: -
|
||||||
Both parts: -
|
Both parts: 1.668ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 10/50
|
Total stars: 11/50
|
||||||
Total time: 342.015ms
|
Total time: 324.318ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day06/README.md
Normal file
9
src/day06/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 🎄 Advent of Code 2024 - day 6 🎄
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
Task description: [link](https://adventofcode.com/2024/day/6)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
...
|
120
src/day06/index.ts
Normal file
120
src/day06/index.ts
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
import run from "aocrunner"
|
||||||
|
|
||||||
|
const parseInput = (rawInput: string) => rawInput.split('\n')
|
||||||
|
|
||||||
|
const part1 = (rawInput: string) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
const rawCol = rawInput.indexOf('^')
|
||||||
|
let width = rawInput.indexOf('\n')
|
||||||
|
let position = [Math.floor(rawCol / (width + 1)), rawCol % (width + 1)]
|
||||||
|
let visited = new Set<string>()
|
||||||
|
const directions = [
|
||||||
|
[-1,0],
|
||||||
|
[0,1],
|
||||||
|
[1,0],
|
||||||
|
[0,-1]
|
||||||
|
]
|
||||||
|
let dir = 0
|
||||||
|
while(true) {
|
||||||
|
visited.add(position.join())
|
||||||
|
const nextPosition = [position[0] + directions[dir][0], position[1] + directions[dir][1]]
|
||||||
|
const next = (input[nextPosition[0]] ?? [])[nextPosition[1]]
|
||||||
|
if (next === undefined) {
|
||||||
|
break // done!
|
||||||
|
}
|
||||||
|
if (next === "#") {
|
||||||
|
dir = (dir + 1) % 4
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
position = nextPosition
|
||||||
|
}
|
||||||
|
return visited.size
|
||||||
|
}
|
||||||
|
|
||||||
|
const part2 = (rawInput: string) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
const rawCol = rawInput.indexOf('^')
|
||||||
|
let width = rawInput.indexOf('\n')
|
||||||
|
const directions = [
|
||||||
|
[-1,0],
|
||||||
|
[0,1],
|
||||||
|
[1,0],
|
||||||
|
[0,-1]
|
||||||
|
]
|
||||||
|
let obstructions = new Set<string>()
|
||||||
|
const walk = (
|
||||||
|
initialPosition: number[],
|
||||||
|
tempObstacle?: [number, number],
|
||||||
|
initialVisited = new Set<string>,
|
||||||
|
initialDir = 0
|
||||||
|
) => {
|
||||||
|
let position = [...initialPosition]
|
||||||
|
let visited = new Set(initialVisited)
|
||||||
|
let dir = initialDir
|
||||||
|
while(true) {
|
||||||
|
const key = [...position, dir].join()
|
||||||
|
if (visited.has(key)) {
|
||||||
|
// we have entered a loop!
|
||||||
|
obstructions.add(tempObstacle.join())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const nextPosition: [number, number] = [position[0] + directions[dir][0], position[1] + directions[dir][1]]
|
||||||
|
const next = (input[nextPosition[0]] ?? [])[nextPosition[1]]
|
||||||
|
if (next === undefined) {
|
||||||
|
// we are done!
|
||||||
|
return obstructions.size
|
||||||
|
}
|
||||||
|
if (tempObstacle == null && next != "#" && !obstructions.has(nextPosition.join())) {
|
||||||
|
walk(position, nextPosition, visited, dir)
|
||||||
|
}
|
||||||
|
visited.add(key)
|
||||||
|
if (next === "#" || (tempObstacle != null && nextPosition.join() === tempObstacle.join())) {
|
||||||
|
dir = (dir + 1) % 4
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
position = nextPosition
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return walk([Math.floor(rawCol / (width + 1)), rawCol % (width + 1)])
|
||||||
|
}
|
||||||
|
|
||||||
|
run({
|
||||||
|
// part1: {
|
||||||
|
// tests: [
|
||||||
|
// {
|
||||||
|
// input: `....#.....
|
||||||
|
// .........#
|
||||||
|
// ..........
|
||||||
|
// ..#.......
|
||||||
|
// .......#..
|
||||||
|
// ..........
|
||||||
|
// .#..^.....
|
||||||
|
// ........#.
|
||||||
|
// #.........
|
||||||
|
// ......#...`,
|
||||||
|
// expected: 41,
|
||||||
|
// },
|
||||||
|
// ],
|
||||||
|
// solution: part1,
|
||||||
|
// },
|
||||||
|
part2: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#..^.....
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...`,
|
||||||
|
expected: 6,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part2,
|
||||||
|
},
|
||||||
|
trimTestInputs: true,
|
||||||
|
onlyTests: false,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue