mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 09:06:09 +00:00
day 7 woo
This commit is contained in:
parent
ded85a988b
commit
74e3e285b5
4 changed files with 96 additions and 12 deletions
|
@ -93,16 +93,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "12553187650171",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 21.47319
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "96779702119491",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 782.404856
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
12
README.md
12
README.md
|
@ -17,7 +17,7 @@
|
||||||
[](src/day04)
|
[](src/day04)
|
||||||
[](src/day05)
|
[](src/day05)
|
||||||
[](src/day06)
|
[](src/day06)
|
||||||

|
[](src/day07)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -111,9 +111,9 @@ Both parts: 37138.281ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 07
|
Day 07
|
||||||
Time part 1: -
|
Time part 1: 21.473ms
|
||||||
Time part 2: -
|
Time part 2: 782.405ms
|
||||||
Both parts: -
|
Both parts: 803.878ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 12/50
|
Total stars: 14/50
|
||||||
Total time: 37460.931ms
|
Total time: 38264.809ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day07/README.md
Normal file
9
src/day07/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 🎄 Advent of Code 2024 - day 7 🎄
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
Task description: [link](https://adventofcode.com/2024/day/7)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
...
|
75
src/day07/index.ts
Normal file
75
src/day07/index.ts
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
import run from "aocrunner"
|
||||||
|
|
||||||
|
const parseInput = (rawInput: string) =>
|
||||||
|
rawInput.split("\n").map((line) => line.split(/[: ]+/).map(Number))
|
||||||
|
|
||||||
|
const part1 = (rawInput: string) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
|
||||||
|
return input.reduce((total, [target, ...values]) => {
|
||||||
|
const operators = [(a,b) => a * b, (a,b) => a + b]
|
||||||
|
const evaluate = (numbers: number[]) => {
|
||||||
|
if (numbers[0] > target) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (numbers.length === 1) {
|
||||||
|
return numbers[0] === target
|
||||||
|
}
|
||||||
|
for (const op of operators) {
|
||||||
|
if (evaluate([op(numbers[0], numbers[1]), ...numbers.slice(2)])) { return true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total + (evaluate(values) ? target : 0)
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
const part2 = (rawInput: string) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
|
||||||
|
return input.reduce((total, [target, ...values]) => {
|
||||||
|
const operators = [(a,b) => a * b, (a,b) => a + b, (a,b) => Number(`${a}${b}`)]
|
||||||
|
const evaluate = (numbers: number[]) => {
|
||||||
|
if (numbers[0] > target) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (numbers.length === 1) {
|
||||||
|
return numbers[0] === target
|
||||||
|
}
|
||||||
|
for (const op of operators) {
|
||||||
|
if (evaluate([op(numbers[0], numbers[1]), ...numbers.slice(2)])) { return true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total + (evaluate(values) ? target : 0)
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
run({
|
||||||
|
part1: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `190: 10 19
|
||||||
|
3267: 81 40 27
|
||||||
|
83: 17 5
|
||||||
|
156: 15 6
|
||||||
|
7290: 6 8 6 15
|
||||||
|
161011: 16 10 13
|
||||||
|
192: 17 8 14
|
||||||
|
21037: 9 7 18 13
|
||||||
|
292: 11 6 16 20`,
|
||||||
|
expected: 3749,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part1,
|
||||||
|
},
|
||||||
|
part2: {
|
||||||
|
tests: [
|
||||||
|
// {
|
||||||
|
// input: ``,
|
||||||
|
// expected: "",
|
||||||
|
// },
|
||||||
|
],
|
||||||
|
solution: part2,
|
||||||
|
},
|
||||||
|
trimTestInputs: true,
|
||||||
|
onlyTests: false,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue