Created
December 2, 2023 05:46
-
-
Save nickforce/3b8c54904740805c0548343c69c63015 to your computer and use it in GitHub Desktop.
day01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class day01 { | |
public static Map<String,String> numberMap = new Map<String,String>{ | |
'one' => '1', | |
'two' => '2', | |
'three' => '3', | |
'four' => '4', | |
'five' => '5', | |
'six' => '6', | |
'seven' => '7', | |
'eight' => '8', | |
'nine' => '9', | |
'1' => '1', | |
'2' => '2', | |
'3' => '3', | |
'4' => '4', | |
'5' => '5', | |
'6' => '6', | |
'7' => '7', | |
'8' => '8', | |
'9' => '9' | |
}; | |
public static Integer part1(List<String> puzzleInputLines) { | |
System.debug(puzzleInputLines); | |
Integer total = 0; | |
for(String line : puzzleInputLines) { | |
System.debug(line); | |
String first = firstMatch(line); | |
String last = lastMatch(line); | |
String fullMatch = first + last; | |
total += Integer.valueOf(fullMatch); | |
} | |
return total; | |
} | |
public static String firstMatch(String line) { | |
String returnVal; | |
// Iterate through each character in the line (first match) | |
for (Integer i = 0; i < line.length(); i++) { | |
// Get the character at the current index | |
String currentChar = line.substring(i, i + 1); | |
if(numberMap.get(currentChar) != null) { | |
returnVal = numberMap.get(currentChar); | |
break; | |
} | |
} | |
return returnVal; | |
} | |
public static String lastMatch(String line) { | |
String returnVal; | |
// Iterate through each character in the line in reverse order (last match) | |
for (Integer i = line.length() - 1; i >= 0; i--) { | |
// Get the character at the current index | |
String currentChar = line.substring(i, i + 1); | |
if(numberMap.get(currentChar) != null) { | |
returnVal = numberMap.get(currentChar); | |
break; | |
} | |
} | |
return returnVal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment