mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 2
This commit is contained in:
parent
c2dfb1d245
commit
4dcf7cb90f
4 changed files with 92 additions and 13 deletions
|
@ -23,16 +23,18 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "1882980",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 2.58
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "1971232560",
|
||||
"attempts": [
|
||||
"1882980"
|
||||
],
|
||||
"time": 2.49
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -12,7 +12,7 @@
|
|||
<!--SOLUTIONS-->
|
||||
|
||||
[](src/day01)
|
||||

|
||||
[](src/day02)
|
||||

|
||||

|
||||

|
||||
|
@ -76,9 +76,9 @@ Both parts: 0.79ms
|
|||
|
||||
```
|
||||
Day 02
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 2.04ms
|
||||
Time part 2: 1.61ms
|
||||
Both parts: 3.6500000000000004ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 2/50
|
||||
Total time: 0.79ms
|
||||
Total stars: 4/50
|
||||
Total time: 4.44ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day02/README.md
Normal file
9
src/day02/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2021 - day 2 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2021/day/2)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
68
src/day02/index.js
Normal file
68
src/day02/index.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput) =>
|
||||
rawInput
|
||||
.split("\n")
|
||||
.map((l) => l.split(" "))
|
||||
.map((i) => [i[0], +i[1]])
|
||||
|
||||
const actions = {
|
||||
forward: ([h, v], x) => [h + x, v],
|
||||
down: ([h, v], x) => [h, v + x],
|
||||
up: ([h, v], x) => [h, v - x],
|
||||
}
|
||||
const actions2 = {
|
||||
forward: ([h, v, a], x) => [h + x, v + a * x, a],
|
||||
down: ([h, v, a], x) => [h, v, a + x],
|
||||
up: ([h, v, a], x) => [h, v, a - x],
|
||||
}
|
||||
|
||||
const part1 = (rawInput) => {
|
||||
const input = parseInput(rawInput)
|
||||
const pos = input.reduce(
|
||||
(acc, [command, val]) => actions[command](acc, val),
|
||||
[0, 0],
|
||||
)
|
||||
return pos[0] * pos[1]
|
||||
}
|
||||
|
||||
const part2 = (rawInput) => {
|
||||
const input = parseInput(rawInput)
|
||||
const pos = input.reduce(
|
||||
(acc, [command, val]) => actions2[command](acc, val),
|
||||
[0, 0, 0],
|
||||
)
|
||||
return pos[0] * pos[1]
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2`,
|
||||
expected: 150,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2`,
|
||||
expected: 900,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue