2022 Day 10 part 1
This commit is contained in:
parent
400efd5802
commit
35fe136562
1 changed files with 73 additions and 0 deletions
73
2022/day10.m
Normal file
73
2022/day10.m
Normal file
|
@ -0,0 +1,73 @@
|
|||
// clang day10.m -fmodules -o day10 && ./day10
|
||||
|
||||
@import Foundation;
|
||||
|
||||
@interface CPUState: NSObject
|
||||
|
||||
@property (readonly, nonatomic) int signalSum;
|
||||
|
||||
- (void)noop;
|
||||
- (void)addX: (int)value;
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPUState {
|
||||
int x;
|
||||
int counter;
|
||||
int *nextSample;
|
||||
}
|
||||
|
||||
static int samplePositions[] = {20, 60, 100, 140, 180, 220, -1};
|
||||
|
||||
- init;
|
||||
{
|
||||
self = [super init];
|
||||
x = 1;
|
||||
counter = 0;
|
||||
nextSample = samplePositions;
|
||||
_signalSum = 0;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)step;
|
||||
{
|
||||
counter++;
|
||||
if (counter == *nextSample) {
|
||||
_signalSum += counter * x;
|
||||
++nextSample;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)noop;
|
||||
{
|
||||
[self step];
|
||||
}
|
||||
|
||||
- (void)addX: (int)value;
|
||||
{
|
||||
[self step];
|
||||
[self step];
|
||||
x += value;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
NSScanner *scanner = [NSScanner scannerWithString: [NSString stringWithContentsOfFile: @"day10.input" encoding: NSUTF8StringEncoding error: NULL]];
|
||||
|
||||
CPUState *state = [[CPUState alloc] init];
|
||||
|
||||
while (![scanner isAtEnd]) {
|
||||
int argument = 0;
|
||||
if ([scanner scanString: @"noop" intoString: NULL]) {
|
||||
[state noop];
|
||||
} else if ([scanner scanString: @"addx" intoString: NULL] && [scanner scanInt: &argument]) {
|
||||
[state addX: argument];
|
||||
}
|
||||
}
|
||||
|
||||
NSLog(@"Part 1: %d", state.signalSum);
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue