Skip to content

Commit

Permalink
day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiCeban committed Dec 3, 2023
1 parent f28fc82 commit 2635b32
Show file tree
Hide file tree
Showing 4 changed files with 1,116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Aoc2023

AOC 2023 using TDD
[AOC 2023](https://adventofcode.com/2023) using TDD

<a alt="Nx logo" href="https://nx.dev" target="_blank" rel="noreferrer"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-logo.png" width="45"></a>

Expand Down
56 changes: 56 additions & 0 deletions src/__tests__/day1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Only for testing purposes, it helps to write tests for the functions before writing the functions themselves
// declare function turnLineInto2DigitNumber(line: string): number;
// declare function turnEachLineInto2DigitNumber(lines: string): number[];
// declare function sumOf2DigitNumbers(numbers: number[]): number;
// declare function preProcess(line: string): string;

const input1 = `1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet`;

import {
preProcess,
sumOf2DigitNumbers,
turnEachLineInto2DigitNumber,
turnLineInto2DigitNumber,
} from '../day1';

import { data1 } from '../data/data1';

describe('day1', () => {
it('should turn line into a 2 digit number by taking the first and last integer to occur in the line', () => {
expect(turnLineInto2DigitNumber('1abc2')).toEqual(12);
expect(turnLineInto2DigitNumber('pqr3stu8vwx')).toEqual(38);
expect(turnLineInto2DigitNumber('a1b2c3d4e5f')).toEqual(15);
expect(turnLineInto2DigitNumber('treb7uchet')).toEqual(77);
expect(turnLineInto2DigitNumber('11abc2')).toEqual(12);
});

it('should turn each line into a 2 digit number', () => {
expect(turnEachLineInto2DigitNumber(input1)).toEqual([12, 38, 15, 77]);
});

it('correct preProcess line', () => {
expect(preProcess('one')).toEqual('1ne');
expect(preProcess('two')).toEqual('2wo');
expect(preProcess('three')).toEqual('3hree');
expect(preProcess('four')).toEqual('4our');
expect(preProcess('five')).toEqual('5ive');
expect(preProcess('six')).toEqual('6ix');
expect(preProcess('seven')).toEqual('7even');
expect(preProcess('eight')).toEqual('8ight');
expect(preProcess('nine')).toEqual('9ine');
expect(preProcess('one two three four five six seven eight nine')).toEqual(
'1ne 2wo 3hree 4our 5ive 6ix 7even 8ight 9ine'
);
});

it('should sum all the 2 digit numbers', () => {
expect(sumOf2DigitNumbers(turnEachLineInto2DigitNumber(input1))).toBe(142);
expect(sumOf2DigitNumbers(turnEachLineInto2DigitNumber(data1))).toBe(54338);
expect(
sumOf2DigitNumbers(turnEachLineInto2DigitNumber(data1, preProcess))
).toBe(53389);
});
});
Loading

0 comments on commit 2635b32

Please sign in to comment.