mirror of
https://github.com/seigler/aoc2023
synced 2025-07-26 06:16:10 +00:00
day 4
This commit is contained in:
parent
a9a646b1e1
commit
e038b96e6f
4 changed files with 92 additions and 15 deletions
|
@ -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
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
16
README.md
16
README.md
|
@ -14,7 +14,7 @@
|
|||
[](src/day01)
|
||||
[](src/day02)
|
||||
[](src/day03)
|
||||

|
||||
[](src/day04)
|
||||

|
||||

|
||||

|
||||
|
@ -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
9
src/day04/README.md
Normal 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
66
src/day04/index.ts
Normal 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,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue