44	_ "embed" 
55	"fmt" 
66	"log" 
7+ 	"strconv" 
78	"strings" 
89
910	"github.com/agrmohit/aoc/internal/inputs" 
@@ -12,6 +13,12 @@ import (
1213//go:embed input.txt 
1314var  input  string 
1415
16+ func  concat (a  int , b  int ) int  {
17+ 	result , _  :=  strconv .Atoi (strconv .Itoa (a ) +  strconv .Itoa (b ))
18+ 
19+ 	return  result 
20+ }
21+ 
1522// doesSolutionExist checks whether a solution exists recursively 
1623func  doesSolutionExist (result  int , current  int , rest  []int ) bool  {
1724	// Exit condition 
@@ -25,6 +32,19 @@ func doesSolutionExist(result int, current int, rest []int) bool {
2532	return  doesSolutionExist (result , current + current1 , rest1 ) ||  doesSolutionExist (result , current * current1 , rest1 )
2633}
2734
35+ // doesSolutionExist checks whether a solution exists recursively, including concat operator 
36+ func  doesSolutionExist2 (result  int , current  int , rest  []int ) bool  {
37+ 	// Exit condition 
38+ 	if  len (rest ) ==  0  {
39+ 		return  result  ==  current 
40+ 	}
41+ 
42+ 	current1  :=  rest [0 ]
43+ 	rest1  :=  rest [1 :]
44+ 
45+ 	return  doesSolutionExist2 (result , current + current1 , rest1 ) ||  doesSolutionExist2 (result , current * current1 , rest1 ) ||  doesSolutionExist2 (result , concat (current , current1 ), rest1 )
46+ }
47+ 
2848func  solvePart1 (input  string ) int  {
2949	input  =  strings .ReplaceAll (input , ":" , "" )
3050	rows , err  :=  inputs .ExtractIntRows (input )
@@ -43,8 +63,28 @@ func solvePart1(input string) int {
4363	return  solutionExistsSum 
4464}
4565
66+ func  solvePart2 (input  string ) int  {
67+ 	input  =  strings .ReplaceAll (input , ":" , "" )
68+ 	rows , err  :=  inputs .ExtractIntRows (input )
69+ 	if  err  !=  nil  {
70+ 		log .Fatalf ("ERROR: %v" , err )
71+ 	}
72+ 
73+ 	solutionExistsSum  :=  0 
74+ 
75+ 	for  _ , line  :=  range  rows  {
76+ 		if  doesSolutionExist2 (line [0 ], line [1 ], line [2 :]) {
77+ 			solutionExistsSum  +=  line [0 ]
78+ 		}
79+ 	}
80+ 
81+ 	return  solutionExistsSum 
82+ }
83+ 
4684func  main () {
4785	part1Solution  :=  solvePart1 (input )
86+ 	part2Solution  :=  solvePart2 (input )
4887
4988	fmt .Println ("Day 07 Part 1 solution:" , part1Solution )
89+ 	fmt .Println ("Day 07 Part 2 solution:" , part2Solution )
5090}
0 commit comments