mirror of
https://github.com/seigler/aoc2022
synced 2025-07-25 14:16:10 +00:00
Day 10, console art
This commit is contained in:
parent
44ece06914
commit
cac2da9199
4 changed files with 262 additions and 8 deletions
|
@ -131,10 +131,10 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "16880",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 0.137569
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
|
|
10
README.md
10
README.md
|
@ -20,7 +20,7 @@
|
|||
[](src/day07)
|
||||
[](src/day08)
|
||||
[](src/day09)
|
||||

|
||||
[](src/day10)
|
||||

|
||||

|
||||

|
||||
|
@ -132,9 +132,9 @@ Both parts: 27.610987ms
|
|||
|
||||
```
|
||||
Day 10
|
||||
Time part 1: -
|
||||
Time part 1: 0.179ms
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Both parts: 0.178526ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 18/50
|
||||
Total time: 68.695ms
|
||||
Total stars: 19/50
|
||||
Total time: 68.874ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day10/README.md
Normal file
9
src/day10/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2022 - day 10 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2022/day/10)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
245
src/day10/index.ts
Normal file
245
src/day10/index.ts
Normal file
|
@ -0,0 +1,245 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
type instructionType =
|
||||
| { op: "noop"; arg: undefined }
|
||||
| { op: "addx"; arg: number }
|
||||
|
||||
const delays = {
|
||||
noop: 1,
|
||||
addx: 2,
|
||||
} as const
|
||||
|
||||
function parseInput(rawInput: string): instructionType[] {
|
||||
return rawInput.split("\n").map((l) => {
|
||||
const [op, arg] = l.split(" ")
|
||||
if (op === "noop") return { op: "noop", arg: undefined }
|
||||
return { op: "addx", arg: +arg }
|
||||
})
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
const isCycleSampled = (cycle: number) => (cycle - 20) % 40 === 0
|
||||
const registers = { x: 1 }
|
||||
let sum = 0
|
||||
let cycle = 0
|
||||
for (const { op, arg } of input) {
|
||||
for (let i = 0; i < delays[op]; i++) {
|
||||
cycle++
|
||||
if (isCycleSampled(cycle)) {
|
||||
sum += cycle * registers.x
|
||||
}
|
||||
}
|
||||
// between cycles
|
||||
if (op === "addx") {
|
||||
registers.x += arg
|
||||
} else if (op === "noop") {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
const registers = { x: 1 }
|
||||
let cycle = 0
|
||||
let display = ""
|
||||
for (const { op, arg } of input) {
|
||||
for (let i = 0; i < delays[op]; i++) {
|
||||
if ([-1, 0, 1].includes(registers.x - (cycle % 40))) {
|
||||
display += "#"
|
||||
} else {
|
||||
display += "."
|
||||
}
|
||||
cycle++
|
||||
if (cycle % 40 === 0) {
|
||||
display += "\n"
|
||||
}
|
||||
}
|
||||
// between cycles
|
||||
if (op === "addx") {
|
||||
registers.x += arg
|
||||
} else if (op === "noop") {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
console.log(display)
|
||||
return
|
||||
}
|
||||
|
||||
const largeExample = `addx 15
|
||||
addx -11
|
||||
addx 6
|
||||
addx -3
|
||||
addx 5
|
||||
addx -1
|
||||
addx -8
|
||||
addx 13
|
||||
addx 4
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx -35
|
||||
addx 1
|
||||
addx 24
|
||||
addx -19
|
||||
addx 1
|
||||
addx 16
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
addx 21
|
||||
addx -15
|
||||
noop
|
||||
noop
|
||||
addx -3
|
||||
addx 9
|
||||
addx 1
|
||||
addx -3
|
||||
addx 8
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -36
|
||||
noop
|
||||
addx 1
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
noop
|
||||
addx -13
|
||||
addx 13
|
||||
addx 7
|
||||
noop
|
||||
addx 1
|
||||
addx -33
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 8
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx 17
|
||||
addx -9
|
||||
addx 1
|
||||
addx 1
|
||||
addx -3
|
||||
addx 11
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx -13
|
||||
addx -19
|
||||
addx 1
|
||||
addx 3
|
||||
addx 26
|
||||
addx -30
|
||||
addx 12
|
||||
addx -1
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -9
|
||||
addx 18
|
||||
addx 1
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 9
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx -37
|
||||
addx 1
|
||||
addx 3
|
||||
noop
|
||||
addx 15
|
||||
addx -21
|
||||
addx 22
|
||||
addx -6
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -10
|
||||
noop
|
||||
noop
|
||||
addx 20
|
||||
addx 1
|
||||
addx 2
|
||||
addx 2
|
||||
addx -6
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
noop`
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
// {
|
||||
// input: `noop
|
||||
// addx 3
|
||||
// addx -5`,
|
||||
// expected: 0,
|
||||
// },
|
||||
{
|
||||
input: largeExample,
|
||||
expected: 13140,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: largeExample,
|
||||
expected: "",
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue