Skip to content

Commit cda23e4

Browse files
committed
feat(2023): Day 1 part 2 (inspired by other solutions)
1 parent 0d9d64c commit cda23e4

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

typescript/2023/01/main.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,29 @@ const getLastDigit = (line: string) => {
2929
}
3030

3131
const getSumofFirstAndLastDigits = (line: string) => {
32-
console.log(line)
33-
34-
const firstDigit = getFirstDigit(line)
35-
const lastDigit = getLastDigit(line)
36-
37-
console.log('first ', firstDigit, 'and last ', lastDigit)
38-
39-
return parseInt(firstDigit + lastDigit)
32+
return Number(getFirstDigit(line) + getLastDigit(line))
4033
}
4134

42-
const total = input.trimEnd().split('\n').reduce((prev, curr) => prev + getSumofFirstAndLastDigits(curr), 0)
43-
44-
console.log(`Result is : ${total}`)
35+
const total = testInput.trimEnd().split('\n').reduce((prev, curr) => prev + getSumofFirstAndLastDigits(curr), 0)
36+
console.log(`Result for part 1 is : ${total}`)
37+
38+
const testInput2 = `two1nine
39+
eightwothree
40+
abcone2threexyz
41+
xtwone3four
42+
4nineeightseven2
43+
zoneight234
44+
7pqrstsixteen`
45+
46+
const easyInput = input.replaceAll('one', 'o1e')
47+
.replaceAll('two', 't2o')
48+
.replaceAll('three', 't3e')
49+
.replaceAll('four', 'f4r')
50+
.replaceAll('five', 'f5e')
51+
.replaceAll('six', 's6x')
52+
.replaceAll('seven', 's7n')
53+
.replaceAll('eight', 'e8t')
54+
.replaceAll('nine', 'n9e')
55+
56+
const total2 = easyInput.trimEnd().split('\n').reduce((prev, curr) => prev + getSumofFirstAndLastDigits(curr), 0)
57+
console.log(`Result for part 2 is : ${total2}`)

0 commit comments

Comments
 (0)