File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { describe , expect , test } from "bun:test" ;
2
+ import { cubeConundrumPartOne , cubeConundrumPartTwo } from "./cube-conundrum" ;
3
+
4
+ const example = Bun . file ( "src/day-02/example.txt" ) ;
5
+ const input = Bun . file ( "src/day-02/input.txt" ) ;
6
+
7
+ const content_input = await input . text ( ) ;
8
+ const content_example = await example . text ( ) ;
9
+
10
+ type maxCountType = {
11
+ [ key : string ] : number ;
12
+ } ;
13
+
14
+ const maxCount : maxCountType = {
15
+ red : 12 ,
16
+ green : 13 ,
17
+ blue : 14 ,
18
+ } ;
19
+
20
+ describe ( "Day 02, cube conundrum" , ( ) => {
21
+ describe ( "Part One" , ( ) => {
22
+ test ( "it should return 8, example" , ( ) => {
23
+ const result = cubeConundrumPartOne ( content_example ) ;
24
+ expect ( result ) . toEqual ( 8 ) ;
25
+ } ) ;
26
+ test ( "it should return 2716, input" , ( ) => {
27
+ const result = cubeConundrumPartOne ( content_input ) ;
28
+ expect ( result ) . toEqual ( 2716 ) ;
29
+ } ) ;
30
+ } ) ;
31
+ } ) ;
Original file line number Diff line number Diff line change
1
+ type maxCountType = {
2
+ [ key : string ] : number ;
3
+ } ;
4
+
5
+ const maxCount : maxCountType = {
6
+ red : 12 ,
7
+ green : 13 ,
8
+ blue : 14 ,
9
+ } ;
10
+
11
+ export function cubeConundrumPartOne ( content : string ) : number {
12
+ // Split the content into lines
13
+ const lines = content . trim ( ) . split ( "\n" ) ;
14
+
15
+ // Process each line
16
+ return (
17
+ lines
18
+ . map ( ( line ) => {
19
+ // Split the line into sets
20
+ const sets = line . split ( ": " ) [ 1 ] . split ( "; " ) ;
21
+
22
+ // Check if all pulls in a set are valid
23
+ const areAllPullsValid = sets . map ( ( set ) => {
24
+ const pulls = set . split ( ", " ) ;
25
+
26
+ // Check if a pull is valid
27
+ return pulls . every ( ( pull ) => {
28
+ const [ count , color ] = pull . split ( " " ) ;
29
+ return maxCount [ color ] >= Number ( count ) ;
30
+ } ) ;
31
+ } ) ;
32
+
33
+ // Check if all sets are valid
34
+ return areAllPullsValid . every ( ( p ) => p ) ;
35
+ } )
36
+ // Sum up the results
37
+ . reduce ( ( sum , result , index ) => {
38
+ return result ? sum + ( index + 1 ) : sum ;
39
+ } , 0 )
40
+ ) ;
41
+ }
You can’t perform that action at this time.
0 commit comments