mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-10-31 23:54:11 +01:00
Compare commits
3 Commits
40e3c65a5b
...
406b6fd197
Author | SHA1 | Date | |
---|---|---|---|
406b6fd197 | |||
91b75011c1 | |||
416700e310 |
11
2023/01/README.md
Normal file
11
2023/01/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# 01
|
||||
|
||||
- First, in the B, was replacing all letters, which did work for the test data, but not the input. Then I just actually searched fof the first and last occurrence.
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
<li>54605</li>
|
||||
<li>55429</li>
|
||||
</ol>
|
||||
</details>
|
60
2023/01/typescript/main.ts
Normal file
60
2023/01/typescript/main.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
// SETUP
|
||||
const INPUT = fs.readFileSync(path.join(import.meta.dir, '../input.txt'), 'utf-8').trim()
|
||||
const TEST = fs.readFileSync(path.join(import.meta.dir, '../test.txt'), 'utf-8').trim()
|
||||
|
||||
// TASK
|
||||
|
||||
function partA(input: string) {
|
||||
const total = input
|
||||
.split('\n')
|
||||
.map((line) => {
|
||||
const digits = line.split('').filter((c) => /\d/.test(c))
|
||||
return parseInt(digits[0]! + digits[digits.length - 1])
|
||||
})
|
||||
.reduce((acc, cur) => acc + cur, 0)
|
||||
console.log(total)
|
||||
}
|
||||
|
||||
function partB(input: string) {
|
||||
const names = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
|
||||
const total = input
|
||||
.split('\n')
|
||||
.map((line) => {
|
||||
let first = 0
|
||||
let last = 0
|
||||
root: for (let i = 0; i < line.length; i++) {
|
||||
for (const [num, name] of names.entries()) {
|
||||
const asString = (num + 1).toString()
|
||||
if (line[i] === asString || line.slice(i).startsWith(name)) {
|
||||
first = num + 1
|
||||
break root
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
root: for (let i = line.length; i >= 0; i--) {
|
||||
for (const [num, name] of names.entries()) {
|
||||
const asString = (num + 1).toString()
|
||||
if (line[i] === asString || line.slice(i).startsWith(name)) {
|
||||
last = num + 1
|
||||
break root
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return last + 10 * first
|
||||
})
|
||||
.reduce((acc, cur) => acc + cur, 0)
|
||||
console.log(total)
|
||||
}
|
||||
|
||||
console.log('Part A:')
|
||||
partA(TEST)
|
||||
partA(INPUT)
|
||||
|
||||
console.log('Part B:')
|
||||
partB(TEST)
|
||||
partB(INPUT)
|
@ -6,8 +6,8 @@ import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
// SETUP
|
||||
const INPUT = fs.readFileSync(path.join(__dirname, '../input.txt'), 'utf-8').trim()
|
||||
const TEST = fs.readFileSync(path.join(__dirname, '../test.txt'), 'utf-8').trim()
|
||||
const INPUT = fs.readFileSync(path.join(import.meta.dir, '../input.txt'), 'utf-8').trim()
|
||||
const TEST = fs.readFileSync(path.join(import.meta.dir, '../test.txt'), 'utf-8').trim()
|
||||
|
||||
// TASK
|
||||
|
||||
|
@ -1,6 +1,4 @@
|
||||
{
|
||||
"name": "advent-of-code",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"gen": "bun hygen riddle new"
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user