mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 09:06:09 +00:00
day 8
This commit is contained in:
parent
22ce365434
commit
8ca6f75820
4 changed files with 159 additions and 17 deletions
|
@ -107,16 +107,21 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "390",
|
||||||
"attempts": [],
|
"attempts": [
|
||||||
"time": null
|
"403"
|
||||||
|
],
|
||||||
|
"time": 0.758513
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "1246",
|
||||||
"attempts": [],
|
"attempts": [
|
||||||
"time": null
|
"1188",
|
||||||
|
"1177"
|
||||||
|
],
|
||||||
|
"time": 1.104361
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
18
README.md
18
README.md
|
@ -18,7 +18,7 @@
|
||||||
[](src/day05)
|
[](src/day05)
|
||||||
[](src/day06)
|
[](src/day06)
|
||||||
[](src/day07)
|
[](src/day07)
|
||||||

|
[](src/day08)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -111,16 +111,16 @@ Both parts: 37138.281ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 07
|
Day 07
|
||||||
Time part 1: 21.473ms
|
Time part 1: 23.012ms
|
||||||
Time part 2: 782.405ms
|
Time part 2: 599.069ms
|
||||||
Both parts: 803.878ms
|
Both parts: 622.08ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 08
|
Day 08
|
||||||
Time part 1: -
|
Time part 1: 0.759ms
|
||||||
Time part 2: -
|
Time part 2: 1.104ms
|
||||||
Both parts: -
|
Both parts: 1.863ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 14/50
|
Total stars: 16/50
|
||||||
Total time: 38264.809ms
|
Total time: 38084.874ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day08/README.md
Normal file
9
src/day08/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 🎄 Advent of Code 2024 - day 8 🎄
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
Task description: [link](https://adventofcode.com/2024/day/8)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
...
|
128
src/day08/index.ts
Normal file
128
src/day08/index.ts
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
import run from "aocrunner"
|
||||||
|
|
||||||
|
const parseInput = (rawInput: string) => rawInput.split('\n').map(line => line.split(''))
|
||||||
|
|
||||||
|
const part1 = (rawInput: string) => {
|
||||||
|
const rows = parseInput(rawInput)
|
||||||
|
const towers = new Map<string, string[]>() // 'a' => ['4,5', '7,7'] etc
|
||||||
|
const antinodes = new Set<string>()
|
||||||
|
rows.forEach((row, r) => {
|
||||||
|
row.forEach((char, c) => {
|
||||||
|
if (char === '.') return
|
||||||
|
const towerLocations = towers.get(char) ?? []
|
||||||
|
towerLocations.forEach(locationKey => {
|
||||||
|
const [r2,c2] = locationKey.split(',').map(Number)
|
||||||
|
const newAntinodes = [[2*r2-r, 2*c2-c], [2*r-r2, 2*c-c2]]
|
||||||
|
newAntinodes.forEach(([y, x]) => {
|
||||||
|
if (y < 0 || y >= rows.length || x < 0 || x >= row.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
antinodes.add(`${y},${x}`)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
towerLocations.push(`${r},${c}`)
|
||||||
|
towers.set(char, towerLocations)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return antinodes.size
|
||||||
|
}
|
||||||
|
|
||||||
|
const part2 = (rawInput: string) => {
|
||||||
|
const rows = parseInput(rawInput)
|
||||||
|
const towers = new Map<string, string[]>() // 'a' => ['4,5', '7,7'] etc
|
||||||
|
const antinodes = new Set<string>()
|
||||||
|
rows.forEach((row, r) => {
|
||||||
|
row.forEach((char, c) => {
|
||||||
|
if (char === '.') return
|
||||||
|
const towerLocations = towers.get(char) ?? []
|
||||||
|
towerLocations.forEach(locationKey => {
|
||||||
|
const [r2,c2] = locationKey.split(',').map(Number)
|
||||||
|
const slopeTop = c2-c
|
||||||
|
const slopeBottom = r2-r
|
||||||
|
for (let R = 0; R < rows.length; R++) {
|
||||||
|
const dr = R - r
|
||||||
|
const dc = dr * slopeTop / slopeBottom
|
||||||
|
const C = c + dc
|
||||||
|
if (C < 0 || C >= row.length || C % 1 !== 0) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
antinodes.add(`${R},${C}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
towerLocations.push(`${r},${c}`)
|
||||||
|
towers.set(char, towerLocations)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return antinodes.size
|
||||||
|
}
|
||||||
|
|
||||||
|
run({
|
||||||
|
part1: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
....a.....
|
||||||
|
........a.
|
||||||
|
.....a....
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........`,
|
||||||
|
expected: 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............`,
|
||||||
|
expected: 14,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part1,
|
||||||
|
},
|
||||||
|
part2: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `T.........
|
||||||
|
...T......
|
||||||
|
.T........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........`,
|
||||||
|
expected: 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............`,
|
||||||
|
expected: 34,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part2,
|
||||||
|
},
|
||||||
|
trimTestInputs: true,
|
||||||
|
onlyTests: false,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue