This commit is contained in:
Joshua Seigler 2024-12-05 11:19:38 -05:00
parent a9a646b1e1
commit e038b96e6f
No known key found for this signature in database
4 changed files with 92 additions and 15 deletions

View file

@ -47,16 +47,18 @@
},
{
"part1": {
"solved": false,
"result": null,
"attempts": [],
"time": null
"solved": true,
"result": "33950",
"attempts": [
"33990"
],
"time": 0.943417
},
"part2": {
"solved": false,
"result": null,
"solved": true,
"result": "14814534",
"attempts": [],
"time": null
"time": 0.829625
}
},
{

View file

@ -14,7 +14,7 @@
[![Day](https://badgen.net/badge/01/%E2%98%85%E2%98%85/green)](src/day01)
[![Day](https://badgen.net/badge/02/%E2%98%85%E2%98%85/green)](src/day02)
[![Day](https://badgen.net/badge/03/%E2%98%85%E2%98%86/yellow)](src/day03)
![Day](https://badgen.net/badge/04/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/04/%E2%98%85%E2%98%85/green)](src/day04)
![Day](https://badgen.net/badge/05/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/06/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/07/%E2%98%86%E2%98%86/gray)
@ -83,16 +83,16 @@ Both parts: 1.293ms
```
Day 03
Time part 1: 2.529ms
Time part 1: 2.532ms
Time part 2: -
Both parts: 2.529ms
Both parts: 2.532ms
```
```
Day 04
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 0.943ms
Time part 2: 0.83ms
Both parts: 1.773ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 5/50
Total time: 5.703ms
Total stars: 7/50
Total time: 7.478ms
```
<!--/RESULTS-->

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

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

66
src/day04/index.ts Normal file
View file

@ -0,0 +1,66 @@
import run from "aocrunner"
const parseInput = (rawInput: string) =>
rawInput.split("\n").map((line) => {
const [, b] = line.split(/:\s+/)
return b.split(" | ").map((x) => x.split(/\s+/).map(Number)) as [
number[],
number[],
]
})
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
return input.reduce((total, [winning, have]) => {
const numMatches = have.filter((x) => winning.includes(x)).length
if (numMatches === 0) {
return total
}
return total + 2 ** (numMatches - 1)
}, 0)
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
const counts = input.map((x) => 1)
input.forEach(([winning, have], row) => {
const numMatches = have.filter((x) => winning.includes(x)).length
for (let i = 0; i < numMatches; i++) {
counts[row + i + 1] += counts[row]
}
}, 0)
return counts.reduce((total, x) => total + x)
}
run({
part1: {
tests: [
{
input: `Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11`,
expected: 13,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11`,
expected: 30,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})