-
Notifications
You must be signed in to change notification settings - Fork 0
/
AOC2021_Day02.cls
74 lines (65 loc) · 3.07 KB
/
AOC2021_Day02.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Class to support all logic for the seconds days' challenge!
* Call as:
* AOC2021_Day02 challenge = new AOC2021_Day02( AOC_Base.MODE.EXAMPLE );
* challenge.part1();
* challenge.part2();
*
* @author Reinier van den Assum ([email protected])
* @created December 2021
*/
public with sharing class AOC2021_Day02 extends AOC_Base{
private final static String MOVEMENT_FORWARD = 'forward';
private final static String MOVEMENT_DOWN = 'down';
private final static String MOVEMENT_UP = 'up';
private static final String MOVEMENT_SPLIT = ' ';
public AOC2021_Day02( AOC_Base.MODE runmode ){
this.runmode = runmode;
this.setInputLines( 'AOC2021_Day02' );
}
public void part1(){
// Result variables
Integer horizontal = 0, depth = 0;
// Loop over input
for( Integer i = 0, j = this.inputLines.size(); i < j; i++ ){
// Split line by space to get the movement direction and the movement units
List<String> movementSplit = inputLines[ i ].split( MOVEMENT_SPLIT );
String direction = movementSplit[ 0 ];
Integer units = Integer.valueOf( movementSplit[ 1 ] ); // Note, might throw an Exception
if( String.isBlank( direction ) ){
throw new InvalidDataException( 'No Direction set for line '+ i + 1 + ': '+ movementSplit );
} else if( direction.equalsIgnoreCase( MOVEMENT_FORWARD ) ){
horizontal += units;
} else if( direction.equalsIgnoreCase( MOVEMENT_DOWN ) ){
depth += units; // Note, when going down, depth increases
} else if( direction.equalsIgnoreCase( MOVEMENT_UP ) ){
depth -= units;
}
}
System.debug( '*** Final position: '+ horizontal + ' at depth of '+ depth );
System.debug( '*** Answer part 1: '+ horizontal * depth );
}
public void part2(){
// Result variables
Integer horizontal = 0, depth = 0, aim = 0;
// Loop over input
for( Integer i = 0, j = this.inputLines.size(); i < j; i++ ){
// Split line by space to get the movement direction and the movement units
List<String> movementSplit = inputLines[ i ].split( MOVEMENT_SPLIT );
String direction = movementSplit[ 0 ];
Integer units = Integer.valueOf( movementSplit[ 1 ] ); // Note, might throw an Exception
if( String.isBlank( direction ) ){
throw new InvalidDataException( 'No Direction set for line '+ i + 1 + ': '+ movementSplit );
} else if( direction.equalsIgnoreCase( MOVEMENT_FORWARD ) ){
horizontal += units;
depth += ( aim * units );
} else if( direction.equalsIgnoreCase( MOVEMENT_DOWN ) ){
aim += units;
} else if( direction.equalsIgnoreCase( MOVEMENT_UP ) ){
aim -= units;
}
}
System.debug( '*** Final position: '+ horizontal + ' at depth of '+ depth );
System.debug( '*** Answer part 2: '+ horizontal * depth );
}
}