day2
This commit is contained in:
parent
9a5602f287
commit
4fd1227924
9
abs.h
Normal file
9
abs.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
static inline int abs(int n)
|
||||
{
|
||||
if (n < 0)
|
||||
return -n;
|
||||
else
|
||||
return n;
|
||||
}
|
11
aoc.mk
11
aoc.mk
@ -21,5 +21,14 @@ DAY1_OBJ = \
|
||||
day1/input.txt.o \
|
||||
day1/solution.o
|
||||
|
||||
DAY2_OBJ = \
|
||||
day2/sample1.txt.o \
|
||||
day2/input.txt.o \
|
||||
day2/solution.o
|
||||
|
||||
DAY_OBJ = \
|
||||
$(DAY1_OBJ) \
|
||||
$(DAY2_OBJ)
|
||||
|
||||
aoc.elf: LDSCRIPT = $(LIB)/main.lds
|
||||
aoc.elf: $(START_OBJ) $(OBJ) $(DREAMCAST_OBJ) $(DAY1_OBJ)
|
||||
aoc.elf: $(START_OBJ) $(OBJ) $(DREAMCAST_OBJ) $(DAY_OBJ)
|
||||
|
@ -1,13 +1,13 @@
|
||||
#include "input.h"
|
||||
#include "parse.h"
|
||||
#include "heapsort.h"
|
||||
#include "abs.h"
|
||||
|
||||
struct list {
|
||||
int left[1000];
|
||||
int right[1000];
|
||||
};
|
||||
|
||||
int parse_input(const char * input, int length, struct list * list)
|
||||
static int parse_input(const char * input, int length, struct list * list)
|
||||
{
|
||||
const char * end = input + length;
|
||||
|
||||
@ -23,14 +23,6 @@ int parse_input(const char * input, int length, struct list * list)
|
||||
return i;
|
||||
}
|
||||
|
||||
int abs(int n)
|
||||
{
|
||||
if (n < 0)
|
||||
return -n;
|
||||
else
|
||||
return n;
|
||||
}
|
||||
|
||||
int day1_part1(char * input, int length)
|
||||
{
|
||||
struct list list;
|
||||
|
1000
day2/input.txt
Normal file
1000
day2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
15
day2/input.txt.h
Normal file
15
day2/input.txt.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_day2_input_txt_start __asm("_binary_day2_input_txt_start");
|
||||
extern uint32_t _binary_day2_input_txt_end __asm("_binary_day2_input_txt_end");
|
||||
extern uint32_t _binary_day2_input_txt_size __asm("_binary_day2_input_txt_size");
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
6
day2/sample1.txt
Normal file
6
day2/sample1.txt
Normal file
@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
15
day2/sample1.txt.h
Normal file
15
day2/sample1.txt.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_day2_sample1_txt_start __asm("_binary_day2_sample1_txt_start");
|
||||
extern uint32_t _binary_day2_sample1_txt_end __asm("_binary_day2_sample1_txt_end");
|
||||
extern uint32_t _binary_day2_sample1_txt_size __asm("_binary_day2_sample1_txt_size");
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
90
day2/solution.c
Normal file
90
day2/solution.c
Normal file
@ -0,0 +1,90 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "parse.h"
|
||||
#include "printf.h"
|
||||
#include "abs.h"
|
||||
|
||||
struct report {
|
||||
int level[16];
|
||||
int length;
|
||||
};
|
||||
|
||||
static int parse_input(const char * input, int length, struct report * report)
|
||||
{
|
||||
const char * end = input + length;
|
||||
|
||||
int i = 0;
|
||||
while (input < end) {
|
||||
report[i].length = 0;
|
||||
while (true) {
|
||||
input = parse_base10(input, &report[i].level[report[i].length++]);
|
||||
if (*input != ' ')
|
||||
break;
|
||||
input = parse_skip(input, ' ');
|
||||
}
|
||||
input = parse_skip(input, '\n');
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static bool report_safe(struct report * report, int skip)
|
||||
{
|
||||
int last_sign = 0;
|
||||
int i = (skip == 0) ? 1 : 0;
|
||||
int last = report->level[i];
|
||||
|
||||
for (++i; i < report->length; i++) {
|
||||
if (i == skip)
|
||||
continue;
|
||||
|
||||
int cur = report->level[i];
|
||||
|
||||
int rate = last - cur;
|
||||
|
||||
if (abs(rate) < 1 || abs(rate) > 3)
|
||||
return false; // unsafe
|
||||
|
||||
int sign = rate / abs(rate);
|
||||
if (last_sign != 0 && sign != last_sign)
|
||||
return false; // unsafe
|
||||
|
||||
last_sign = sign;
|
||||
last = cur;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int day2_part1(char * input, int length)
|
||||
{
|
||||
struct report report[1000];
|
||||
|
||||
int report_count = parse_input(input, length, report);
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < report_count; i++) {
|
||||
sum += (int)report_safe(&report[i], -1);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int day2_part2(char * input, int length)
|
||||
{
|
||||
struct report report[1000];
|
||||
|
||||
int report_count = parse_input(input, length, report);
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < report_count; i++) {
|
||||
for (int j = -1; j < report[i].length; j++) {
|
||||
bool safe = report_safe(&report[i], j);
|
||||
sum += (bool)safe;
|
||||
if (safe)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
@ -6,6 +6,9 @@
|
||||
#include "day1/sample1.txt.h"
|
||||
#include "day1/input.txt.h"
|
||||
|
||||
#include "day2/sample1.txt.h"
|
||||
#include "day2/input.txt.h"
|
||||
|
||||
struct start_size {
|
||||
char * start;
|
||||
uint32_t size;
|
||||
@ -14,6 +17,9 @@ struct start_size {
|
||||
static struct start_size input[] = {
|
||||
{ ( char *)&_binary_day1_input_txt_start,
|
||||
(uint32_t)&_binary_day1_input_txt_size },
|
||||
|
||||
{ ( char *)&_binary_day2_input_txt_start,
|
||||
(uint32_t)&_binary_day2_input_txt_size },
|
||||
};
|
||||
|
||||
static struct start_size sample[][2] = {
|
||||
@ -23,6 +29,12 @@ static struct start_size sample[][2] = {
|
||||
{ ( char *)&_binary_day1_sample1_txt_start,
|
||||
(uint32_t)&_binary_day1_sample1_txt_size },
|
||||
},
|
||||
{
|
||||
{ ( char *)&_binary_day2_sample1_txt_start,
|
||||
(uint32_t)&_binary_day2_sample1_txt_size },
|
||||
{ ( char *)&_binary_day2_sample1_txt_start,
|
||||
(uint32_t)&_binary_day2_sample1_txt_size },
|
||||
},
|
||||
};
|
||||
|
||||
const int input_size = (sizeof (input)) / (sizeof (input[0]));
|
||||
|
5
runner.c
5
runner.c
@ -1,14 +1,19 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "printf.h"
|
||||
#include "input.h"
|
||||
#include "runner.h"
|
||||
|
||||
int day1_part1(char * input, int length);
|
||||
int day1_part2(char * input, int length);
|
||||
int day2_part1(char * input, int length);
|
||||
int day2_part2(char * input, int length);
|
||||
|
||||
typedef int (* part_func)(char * input, int length);
|
||||
|
||||
part_func solution[][2] = {
|
||||
{day1_part1, day1_part2},
|
||||
{day2_part1, day2_part2},
|
||||
};
|
||||
const int solution_days = (sizeof (solution)) / (sizeof (solution[0]));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user