mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 7
This commit is contained in:
parent
a771143991
commit
64bea87901
4 changed files with 66 additions and 13 deletions
|
@ -95,16 +95,18 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "345035",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 0.71
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "97038163",
|
||||
"attempts": [
|
||||
"97038219"
|
||||
],
|
||||
"time": 46.18
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -17,7 +17,7 @@
|
|||
[](src/day04)
|
||||
[](src/day05)
|
||||
[](src/day06)
|
||||

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

|
||||

|
||||

|
||||
|
@ -111,9 +111,9 @@ Both parts: 25.08ms
|
|||
|
||||
```
|
||||
Day 07
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 0.71ms
|
||||
Time part 2: 46.18ms
|
||||
Both parts: 46.89ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 12/50
|
||||
Total time: 331.78999999999996ms
|
||||
Total stars: 14/50
|
||||
Total time: 378.67999999999995ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day07/README.md
Normal file
9
src/day07/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2021 - day 7 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2021/day/7)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
42
src/day07/index.js
Normal file
42
src/day07/index.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput) =>
|
||||
rawInput.trim().split`,`.map((x) => +x).sort((a, b) => a - b)
|
||||
|
||||
const part1 = (rawInput) => {
|
||||
const input = parseInput(rawInput)
|
||||
const median = input[Math.floor(input.length / 2)]
|
||||
return input.reduce((acc, cur) => acc + Math.abs(cur - median), 0)
|
||||
}
|
||||
|
||||
const part2 = (rawInput) => {
|
||||
const input = parseInput(rawInput)
|
||||
const min = input[0],
|
||||
max = input[input.length - 1]
|
||||
const gas = (position) =>
|
||||
input.reduce(
|
||||
(acc, cur) =>
|
||||
acc + ((1 + Math.abs(cur - position)) * Math.abs(cur - position)) / 2,
|
||||
0,
|
||||
)
|
||||
let bestGas = Infinity
|
||||
for (let pos = min; pos <= max; pos++) {
|
||||
const thisGas = gas(pos)
|
||||
if (thisGas < bestGas) {
|
||||
bestGas = thisGas
|
||||
}
|
||||
}
|
||||
return bestGas
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [{ input: `16,1,2,0,4,2,7,1,2,14`, expected: 37 }],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [{ input: `16,1,2,0,4,2,7,1,2,14`, expected: 168 }],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue