@@ -3,6 +3,7 @@ package main
33import  (
44	_ "embed" 
55	"fmt" 
6+ 	"slices" 
67
78	"github.com/agrmohit/advent-of-code/internal/inputs" 
89	mathinternal "github.com/agrmohit/advent-of-code/internal/math" 
@@ -11,6 +12,30 @@ import (
1112//go:embed input.txt 
1213var  input  string 
1314
15+ // checkIsSafe takes an int row and returns whether it is safe 
16+ func  checkIsSafe (row  []int ) bool  {
17+ 	isIncreasing  :=  row [1 ] >  row [0 ]
18+ 
19+ 	for  i  :=  1 ; i  <  len (row ); i ++  {
20+ 		// If 2 consecutive numbers are same, it's unsafe 
21+ 		if  row [i ] ==  row [i - 1 ] {
22+ 			return  false 
23+ 		}
24+ 
25+ 		// If `isIncreasing` flips anytime during execution, it's unsafe 
26+ 		if  isIncreasing  !=  (row [i ] >  row [i - 1 ]) {
27+ 			return  false 
28+ 		}
29+ 
30+ 		// If absolute difference between consecutive numbers is not 1, 2 or 3 
31+ 		if  mathinternal .Abs (row [i ]- row [i - 1 ]) >  3  {
32+ 			return  false 
33+ 		}
34+ 	}
35+ 
36+ 	return  true 
37+ }
38+ 
1439func  solvePart1 (input  string ) int  {
1540	rows , err  :=  inputs .ExtractIntRows (input )
1641	if  err  !=  nil  {
@@ -20,31 +45,34 @@ func solvePart1(input string) int {
2045	safeCount  :=  0 
2146
2247	for  _ , row  :=  range  rows  {
23- 		isSafe  :=  true 
24- 		isIncreasing  :=  row [1 ] >  row [0 ]
25- 
26- 		for  i  :=  1 ; i  <  len (row ); i ++  {
27- 			// If 2 consecutive numbers are same, it's unsafe 
28- 			if  row [i ] ==  row [i - 1 ] {
29- 				isSafe  =  false 
30- 				break 
31- 			}
48+ 		if  checkIsSafe (row ) {
49+ 			safeCount ++ 
50+ 		}
51+ 	}
3252
33- 			// If `isIncreasing` flips anytime during execution, it's unsafe 
34- 			if  isIncreasing  !=  (row [i ] >  row [i - 1 ]) {
35- 				isSafe  =  false 
36- 				break 
37- 			}
53+ 	return  safeCount 
54+ }
3855
39- 			// If absolute difference between consecutive numbers is not 1, 2 or 3 
40- 			if  mathinternal .Abs (row [i ]- row [i - 1 ]) >  3  {
41- 				isSafe  =  false 
42- 				break 
43- 			}
44- 		}
56+ func  solvePart2 (input  string ) int  {
57+ 	rows , err  :=  inputs .ExtractIntRows (input )
58+ 	if  err  !=  nil  {
59+ 		panic (err )
60+ 	}
61+ 
62+ 	safeCount  :=  0 
4563
46- 		if  isSafe  {
64+ 	for  _ , row  :=  range  rows  {
65+ 		// Check whether it is safe without removing any element 
66+ 		if  checkIsSafe (row ) {
4767			safeCount ++ 
68+ 		} else  {
69+ 			// Remove each element one by one and check whether it is safe 
70+ 			for  i  :=  range  row  {
71+ 				if  checkIsSafe (slices .Concat (row [:i ], row [i + 1 :])) {
72+ 					safeCount ++ 
73+ 					break 
74+ 				}
75+ 			}
4876		}
4977	}
5078
@@ -53,6 +81,8 @@ func solvePart1(input string) int {
5381
5482func  main () {
5583	part1Solution  :=  solvePart1 (input )
84+ 	part2Solution  :=  solvePart2 (input )
5685
57- 	fmt .Println ("Day 02 Part 1 solution:" , part1Solution )
86+ 	fmt .Println ("Day 01 Part 1 solution:" , part1Solution )
87+ 	fmt .Println ("Day 01 Part 2 solution:" , part2Solution )
5888}
0 commit comments