day 14, fun but strange

This commit is contained in:
Joshua Seigler 2024-12-14 00:55:25 -05:00
parent 6106f22c63
commit 992ab32a04
4 changed files with 101 additions and 11 deletions

View file

@ -209,10 +209,10 @@
},
{
"part1": {
"solved": false,
"result": null,
"solved": true,
"result": "224554908",
"attempts": [],
"time": null
"time": 1.492066
},
"part2": {
"solved": false,

View file

@ -24,7 +24,7 @@
[![Day](https://badgen.net/badge/11/%E2%98%85%E2%98%85/green)](src/day11)
[![Day](https://badgen.net/badge/12/%E2%98%85%E2%98%85/green)](src/day12)
[![Day](https://badgen.net/badge/13/%E2%98%85%E2%98%85/green)](src/day13)
![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/14/%E2%98%85%E2%98%86/yellow)](src/day14)
![Day](https://badgen.net/badge/15/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/16/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/17/%E2%98%86%E2%98%86/gray)
@ -153,16 +153,16 @@ Both parts: 387.236ms
```
Day 13
Time part 1: -
Time part 2: 7.731ms
Both parts: 7.731ms
Time part 1: 1.095ms
Time part 2: 0.818ms
Both parts: 1.913ms
```
```
Day 14
Time part 1: -
Time part 1: 1.507ms
Time part 2: -
Both parts: -
Both parts: 1.507ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 26/50
Total time: 38909.996ms
Total stars: 27/50
Total time: 38905.685ms
```
<!--/RESULTS-->

9
src/day14/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2024 - day 14 🎄
## Info
Task description: [link](https://adventofcode.com/2024/day/14)
## Notes
...

81
src/day14/index.ts Normal file
View file

@ -0,0 +1,81 @@
import run from "aocrunner"
const parseInput = (rawInput: string) => rawInput.split("\n")
.map((l) => Array.from(l.matchAll(/-?\d+/g)).map(Number))
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
const width = 101
const height = 103
let quadrantCounts = [0,0,0,0] // TL, TR, BR, BL
const halfX = (width - 1) / 2
const halfY = (height - 1) / 2
input.forEach(([ix, iy, dx, dy]) => {
const destx = (ix + (width + dx) * 100) % width
const desty = (iy + (height + dy) * 100) % height
if (desty === halfY || destx === halfX) { return }
quadrantCounts[
(desty > halfY ? 2 : 0) + (destx > halfX ? 1 : 0)
] += 1
})
return quadrantCounts.reduce((total, here) => {
return total * here
}, 1)
}
const part2 = (rawInput: string) => {
(async () => {
const input = parseInput(rawInput)
const width = 101
const height = 103
for (let i = 0; i < 20000; i++) {
const canvas: string[][] = []
for (let y = 0; y < height; y++) {
canvas.push(" ".repeat(width).split(''))
}
input.forEach(([ix, iy, dx, dy]) => {
const destx = (ix + (width + dx) * i) % width
const desty = (iy + (height + dy) * i) % height
canvas[desty][destx] = "#"
})
if (canvas.some(line => line.join('').includes("##########"))) {
await new Promise(resolve => setTimeout(resolve, 1000))
console.log(`\nTIME: ${i} seconds\n${canvas.map(line => console.log(line.join(""))).join('\n')}`)
}
}
})()
}
run({
part1: {
tests: [
// {
// input: `p=0,4 v=3,-3
// p=6,3 v=-1,-3
// p=10,3 v=-1,2
// p=2,0 v=2,-1
// p=0,0 v=1,3
// p=3,0 v=-2,-2
// p=7,6 v=-1,-3
// p=3,0 v=-1,-2
// p=9,3 v=2,3
// p=7,3 v=-1,2
// p=2,4 v=2,-3
// p=9,5 v=-3,-3`,
// expected: 12,
// },
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})