This commit is contained in:
Joshua Seigler 2024-12-08 14:33:35 -05:00
parent 22ce365434
commit 8ca6f75820
4 changed files with 159 additions and 17 deletions

View file

@ -107,16 +107,21 @@
},
{
"part1": {
"solved": false,
"result": null,
"attempts": [],
"time": null
"solved": true,
"result": "390",
"attempts": [
"403"
],
"time": 0.758513
},
"part2": {
"solved": false,
"result": null,
"attempts": [],
"time": null
"solved": true,
"result": "1246",
"attempts": [
"1188",
"1177"
],
"time": 1.104361
}
},
{

View file

@ -18,7 +18,7 @@
[![Day](https://badgen.net/badge/05/%E2%98%85%E2%98%85/green)](src/day05)
[![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%85/green)](src/day06)
[![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](src/day07)
![Day](https://badgen.net/badge/08/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](src/day08)
![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/10/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/11/%E2%98%86%E2%98%86/gray)
@ -111,16 +111,16 @@ Both parts: 37138.281ms
```
Day 07
Time part 1: 21.473ms
Time part 2: 782.405ms
Both parts: 803.878ms
Time part 1: 23.012ms
Time part 2: 599.069ms
Both parts: 622.08ms
```
```
Day 08
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 0.759ms
Time part 2: 1.104ms
Both parts: 1.863ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 14/50
Total time: 38264.809ms
Total stars: 16/50
Total time: 38084.874ms
```
<!--/RESULTS-->

9
src/day08/README.md Normal file
View 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
View 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,
})