This commit is contained in:
Joshua Seigler 2021-12-02 00:20:42 -05:00
parent c2dfb1d245
commit 4dcf7cb90f
4 changed files with 92 additions and 13 deletions

View file

@ -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
}
},
{

View file

@ -12,7 +12,7 @@
<!--SOLUTIONS-->
[![Day](https://badgen.net/badge/01/%E2%98%85%E2%98%85/green)](src/day01)
![Day](https://badgen.net/badge/02/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/02/%E2%98%85%E2%98%85/green)](src/day02)
![Day](https://badgen.net/badge/03/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/04/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/05/%E2%98%86%E2%98%86/gray)
@ -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
View 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
View 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,
})