Compare commits

...

2 Commits

Author SHA1 Message Date
e8010222bd day 7 2024-12-07 01:58:08 -06:00
0e83033a4a day 6 2024-12-07 01:58:05 -06:00
31 changed files with 2197 additions and 88 deletions

3
aoc.mk
View File

@ -14,7 +14,8 @@ DREAMCAST_OBJ = \
$(LIB)/holly/ta_fifo_polygon_converter.o \
$(LIB)/holly/video_output.o \
$(LIB)/font/dejavusansmono/dejavusansmono.data.o \
$(LIB)/sh7091/serial.o
$(LIB)/sh7091/serial.o \
$(LIB)/maple/maple.o
include solutions.mk

View File

@ -1,3 +1,5 @@
#include <stdint.h>
#include "parse.h"
#include "heapsort.h"
#include "abs.h"
@ -23,7 +25,7 @@ static int parse_input(const char * input, int length, struct list * list)
return i;
}
int day1_part1(char * input, int length)
int64_t day1_part1(const char * input, int length)
{
struct list list;
int list_length = parse_input(input, length, &list);
@ -40,7 +42,7 @@ int day1_part1(char * input, int length)
return sum;
}
int day1_part2(char * input, int length)
int64_t day1_part2(const char * input, int length)
{
struct list list;
int list_length = parse_input(input, length, &list);

View File

@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdbool.h>
#include "parse.h"
@ -53,7 +54,7 @@ static bool report_safe(struct report * report, int skip)
if (abs(rate) < 1 || abs(rate) > 3)
return false; // unsafe
int sign = int_sign(sign);
int sign = int_sign(rate);
if (last_sign != 0 && sign != last_sign)
return false; // unsafe
@ -64,7 +65,7 @@ static bool report_safe(struct report * report, int skip)
return true;
}
int day2_part1(char * input, int length)
int64_t day2_part1(const char * input, int length)
{
struct report report[1000];
@ -78,7 +79,7 @@ int day2_part1(char * input, int length)
return sum;
}
int day2_part2(char * input, int length)
int64_t day2_part2(const char * input, int length)
{
struct report report[1000];

View File

@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
@ -88,7 +89,7 @@ int parse_input(const char * input, int length, struct instruction * ins)
return i;
}
int day3_part1(char * input, int length)
int64_t day3_part1(const char * input, int length)
{
struct instruction ins[1000];
@ -103,7 +104,7 @@ int day3_part1(char * input, int length)
return sum;
}
int day3_part2(char * input, int length)
int64_t day3_part2(const char * input, int length)
{
struct instruction ins[1000];

View File

@ -1,31 +1,8 @@
#include <stdint.h>
#include <stdbool.h>
#include "printf.h"
static int parse_stride(char * input, int length)
{
for (int i = 0; i < length; i++) {
if (input[i] == '\n') {
return i + 1;
}
}
return -1;
}
static int parse_height(char * input, int length)
{
int rows = 0;
int i;
for (i = 0; i < length; i++) {
if (input[i] == '\n') {
rows += 1;
}
}
if (input[i-1] != '\n') {
rows += 1;
}
return rows;
}
#include "parse.h"
enum offset_type {
N,
@ -61,7 +38,7 @@ static bool xy_inside(int width, int height,
return true;
}
static char get_char(char * input,
static char get_char(const char * input,
int stride,
int x, int y)
{
@ -69,7 +46,7 @@ static char get_char(char * input,
return input[ix];
}
static bool offset_match(char * input,
static bool offset_match(const char * input,
int stride,
int width, int height,
int x, int y,
@ -92,7 +69,7 @@ static bool offset_match(char * input,
return true;
}
static bool offset_match_centered(char * input,
static bool offset_match_centered(const char * input,
int stride,
int width, int height,
int x, int y,
@ -117,7 +94,7 @@ static bool offset_match_centered(char * input,
str_length);
}
static bool offset_match_cross(char * input,
static bool offset_match_cross(const char * input,
int stride,
int width, int height,
int x, int y,
@ -155,7 +132,7 @@ static bool offset_match_cross(char * input,
return match1 || match2;
}
int day4_part1(char * input, int length)
int64_t day4_part1(const char * input, int length)
{
int stride = parse_stride(input, length);
int height = parse_height(input, length);
@ -183,7 +160,7 @@ int day4_part1(char * input, int length)
return match_count;
}
int day4_part2(char * input, int length)
int64_t day4_part2(const char * input, int length)
{
int stride = parse_stride(input, length);
int height = parse_height(input, length);

View File

@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdbool.h>
#include "parse.h"
@ -115,12 +116,12 @@ static int solve(const char * input, int length, bool correctly_ordered)
return sum;
}
int day5_part1(const char * input, int length)
int64_t day5_part1(const char * input, int length)
{
return solve(input, length, true);
}
int day5_part2(const char * input, int length)
int64_t day5_part2(const char * input, int length)
{
return solve(input, length, false);
}

130
day6/input.txt Normal file
View File

@ -0,0 +1,130 @@
.........................#...........#.#...#...............#..............##.....................#...........#..#.........#.......
.....................#.................#.......#.........................#...........#.#.......#..................................
.........#...........#........#.........#.........#...........#........................................#.........................#
.......#.........................#...........................#.............#......................................................
..................#................##...............#.#.....................##............#.......................................
......#...#................#..................................................................#....#.#............................
.......##................#........#..#.......#......................................#........#............#......###..........#...
.....................#....................................................#...........#..#...#..........................#.........
...........#....#...........................#.#...............#..#....................................#..............#......#.....
.....#........##...............#.......#..#..............................................#..........................#.............
.#.........#.#...........................#..........................#......................#...........#........#.................
.................................................................#..#.#.......#..#................................................
...............................................#.......#...................#....................#.................................
.......#......#......#......................#...............................#.....##........#...........#......#..................
........................................................................................................#.....#.#.................
..#...............................#.#..................................#.............#............................................
..............#..........................................#.....#.....#..........................#....#.........#..................
...................................#........................................#................#...........##.....#....#............
.#.....................................#.......#........................#...................##............#.......................
.#..................#...........#...........................................................................................#.....
........................#...................................................##.................................#..#..........##...
........#............................#.......#...........................#.#...#...................................#.....#........
...............................#............#.....#......................#.......#...#........................#...................
.........#.............#.................................................#.......................................#....#...........
...................................................................#...#.................................#...................#....
...#.....................#...#...............#..................................................#...#...#.........................
...................#........#....#....#..#..#...................#.........#..................#..#....#.......#.#.................#
..............................#........................................#......#.....#..............##................#............
............#..#.............#.......#........................................................##........#.....#...................
.....#.............................................................#...#.#.#.............................#..#.#...........#.......
.......#....#............................................#.................................#...................................#..
..#.................#.......#..........................#........................................#...............#..............#..
....#.#...................#............................#..#.......................................................................
.......................#...............#..............#......#......................#.....................#..............#........
.#..........#.......................................................................#....................................#........
.#.......#..................................................................#.....................................................
..................................................#.............#.............................................#................#.#
.......................#......#....#................#......................................................#.......#.........#....
..........................#.#........................................................#......#................................#....
....................................................................#...............................#.............................
..#...........#..............................#..#....#............................#...............##..............#...#...#...#..#
...................................#................................................................#....#...................#....
..#....#........#..##....#........................#...#...................................#.......................................
...................#................................................................................................#.............
.......#........#................#...#.......................................................................#....................
..............#....................#..........................##.........#.....................................#..................
.........................................................................#.................#........................#.............
.............................#..........#.......................................................#......................#..........
............###....................................................................#.......................#......................
........#........#...........#.............................#....................#..........#.........................#.#.....#....
.........................................................................................................#..#.....................
#.#.....#..........................................................#.....................#........................................
........##..........#....................................................#..#........#.......#........#................#..........
.............................................#..........................#.........#........................#......................
...........................##.............#........................#.#........................#..#................#.....#........#
#...#..............................................#........#...................................................#.................
.............#................#............................#............#.........................................................
#..........................#............#.....................#.....#.................#......#...##...#.............#.#...........
.....................#...#..#....#...................................#....................#..............#........#...............
.......................#.................#...........#....#.......#...........................................#..#................
.........#...........#.#.........#.........................#.....#................................................................
....................#........#...................................................................#............#..#.#.#............
..........#.......................#.......................#.......................................................................
......................................#......#............................#..............................................#........
.....................#...#.................#.............................#....#...................................................
..............##...................................................................#......................................##......
.....#....#...............................................#.......#.##........#.#.............#...............#...................
....................................#......................#........#.......................................................#.....
.....#.........................................#.....................#.............#.................#..................#.........
................................#.........#..#............................................#.........................#.............
....#.#.....................#......#...................................#..........................................#..#.....#...#..
.........##.......................#..........................................#.........................#...##.....................
...............................................................#................................................................#.
............#.........................................##.......................................#..................................
.............................#..............................#..................#.........#.....#..................................
..........................................................#...#.....#..................#................................#.........
......#...................#.........................................................................................#.............
#...............................#...........................#.....................................................................
.......#........#.................................................#.......................................................#.......
.#......................................................#..............#..................................#...........#...........
.............#..#.#.......................#..........#......#.##.........................................................#..#.....
.......#...............#........#......................................................................#..#...........#...........
............#.........................................#................................................#...................#......
............#...........................................#.#.......#....................................#..........................
...........................................#.........................................................#..............#.............
..................#..#....................................................#..................#....................................
..#........................#........#.............................................................................................
.............#........#......................#.......................................#...#....#............................#......
............#.......#.............#...#...........#.#.................................#.................##........................
.....................................................................................#.........#.......................#.....#....
.................#................................#..#....#......#..........................................#.....#...............
..#...............................#........................................................#.......#...#............#.............
#.........#..#..#.....................................#....................................#................#.................#...
.......................#..#...#............................................................................#..#.....#.......##....
..........#............#.......................#.........................^......#.#...................................#...........
...##........................##..........#..........#.................#...............#...........#.....##........................
..............#............#..........................#....#...........#......#......#......................#.....................
......#.....#....................................................#............#.#......................................#..........
......#.............................#......................#...............................#........#.............................
.#..........#.............................................#.............................................#.........................
...............................##.................................................................................................
.....................#................................................#...............#............#...............#.....#.....#..
..................#...........................................................................#...........#...................#...
##..............................#.#...#.#...............................#...........#.#...................#.......................
......................#.........#...................................#.................#...........................................
.......................#............................................................................#.........##...........#......
.....................................................................#.......#.....#.......................................#......
#....................................#........#.#.................................................................##.....#........
..............#..............................#......#..........###.........#................#..............#..................#...
..................................#..............................#.................##.............................#...#...........
...............#......#........................#....#........#............#.........#..........#..............#......#............
...#...........#........#...#...........................#.............#.....................................#.....................
..............###............#...........................................................#.............#.........##..#............
............#................................................#.......#..................#....#....................................
.........#.#...#.....#...........................#.....##......#.....................#..#........#......#.........................
.#....#.......#.......................#.................................................................#.......#..............#..
...................#................................................................#.............#...#...........#.........#.....
.......#.#.......#.......................#....#.....#...............#...#.....#...........................................#.......
.......#........#...........................................#...........#.........................#....#.......#..................
..................................#.#.................#.#............................................#...........#....#...........
....##......#.............#...................................#...#.........#...........................#......#..................
.........................#......#....#...#........................................................................................
...#...............#............................................................................#...........................#.....
....##.#..............................................#............#................#................................#......#.....
.........#...........................................#...#.......##..............................#................................
..................#..........#......#.......#......................................#........#...............#......#...........##.
........................................#......#..........#......................................................................#
..............#...#...................#.....#..........................................................#..............#......#....
...#........#........................#...#................#........#.............................##..........................#....
............#............................#.#........................#.................................#..........................#

15
day6/input.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_day6_input_txt_start __asm("_binary_day6_input_txt_start");
extern uint32_t _binary_day6_input_txt_end __asm("_binary_day6_input_txt_end");
extern uint32_t _binary_day6_input_txt_size __asm("_binary_day6_input_txt_size");
#ifdef __cplusplus
}
#endif

127
day6/pos.py Normal file
View File

@ -0,0 +1,127 @@
s = """
....#.....
....+---+#
....|...|.
..#.|...|.
....|..#|.
....|...|.
.#.O^---+.
........#.
#.........
......#...
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
......O.#.
#.........
......#...
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
.+----+O#.
#+----+...
......#...
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
..|...|.#.
#O+---+...
......#...
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
....|.|.#.
#..O+-+...
......#...
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
.+----++#.
#+----++..
......#O..
"""
ss = [i.strip() for i in s.strip().split('\n\n')]
ll = set([
(4, 3),
(7, 1),
(3, 6),
(6, 7),
(3, 8),
(1, 8),
(7, 7),
(7, 9),
])
def find_o(m):
x = 0
y = 0
ix = 0
while ix < len(m):
if m[ix] == 'O':
return (x, y)
if m[ix] == '\n':
y += 1
x = 0
else:
x += 1
ix += 1
oo = set([find_o(m) for m in ss])
print(oo)
print(ll - oo)
0123456789
0 ....#.....
1 ....|--O.#
2 ....|.|...
3 ..#.|.|...
4 ....|.|#..
5 ....|.|...
6 .#--------
7 .|....|.#.
8 #-----|...
9 ......#...
0123456789
0 ....#.....
1 .........#
2 ..........
3 ..#.......
4 .......#..
5 ..........
6 .#........
7 ........#.
8 #.........
9 ......#...

223
day6/render.cpp Normal file
View File

@ -0,0 +1,223 @@
#include "holly/isp_tsp.hpp"
#include "holly/ta_parameter.hpp"
#include "holly/ta_global_parameter.hpp"
#include "holly/ta_vertex_parameter.hpp"
#include "sh7091/store_queue.hpp"
#include "sh7091/serial.hpp"
#include "math/vec2.hpp"
#include "font/font.h"
#include "maple/maple_bus_ft0.hpp"
#include "solution.h"
extern void glyph_start(const uint32_t texture_width, uint32_t texture_height);
extern int32_t transform_char(const uint32_t texture_width,
const uint32_t texture_height,
const uint32_t first_char_code,
const glyph * glyphs,
const char c,
int32_t horizontal_advance,
int32_t vertical_advance);
using vec2 = vec<2, float>;
constexpr vec2 arrow_vertices[] = {
{-0.1, -0.15},
{ 0.0, -0.45},
{ 0.1, -0.15},
};
constexpr vec2 square_vertices[] = {
{0.475, 0.525},
{0.525, 0.525},
{0.475, 0.475},
{0.525, 0.475},
};
vec2 rotate(vec2 v, int n) {
switch (n) {
case 0: return {v.x, v.y};
case 1: return {-v.y, v.x};
case 2: return {-v.x, -v.y};
case 3: return {v.y, -v.x};
}
__builtin_unreachable();
}
void polygon_start()
{
const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume
| para_control::list_type::translucent
| obj_control::col_type::packed_color;
const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::always
| isp_tsp_instruction_word::culling_mode::no_culling;
const uint32_t tsp_instruction_word = tsp_instruction_word::src_alpha_instr::one
| tsp_instruction_word::dst_alpha_instr::zero
| tsp_instruction_word::fog_control::no_fog;
*reinterpret_cast<ta_global_parameter::polygon_type_0 *>(store_queue) =
ta_global_parameter::polygon_type_0(parameter_control_word,
isp_tsp_instruction_word,
tsp_instruction_word,
0, // texture_control_word
0, // data_size_for_sort_dma
0 // next_address_for_sort_dma
);
sq_transfer_32byte(ta_fifo_polygon_converter);
}
constexpr int scale = 45;
void arrow(int cx, int cy, int rotation, uint32_t base_color)
{
float z = 1.0 / 10.0;
for (int i = 0; i < 3; i++) {
bool end_of_strip = i == 2;
vec2 v = arrow_vertices[i];
v = rotate(v, rotation);
v.x *= scale;
v.y *= scale;
v.x += scale / 2.0 + (640 - (scale * 10)) / 2 + cx * scale;
v.y += scale / 2.0 + (480 - (scale * 10)) / 2 + cy * scale;
*reinterpret_cast<ta_vertex_parameter::polygon_type_0 *>(store_queue) =
ta_vertex_parameter::polygon_type_0(polygon_vertex_parameter_control_word(end_of_strip),
v.x, v.y, z,
base_color);
sq_transfer_32byte(ta_fifo_polygon_converter);
}
}
void border(int cx, int cy, int rotation)
{
uint32_t base_color = 0xff'ff00ff;
float z = 1.0 / 10.0;
for (int i = 0; i < 4; i++) {
bool end_of_strip = i == 3;
vec2 v = square_vertices[i];
v = rotate(v, rotation);
v.x *= scale;
v.y *= scale;
v.x += scale / 2.0 + (640 - (scale * 10)) / 2 + cx * scale;
v.y += scale / 2.0 + (480 - (scale * 10)) / 2 + cy * scale;
*reinterpret_cast<ta_vertex_parameter::polygon_type_0 *>(store_queue) =
ta_vertex_parameter::polygon_type_0(polygon_vertex_parameter_control_word(end_of_strip),
v.x, v.y, z,
base_color);
sq_transfer_32byte(ta_fifo_polygon_converter);
}
}
void glyph(const struct font * font,
const struct glyph * glyphs,
int cx, int cy,
char c)
{
int32_t horizontal_advance = font->face_metrics.max_advance * 2 / 5; // 26.6 fixed point
int32_t vertical_advance = font->face_metrics.height * 5 / 4; // 26.6 fixed point
int x = (640 - (scale * 10)) / 2 + cx * scale;
int y = (480 - (scale * 10)) / 2 + cy * scale;
horizontal_advance += (x << 6);
vertical_advance += (y << 6);
transform_char(font->texture_width,
font->texture_height,
font->first_char_code,
glyphs,
c,
horizontal_advance,
vertical_advance);
}
extern "C" struct solution_state day6_state;
void a_press()
{
serial::string("press\n");
serial::integer<uint8_t>(day6_state.part);
day6_state.step = true;
}
extern "C"
void day6_render(const struct font * font,
const struct glyph * glyphs,
ft0::data_transfer::data_format * maple_ft0_data)
{
return;
static bool last_a = true;
if (maple_ft0_data[0].digital_button != 0) {
bool a = ft0::data_transfer::digital_button::a(maple_ft0_data[0].digital_button) == 0;
if (a && !last_a) {
a_press();
}
last_a = a;
}
polygon_start();
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
border(x, y, 0);
border(x, y, 2);
}
}
border(9, 0, 3);
border(0, 9, 1);
uint32_t base_color[2] = {
0xff'00ffff,
0xff'ff0000,
};
for (int y = 0; y < day6_state.height; y++) {
for (int x = 0; x < day6_state.width; x++) {
int ix = y * day6_state.width + x;
unsigned char c = day6_state.visited[ix];
for (int i = 0; i < 4; i++) {
if (c & (1 << i)) {
arrow(x,
y,
i,
0xff'005555);
}
}
}
}
arrow(day6_state.guard[0]->position.x,
day6_state.guard[0]->position.y,
day6_state.guard[0]->facing,
base_color[0]);
// render text
glyph_start(font->texture_width, font->texture_height);
glyph(font, glyphs, 11, 0, '0' + day6_state.part);
for (int y = 0; y < day6_state.height; y++) {
for (int x = 0; x < day6_state.width; x++) {
int ix = y * day6_state.stride + x;
char c = day6_state.input[ix];
if (c == '#')
glyph(font, glyphs, x, y, '#');
}
}
}

10
day6/sample1.txt Normal file
View File

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

15
day6/sample1.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_day6_sample1_txt_start __asm("_binary_day6_sample1_txt_start");
extern uint32_t _binary_day6_sample1_txt_end __asm("_binary_day6_sample1_txt_end");
extern uint32_t _binary_day6_sample1_txt_size __asm("_binary_day6_sample1_txt_size");
#ifdef __cplusplus
}
#endif

258
day6/solution.c Normal file
View File

@ -0,0 +1,258 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "printf.h"
#include "parse.h"
#include "solution.h"
static struct position find_guard(const char * input, int length,
int stride)
{
int x = 0;
int y = 0;
for (int i = 0; i < length; i++) {
if (input[i] == '^') {
break;
}
x += 1;
if (x >= stride) {
x = 0;
y += 1;
}
}
return (struct position){x, y};
}
static bool position_inside_map(int width, int height,
int x, int y)
{
return
x >= 0 &&
y >= 0 &&
x < width &&
y < height ;
}
static bool position_contains_obstacle(const char * input,
int stride,
int width, int height,
int x, int y)
{
if (!position_inside_map(width, height, x, y))
return false;
char c = input[y * stride + x];
return c == '#';
}
static enum facing turn(enum facing facing)
{
switch (facing) {
case UP: return RIGHT;
case RIGHT: return DOWN;
case DOWN: return LEFT;
case LEFT: return UP;
}
__builtin_unreachable();
}
static struct position move(enum facing facing, int x, int y)
{
switch (facing) {
case UP: return (struct position){x , y - 1};
case DOWN: return (struct position){x , y + 1};
case LEFT: return (struct position){x - 1, y };
case RIGHT: return (struct position){x + 1, y };
}
__builtin_unreachable();
}
static void guard_move(struct guard * guard, bool collision)
{
if (collision) {
guard->facing = turn(guard->facing);
} else {
guard->position = move(guard->facing, guard->position.x, guard->position.y);
}
}
static bool guard_collision(const char * input,
int stride,
int width, int height,
const struct guard * guard)
{
struct position position = move(guard->facing, guard->position.x, guard->position.y);
return position_contains_obstacle(input, stride, width, height, position.x, position.y);
}
static bool guard_collision_obstacle(const char * input,
int stride,
int width, int height,
const struct guard * guard,
struct position obstacle)
{
struct position position = move(guard->facing, guard->position.x, guard->position.y);
return (position.x == obstacle.x && position.y == obstacle.y)
|| position_contains_obstacle(input, stride, width, height, position.x, position.y);
}
static int sum(unsigned char * l, int length)
{
int acc = 0;
for (int i = 0; i < length; i++) {
acc += (int)(l[i] > 0);
}
return acc;
}
struct solution_state day6_state = {0};
void state_init(int part,
const char * input,
int stride,
int width,
int height,
unsigned char * visited,
struct guard * guard)
{
day6_state.part = part;
day6_state.input = input;
day6_state.stride = stride;
day6_state.width = width;
day6_state.height = height;
day6_state.visited = visited;
day6_state.guard[0] = guard;
}
int64_t day6_part1(const char * input, int length)
{
int stride = parse_stride(input, length);
int height = parse_height(input, length);
int width = stride - 1;
unsigned char visited[height * width];
for (int i = 0; i < width * height; i++) visited[i] = 0;
struct guard guard;
guard.facing = UP;
guard.position = find_guard(input, length, stride);
state_init(1, input, stride, width, height, visited, &guard);
while (true) {
/*
if (!day6_state.step)
continue;
day6_state.step = false;
*/
bool guard_inside_map = position_inside_map(width, height,
guard.position.x, guard.position.y);
if (!guard_inside_map)
break;
visited[guard.position.y * width + guard.position.x] |= (1 << (int)guard.facing);
bool collision = guard_collision(input,
stride,
width, height,
&guard);
guard_move(&guard, collision);
}
return sum(visited, height * width);
}
static bool speculative_obstacle_causes_loop(const char * input,
int stride,
int width, int height,
unsigned char * visited,
struct position origin,
struct position obstacle)
{
// reset visited
for (int i = 0; i < width * height; i++) visited[i] = 0;
struct guard guard;
guard.facing = UP;
guard.position = origin;
while (true) {
bool guard_inside_map = position_inside_map(width, height,
guard.position.x, guard.position.y);
if (!guard_inside_map)
break;
// is this a loop?
if (visited[guard.position.y * width + guard.position.x] & (1 << (int)guard.facing))
return true;
visited[guard.position.y * width + guard.position.x] |= (1 << (int)guard.facing);
bool collision = guard_collision_obstacle(input,
stride,
width, height,
&guard,
obstacle);
guard_move(&guard, collision);
}
return false;
}
int64_t day6_part2(const char * input, int length)
{
int stride = parse_stride(input, length);
int height = parse_height(input, length);
int width = stride - 1;
unsigned char visited[height * width];
for (int i = 0; i < width * height; i++) visited[i] = 0;
struct position origin = find_guard(input, length, stride);
struct guard guard;
guard.facing = UP;
guard.position = origin;
state_init(2, input, stride, width, height, visited, &guard);
unsigned char obstacles[height * width];
for (int i = 0; i < width * height; i++) obstacles[i] = 0;
unsigned char loops[height * width];
for (int i = 0; i < width * height; i++) loops[i] = 0;
while (true) {
bool guard_inside_map = position_inside_map(width, height,
guard.position.x, guard.position.y);
if (!guard_inside_map)
break;
bool collision = guard_collision(input,
stride,
width, height,
&guard);
if (!collision) {
struct position obstacle = move(guard.facing, guard.position.x, guard.position.y);
if (obstacles[obstacle.y * width + obstacle.x] == 0) {
obstacles[obstacle.y * width + obstacle.x] = 1;
bool loop = speculative_obstacle_causes_loop(input,
stride,
width, height,
visited,
origin,
obstacle);
loops[obstacle.y * width + obstacle.x] |= loop;
}
}
guard_move(&guard, collision);
}
return sum(loops, height * width);
}

39
day6/solution.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct position {
int x;
int y;
};
enum facing {
UP,
RIGHT,
DOWN,
LEFT,
};
struct guard {
struct position position;
enum facing facing;
};
struct solution_state {
bool step;
int part;
const char * input;
int stride;
int width;
int height;
struct guard * guard[2];
unsigned char * visited;
};
#ifdef __cplusplus
}
#endif

850
day7/input.txt Normal file
View File

@ -0,0 +1,850 @@
12615: 804 5 2 1 5 7 7 7 1 3 3 5
33925: 7 7 4 3 2 1 7 36 4 8 881 3
1046107888: 263 576 34 8 863
700274: 700 15 3 33 88
16420652: 202 241 8 74 3 5 35
660814849: 569 509 613 847 1
29746: 33 498 56 9 1
763511: 7 442 17 2 11
19200948413: 4 2 6 9 9 3 33 1 896 8 2 5
1833667: 240 764 4 2 8
19457968: 1 741 691 38 6 781
1136: 7 1 3 31 5
1874160190: 8 8 22 2 8 4 64 5 5 5 133
13721299: 48 44 354 386 8
1899744: 85 458 648 8 6
554415: 46 9 435 6 5
91193289: 1 4 5 7 301 91 81 6 96 8
36776916040: 9 4 42 4 75 3 5 561 4 43
128828: 61 9 456 787 5 69 8
5273196: 633 9 9 81 97
10935737837: 6 5 7 612 5 3 73 1 78 36
902461: 45 122 1 4 5
1327804638093: 2 3 8 597 7 8 8 883 463
102018: 277 91 94 8 550 20
8951006747: 843 6 880 4 421 599 2
407827: 814 1 66 1 5
3181026: 1 9 704 6 251 50 84 89
2254430: 7 1 45 8 7 302
614187: 4 644 306 19 6 5 57 7 3
4820287: 11 7 43 93 692 4 3 19 6
2695156093882: 23 7 558 535 8 6 6 3 84
61814410: 35 44 1 2 8 7 1 1 6 2 9 3
14769966: 2 7 2 5 5 8 7 5 1 995 1 4
87113: 3 829 35 5 64
209723664824: 2 9 5 4 556 2 23 1 3 826
6522: 28 86 54 32 334
7552020582: 63 81 3 609 301 2 81
4507014: 3 3 3 1 9 77 7 9 6 5 71 3
12923202: 1 4 90 8 5 2 8 3 3 6 572 6
12392: 4 95 898 9 91 799
294789341: 25 7 35 94 4 7 6 3 7
72913: 34 17 22 882 6 49
69385571: 85 660 6 6 81
4575: 96 53 7 3 9 74
704039583: 80 9 2 40 9 87
8481251708752: 268 5 3 56 2 6 4 47 75 1
595700: 12 6 443 635 518
4381885420: 1 7 1 8 3 5 369 2 5 408 9
18639667: 6 4 9 981 667
121169760: 46 3 279 2 9 81 2 4 3 82
45035: 176 248 482 900 5
923326: 5 917 34 9 234 746
53784612: 2 6 6 1 4 23 2 7 41 4 609
524271640: 43 15 927 71 637
30631969: 895 33 24 55 6 38 9
583593561: 83 9 14 5 93 58 96 4 6
204744990: 846 6 5 1 8 1 8 7 16 3 9 3
12817652: 2 64 1 67 10 942
142228: 3 3 91 5 7 4 215 1 7 2 74
2952720220097: 9 1 3 4 45 60 5 501 7 8 4
1281605970: 801 200 7 46 8 2
205858: 76 4 34 45 5 9 4 2 5 581
26828150: 11 34 3 3 706 93 56 1 1
819158: 9 40 46 1 8 3 712 158
19395810: 8 1 142 88 8 7 81 2 3 6
220127627: 90 6 2 2 4 7 1 6 286 6
58968: 464 2 2 9 14
6881901879985: 996 638 57 2 1 19 87
22484140: 70 753 9 435 423
3634605: 3 484 60 7 17 6 9 4 1 7 4
1585232880: 2 6 9 7 7 4 851 1 6 11 8 7
20496290: 658 3 76 408 2
73616: 3 8 37 39 92 2 8
800: 709 3 47 41 1
1340484319: 1 556 3 5 71 8 3 1 2 4 7 8
2198657172: 5 196 17 6 72 45 56 4 3
254513: 318 1 1 3 8 9
37479836067: 945 16 39 78 5 60 64
24112988: 61 19 376 63 3
1492811: 75 9 70 34 18
405446931: 5 9 2 2 901 924 7 5 7 4 9
1619555207: 75 5 4 6 4 45 7 7 7 7 205
22187314586045: 9 8 9 5 71 85 8 8 6 51 1 4
1395487: 857 4 4 95 1 495 1 4
626025: 59 9 1 78 849 398 4 5
414830: 93 4 404 69 4 3
382136: 2 6 62 9 2 2 5 533 3 7 8
147217631372: 1 99 3 6 7 6 9 45 1 716 7
303509: 105 798 3 7 2 1 7 8 45
772677938: 760 6 6 677 935
549726257651: 3 9 9 8 3 9 2 3 3 3 14 725
1252317150: 2 19 535 58 1 4 40 225
616863634: 84 90 139 35 587 9
17404425260: 870 2 1 21 2 5 248 9 1
5717542240278: 9 6 5 159 96 987 9 102
31826589849: 3 1 826 5 851 9 466 1
1576284: 5 7 4 1 4 1 1 5 320 5 3 6
6077335228: 59 79 98 334 5 7 30
550434: 38 6 4 9 23 114 280 3
10227883: 5 9 7 3 721 6 1 3 5 6 5 9
3314108926: 87 9 851 2 3 52 338 3
25523070: 5 51 22 176 892
25781933829: 257 818 942 389 7 26
5517196720: 4 70 71 473 3 2 35
39957: 98 30 965 2 93 815
489483804: 593 4 491 168 10
333230997: 1 32 7 410 2 144 308
29526863042: 5 724 478 237 72
2316083879755: 687 265 337 82 9 7 5 6
2867208: 71 75 308 509 8 6
1593944414484: 253 63 444 144 87
2516562: 8 1 6 7 41 2 429 7 34 7 7
39215953: 4 976 39 89 4
408331: 2 314 176 985 84
68483: 2 9 96 414 9 2 13 7 9
76532283502: 7 64 48 1 837 4 83 50 4
367445644: 32 5 7 5 71 6 2 8 7 8 8 81
970551: 506 95 418 4 1 9 5 746
1931531: 3 9 7 9 12 9 631 480 10
6920242: 1 4 65 20 239
4262: 4 79 46 73 17 354
5307445: 5 30 7 441 1
126430: 8 59 59 42 7
246780: 264 44 114 7 1 7 7 898
19980364464: 9 387 705 7 1 16 9 71
221570: 4 2 314 331 70
5996087: 6 8 62 46 41
1996930097: 6 20 19 385 1 8 523 4 2
170504569: 276 8 1 31 43 2 5 6 3
116291719335: 6 9 782 505 5 3 442 33
932253926: 971 320 3 93 926
118: 76 8 31
860850091000: 48 427 4 2 1 5 369 84 4
131: 6 2 56 2 61 4
15234086180: 84 8 8 456 3 8 61 7 4 2 9
86790443: 919 20 787 6 83
923888: 1 635 939 7 292 2 1
1130341: 292 79 7 1 7
205860750: 99 501 4 1 47 7 5 3 7 83
1077: 82 898 97
704222: 703 858 366
1674169: 50 95 8 49 44 6 7
1718200487: 2 7 917 5 331 364
4630570616793: 3 2 9 4 798 471 7 8 792
514295124840: 1 929 77 76 8 101 840
65231559928: 407 4 2 972 4 9 4 1 16 9
17814590: 5 328 62 451 85 6
15130530: 5 29 741 4 3 2 488 87 2
1026969: 142 1 7 877 969
40230464: 18 27 894 463 1
3443736: 321 9 149 6 8
402: 49 5 8 143 5
452065: 1 797 311 1 408
8069098637: 806 89 1 97 9 7 66 1 8
16129969360: 700 887 416 23 35 7
1002697947384: 4 6 4 9 9 96 59 388 3 2 7
1379379617088: 5 507 9 559 3 85 7 63 1
2346403: 4 7 5 74 2 4 3 7 6 6 8 20
7509412: 4 27 7 5 8 68 6 6 510 7 4
29093479449: 1 16 77 229 40 4 6 68
259998: 4 71 1 19 6 6 8 6 5 691
2436234977277: 68 64 260 566 4 951
189021992340: 193 67 976 6 66 3 40
1492448: 24 8 7 41 6
61581011: 9 9 9 54 5 51 9 103 87 7
16976: 1 55 5 9 3
799980: 744 4 1 51 977
386155: 58 76 810 74 23
102708121: 4 5 6 26 8 195 7 9 6 2 6 1
9053097: 2 80 7 47 8 90 4 3 6 519
2844455328: 37 6 945 7 533 1
326750483: 7 18 7 169 3 4 1 548 4 3
99445021898: 9 3 41 8 2 5 1 9 2 907 25
83874: 238 42 296 6 988
171779: 4 412 65 4 78 4
5150458425: 1 4 9 39 8 39 66 83 842
91887843756: 980 137 5 625 3 4
144563844: 727 1 4 788 8 3 3 7 12
6369361600: 65 14 306 3 8 320 823
372861: 981 2 38 7
20610: 205 99 9 1 4
101170: 5 4 6 5 775 3 64 344 1 8
3256828274195: 650 5 7 79 56 54 839 5
23531: 80 224 6 702 85 3 9 2 5
862035: 75 778 85 818 5
337124: 4 1 9 2 2 5 2 3 3 1 6 725
21207288: 154 7 9 5 3 370 7 8 46
118311418: 98 34 89 83 1 4 21
19027710: 2 466 88 58 4
50793: 33 9 5 6 1 6 17
1788068073: 388 9 3 4 8 6 4 7 7 7 9 4
7274340: 993 70 5 76 9
3598942581: 24 250 291 51 5 74
14696: 7 1 8 82 94
2960064560: 1 1 83 6 90 1 6 6 6 8 490
31493133: 157 4 1 6 5 4 3 5 8 1 9 2
48391: 3 6 94 432 7
1508923: 1 831 676 92 3
264640: 817 10 8 8 5
31331: 5 7 57 22 2 26 470 739
52034312079: 577 5 79 580 9 207 1 9
4085878333: 8 910 61 61 92
208153292: 3 1 239 4 72 7 9 3 7 6 4 8
54112: 483 1 7 1 16
3254589: 945 574 6 5 4
1833763942: 7 63 180 42 463 61 4 5
48168908: 7 2 1 75 7 375 2 7 148
782858072: 5 245 13 639 2
753: 2 5 20 2 14 243 96
2422200: 519 2 690 100 2
26470844: 348 3 76 4 2
107164: 1 153 93 86 8
55198753: 1 2 11 3 14 3 9 7 9 2 56
46459108: 5 31 479 9 931 7 111
514568039289: 52 37 3 982 390 2 92
100240: 6 13 547 501 5
2922092248: 2 9 2 52 94 769 1 5 9 10
894623027: 6 6 3 77 9 30 6 3 5 35 24
1917027689: 83 22 5 824 163 722
20040668: 194 4 68 9 258
284: 1 56 3 225
4169656344672: 909 5 57 502 4 91 438
16386749280: 8 784 2 145 506 141
352590058: 75 322 2 73 45 4 6
622: 98 1 433 10 40 41
2681459: 65 4 41 2 5 37
98997: 2 65 835 24 94
57160224: 5 720 5 17 2 8 8 8 396 8
56454895448: 740 762 668 954 49
86649004: 892 70 7 608 9 530
625876561: 5 744 8 5 261 765 1 5 3
210366055: 6 4 5 7 756 9 7 27 115 5
16118332454: 3 537 8 332 45 1
355104016943: 930 34 39 8 545 278 7
207781038: 6 14 2 83 81 153
5792: 49 1 396 504 6 7 85
5926579: 2 6 3 85 92 8 6 4 8 45 4
284043: 9 690 95 7 45
88053: 170 33 54 8 357
15155250292: 19 222 374 5 330 292
733049: 7 1 7 53 3 1 6 7 246 80 9
71237: 1 7 11 8 797 9
1830332579: 7 366 537 2 6 49 2 3
7159: 70 77 80 1
2376095553: 10 264 9 95 5 36 2 9 6
1835802115814: 9 1 73 8 4 9 571 35 814
2090: 9 7 920 574 533
35511093854: 9 65 6 8 4 7 4 563 3 8 1 6
156103282940: 9 90 730 38 8 3 54 10 5
212632938: 187 130 377 21 54
29870448143: 768 791 400 17 479 1
147832353: 1 41 27 35 309 323 53
2021423350: 1 1 5 4 214 2 3 341 2 4 1
460956608: 3 1 609 56 6 10
1638166: 8 1 216 3 2 87 9 8 7 74 5
6633: 29 691 13 4 9
20155519: 83 1 94 8 258 43
16983: 1 4 6 4 27 3 136 255
3518081: 1 4 692 1 8 63 8 6 6 4 14
48682976952: 2 7 7 2 3 42 6 5 88 91 83
301108105: 170 8 8 6 732 22
1047200: 54 1 34 20 28
26847028: 4 4 70 7 6 2 4 9 1 4 4 132
296820495: 2 90 29 702 8 9 6 8 1 8 9
37792579871: 7 558 515 972 5 6 8
5669: 62 25 6 9 973
723: 35 2 5 3 3 2
10140: 575 8 4 2 932
462704969: 214 2 4 6 6 9 35 8 523
2920: 8 3 4 1 20
1348498: 17 2 392 7 2 4
4874650231: 879 22 36 549 9 1 28
2105066: 7 48 93 1 25 6 4
115907: 7 7 336 7 7 2 546 91 2 8
16120: 87 2 766 17 44 1 96
463130000: 37 5 7 7 45 8 8 5 5 5 5 8
1095006743000: 15 6 9 809 8 763 177
303005195: 2 4 63 986 8 14 66 6
14968802772: 7 5 3 4 308 5 3 4 3 9 6 9
103385920: 1 116 5 2 7 6 92 1
3398720: 70 23 2 688 52
14747: 8 4 63 46
52529797495: 9 6 606 4 935 8 8 1 2 3 9
40788: 60 112 12 66 6
8842: 2 4 1 9 5 54 62 277 257
9400381: 2 35 17 6 98 2 379
185753015: 6 3 8 6 425 66 8 36 3 34
40155140909: 9 53 74 1 9 5 8 6 8 98 11
29883814563: 62 3 34 54 7 3 814 561
36538630: 943 8 8 4 48 3 5 1 1 4 7 9
913186: 909 4 1 18 3
101093763: 8 3 568 873 1 82 2 1
44928082617: 1 56 7 77 8 7 3 2 3 5 4 27
2866145985: 573 2 39 6 25 20 96 2 2
207199: 3 50 592
1940208: 86 3 94 6 8
20161960: 42 658 5 9 40 35 764
7055560512: 9 90 98 9 5 2 3 6 9 1 862
5730: 1 938 2 6 90
1655835: 28 5 963 1 8 5 3 6 346 3
61: 7 5 39 10
8624: 2 13 3 319 9
14912: 9 69 8 3 8
1906734465: 6 334 666 301 1
26919: 5 263 1 1 9
12351333: 511 59 233 93 3
370053: 73 6 40 58 9 7 3 640 86
6262819971675: 61 5 8 4 8 562 8 457 2 8
937: 4 40 778
38056042: 4 2 2 9 1 3 9 732 5 58 9
3159994: 4 1 62 95 78 9 9 4
100831554: 6 17 4 966 3 7 5 3 2 28 1
2499841609: 279 128 70 871 738
1708000: 20 2 45 2 7 995 5
57451: 25 27 23 11
759531135984: 21 7 271 7 311 3 5 981
54868732: 35 15 3 8 3 4 6 6 3 725 7
1807682561: 443 88 67 5 67 68 2 4
86820: 7 69 1 1 8 1 91 19 4 699
643731360482: 96 87 95 293 3 923
11610: 61 4 915 5 15
14699068836: 1 16 7 1 776 936 1 1 82
485652428459: 4 8 1 4 6 5 2 428 1 45 2 7
329778361: 471 7 783 62
3778353594: 68 7 56 707 5 94
1724810170: 35 712 5 692 9 8 1
19430525: 969 1 2 48 5 23
81808470513: 82 6 348 187 99
4697640107: 7 897 9 7 460 8 7 849
43814941850: 2 7 48 3 338 1 848
374336: 37 4 249 9 81
3296279207282: 5 5 824 697 7 3 18 21 4
40335487: 67 2 5 60 76 83 62 2
14167602: 4 788 9 83 54
4830: 3 7 7 4 2 3 7
511441831: 4 3 744 9 955 1 560 9 7
758358826: 1 7 463 962 4 8 9 8 26 1
32787846640229: 7 5 836 944 4 4 5 2 916
137966524: 9 5 67 9 9 67 5 8 6 94 7 9
64761: 57 592 5 701 9
9279331: 86 203 97 9 32
11837726: 62 303 3 5 7 7 29 3 6 6 2
101355067: 1 6 7 5 12 9 940 2 3 1 6 8
5192667: 402 411 6 314 666
4727525083: 14 5 3 2 9 9 7 3 6 105 5 8
2332455: 4 20 41 29 5 51 4
61721503606: 8 8 5 4 2 95 5 89 360 5 1
11181297204: 1 7 80 2 97 475 6 3 5 7 9
442396981: 45 807 519 208 982
43896: 2 379 363 59
7181588325: 5 2 18 15 8 8 1 5 8 9 7 1
2129084: 7 7 1 47 62 4 4 9 12 9
1016: 277 3 672 1 64
2635441777609: 416 4 731 8 791 2 6 10
6016677: 4 5 31 2 538 619 9 2 3 7
4283029: 4 2 8 20 1 2 88 647 96
39861504: 3 3 4 4 73 6 948
3169130: 7 149 49 2 6 62
3367293818524: 115 8 6 1 1 5 3 8 61 52 4
38130: 33 4 37 55 972
48389990: 48 388 1 662 328
779654659: 77 9 5 1 5 9 3 5 731 2 3 9
11913060875: 325 866 30 608 75
1061004: 37 924 16 227 68
389270: 80 694 90 7
1157: 1 6 9 4 9 5 804 6 2
5516918: 854 646 3 4 44
2015: 62 8 8 325 5
146: 1 25 80 36 5
2586431375: 8 910 1 5 7 2 5 4 9 7 4 5
554: 45 6 3 1 4
901722160: 8 3 65 892 9 232 6 9 8 8
1508259: 128 60 33 8 8 97 6
1860109094171: 644 476 370 6 23 164
7606509: 10 159 45 61 89 9
195906: 30 9 170 5 52 3
382857: 73 3 558 14 5
24471778: 9 3 5 320 4 7 59 4 7 7 7 8
125: 19 54 37 12 3
80376: 5 574 28 8 8
75172044: 27 15 8 75 706 5 6 334
1810587: 4 8 1 2 4 862 4 5 32 3 7 2
33096068: 8 985 3 14 69
461181: 38 8 1 183
8235895: 7 3 74 9 3 7 903 4 8 2 9 9
319422733: 360 9 59 5 12 5 3 7 25 6
11735009: 39 1 66 8 8 3 6 9 6 5 7 7
250447: 832 88 3 580 4
116751: 8 5 2 24 187 3 54 1 6
1605684: 7 527 47 8 48 701 3 6
65584601: 43 88 4 6 5 93 2 151 4
72951226: 527 24 61 10 2 7 5 82 8
47920948206: 532 454 978 1 1 1 9
333509945118: 6 1 1 7 2 72 990 8 5 3 3 2
29133: 2 97 6 25 30
73818409561: 1 5 455 4 27 40 9 5 5 1 8
688251860: 93 72 807 91 294
20176506: 875 21 183 126 6
878: 145 38 543 79 73
1537363: 1 1 470 67 288 77
844227: 7 7 603 1 7 10
2186499: 5 25 33 92 21 6 1 4 75
282773964526: 6 300 6 85 57 51 66 8
9737: 9 1 1 62 9
1814424: 202 9 4 3 82 9 603
618982: 6 612 97 1 3
144229: 1 9 3 12 231
280844455: 3 973 39 7 4 451 1
93053313: 758 17 54 799 197 9
16848: 4 8 2 4 3 39
9831864: 5 454 9 8 2 4 38 60 63 8
9216: 8 20 7 32 30
83915: 63 24 625 39 572
1324812384828: 91 886 178 9 4 9 81 6 9
9807944: 6 41 9 38 17
1011990: 5 95 97 22 89
3854: 61 3 9 9 47
56253507981121: 97 997 841 711 960
424316: 64 3 79 441 74
117272977: 6 67 6 475 2 71 878 44
2256: 92 9 628 264 435
11981841: 443 73 27 112 9
1787: 63 5 3 51 7
3659864172: 7 398 54 3 2 8 94 8 3 5 2
248085895127: 65 7 584 45 68 59 8
16660853402: 326 68 3 400 51
41592384: 57 761 6 2 9 8
9459986: 13 3 4 93 6 97 2 271 2 2
13616829: 9 54 20 5 2 12 7 7 6 1 2
166598199: 6 5 8 38 7 4 30 899 5 8 1
377586995: 751 4 5 869 2 2 72
320173: 62 4 70 48 8 3
963099536: 188 908 93 81 9 534
331336: 143 683 401 5 99 6
50535518: 9 6 6 5 1 1 4 61 26 7 4 87
477: 14 8 7 4 1
11968171: 9 85 9 316 2 7 4 104 15
495519: 791 419 339 81 76 4
12480: 2 929 485 213 562 2 4
1307445699: 1 8 4 1 4 28 476 9 8 3 3 9
1435: 7 32 2 7 6 40
25913272: 43 2 7 1 67 22 59 31 4
7266511816: 3 269 925 5 97 9 466
59303392302: 5 74 6 427 5 6 88 3 5 6 5
11455248: 3 98 9 42 1 285 2 7 662
2404154: 9 3 371 3 24
190728476: 2 8 3 2 596 39 9 447 6 8
40749652398: 9 3 3 497 5 6 5 5 2 392 5
2283838454: 1 4 3 7 5 9 4 1 7 8 9 828
9239008: 769 28 2 161 3 6 4 8 6 4
2848880061: 9 5 42 3 5 2 8 476 204 5
615780855301: 4 4 1 8 23 404 9 1 2 5 5 6
6044: 9 5 8 4 1 28 54 7 6 1 563
49295691: 161 62 1 373 59 9
116571994: 8 7 9 7 200 58 7 354 77
88343824059: 724 5 41 7 8 9 2 7 744 6
29725: 2 13 3 66 2 4
7000627200: 5 54 206 8 50 24 4 383
19577668368: 73 352 7 894 2 852
22491: 84 654 2 116 3
398373: 219 4 9 5 33 6
509953: 53 89 75 235 3
27740: 1 320 5 94 76 8 736 5
850240482: 8 4 9 70 3 5 2 530 1 4 8 5
208: 6 7 7 43 74
99824431656: 450 8 711 8 78 5 45 1 9
693095506: 90 77 9 550 6
4414: 8 9 5 6 461 8
760653718: 84 4 3 8 4 3 9 2 50 464 1
16871764: 5 450 142 2 1 36 5 7 3 5
34513: 6 99 13 7 48
807678432: 39 992 374 54
116870416: 93 23 870 356 61
65653: 698 1 433 58 55
2176: 9 25 64 1
6520574: 3 3 6 13 2 389 8 945 4
8062111: 83 5 758 762 54 5 9 1
4690: 479 4 8 822 1 7
600: 50 3 4
30237: 914 89 36 4 28 994 39
60639: 670 13 5 9 29 3
833482: 2 9 4 39 299 16 33 2
62188602: 30 94 1 2 8 277 3 60 5
646272: 83 38 5 5 2 22 48
7797254949: 448 10 4 3 5 87 666 9
182106834186: 3 71 7 899 78 1 89
204713987: 27 967 76 73 25 61
65062185851: 8 406 2 9 9 3 1 8 5 85 2 1
2442779235476: 2 4 42 779 235 476
1101541404783: 5 494 8 784 1 8 63 2 9 6
20022139050: 6 577 756 51 55 150
9334693099: 93 3 469 309 7
43468360: 90 1 15 3 6 5 26 6 6 86 4
53125168450: 2 1 14 90 81 2 9 21 4 50
1675: 50 2 86 7 79 6 192 1 95
44954008: 726 86 18 4 8 6
1855: 84 2 5 97 6 706 2 953
3398: 3 911 6 93 5 561
240590073: 840 612 234 4 312 2
45648251: 36 3 2 9 22 747 481 54
31494224: 501 6 62 60 224
19022: 3 1 4 2 48 1 53 4
470975: 54 2 62 65 704
83169544: 7 4 840 998 9 562
42134273: 1 819 585 37 3 31 73
1332: 96 5 366
207764: 4 98 53 4
35716660368: 420 1 96 85 368
154816372258: 2 6 8 1 523 7 8 1 709 6 4
47658307: 35 1 1 983 1 8 42 70 85
1285582: 5 89 4 218 691
8449218636: 92 4 8 151 4 879 6 639
811125: 9 9 91 9 6 312
6540: 5 67 8 492 367
248227642: 3 755 686 807 80 213
178168925418: 134 7 8 4 7 8 4 6 745 9 2
40272366834: 4 63 657 954 959
752285864242: 940 3 5 7 330 3 80 1
57089214: 1 8 753 4 9 91 91
254401: 5 5 5 9 2 706 12 4 191 1
289690622: 438 6 7 96 6 669 1 240
64649: 73 7 8 8 9 1
7651: 85 9 65 48 19
1821: 2 2 1 8 9 42
193243: 2 5 5 9 1 1 1 20 8 788 2 4
39264864: 6 9 9 73 25 1 54 8 2 552
5618264957: 81 261 4 16 88 70 659
27290: 8 98 693 280 15 935
35742721: 8 716 78 8 4
9360: 18 8 65
229680792: 11 72 1 1 2 3 533 6 2 9 6
1395376: 9 9 34 5 7 98 739 7
425812869100: 266 12 667 345 2 2 5 8
116284: 6 37 36 9 232 65
227233559: 51 1 93 43 167 51 5
25013651680: 710 615 44 60 1 399 8
12783096: 8 7 5 6 5 2 745 85 96
122796: 6 2 9 2 8 200 2 6 27 1 36
3201265: 714 94 4 6 66
13432931938: 961 173 73 5 5 12 673
1491958: 586 67 38
307760: 12 26 4 70 8
127538520: 6 6 511 435 536
3804190: 439 1 3 9 96
10528777518: 7 94 55 373 77 75 15
3950245: 3 631 89 6 7 4
11351826131: 47 89 8 237 134
1371750: 42 14 2 93 25
10089793: 34 970 87 410 90
135793394: 5 7 2 1 6 14 8 62 7 17 4 7
5467360: 25 4 31 190 45 32
49150761097: 98 85 1 4 892 5 4 7 667
25020907: 53 281 6 10 1 2 14 639
20376257: 97 21 59 10 349
25198543: 8 8 2 129 6 98 6 4 3 3 5 5
8740866: 87 202 205 1 65
765976644056: 979 509 775 782 5
211736: 882 5 8 6 56
172867358762: 67 258 7 358 760
27021769152: 270 99 6 752 9 2 7 6 8 4
6486: 64 8 6
16370794990653: 715 795 5 3 8 162 4 72
75950: 8 8 1 2 7 1 102 74 7 50
1890962164: 3 75 9 8 9 7 6 8 53 2 4 1
36552: 7 155 9 4 25 2
528976841: 459 69 971 82 501 9
12617168724: 4 9 6 8 686 427 322 5 5
21463150097: 7 3 4 63 150 10 97
1291956: 5 6 78 2 6 30 93 31 2 2
2767401: 1 2 1 9 5 8 5 1 951 403
654853: 94 2 466 353 492 5
8482556: 1 69 75 585 56
776226044: 2 3 275 2 4 69 9 4 41
2897117690022: 9 29 50 5 5 77 2 8 3 9 9 2
20834: 209 65 74 4 557
53683: 4 3 7 2 9 9 3 538 959 6 5
1072822036014: 25 763 7 83 592 1 95
28470973867: 1 807 6 98 6 9 4 8 60 5 5
4730450: 5 666 6 834
38783138: 38 7 1 82 20 93 5 5
1229280: 296 477 343 66 16 65
2105673483: 66 997 4 218 967 8
99285: 3 669 7 49 9 69 512 9
9135: 5 4 4 4 92 3 481 828 95
418720: 16 26 24 248 2
41660694: 6 132 868 98 3
79461422: 799 64 711 92 8
78900784: 164 879 9 75 785
321783039886: 5 7 7 5 366 3 8 9 8 51 35
44238422: 9 1 3 767 2 8 3 1 7 4 4 22
4052: 19 1 7 2 75
4241515618: 7 6 415 156 20
172815106: 78 3 6 4 3 5 37 7 8 662 6
12961912: 629 7 733 7 53 6 9 1 9 6
3221260: 5 324 99 6 99
69252725603: 961 840 3 409 72 155
64606511: 6 3 5 7 96 7 2 5 2 1 6 2
2712508: 1 7 795 3 3 9 3 426 4 2 6
96279559: 7 1 907 6 463 8 955 9
17382804784: 71 1 3 6 19 37 8 57 42 2
608: 355 84 58 3 108
22788516579714: 8 2 2 77 51 2 3 685 3 7 5
10413092463: 61 9 8 261 8 3 84 9 2 7 2
493395: 5 341 35 5 7 37
1110: 45 8 53 5 914 85
290494566: 10 9 78 9 590 6 6 9 5 69
217807638: 5 1 599 3 7 85 55 63 6
553644516: 553 6 445 15
2494: 25 202 85 8 1
1130126068: 891 317 53 31 517 4
580462601: 6 4 99 8 4 8 4 72 8 32 8 9
94899600: 99 2 6 9 696 25
7180338: 14 3 5 1 5 48 41
17883: 992 3 3 9 2 5 4
298960: 3 6 3 3 78 9 904 16 5 16
1256036: 4 60 5 522 626
10056175920: 7 77 9 1 135 102 46 3 7
214394: 9 85 6 1 28
222768: 93 2 2 26 9
70896: 47 4 139 3 1
828727414020: 56 810 70 82 870 3 2
2726478861: 6 2 953 28 2 509 70
3661356469: 10 951 5 770 64 68
3093697: 61 11 82 524 1
1581223: 2 399 29 478 4
144528: 2 6 70 66 832 7 8 308 8
1560089796: 8 9 724 8 10 999 3 7 7
524867255: 7 91 4 6 3 4 68 8 4 74 5 5
6546332840318: 9 58 10 3 6 6 6 7 209 1 9
340267842041: 48 3 3 35 4 44 2 2 5 6 4 1
106619048: 56 411 41 99 424 5 6 2
11007: 35 523 4 74 587 9
3448675: 3 1 5 1 1 172 1 3 2 7 4
3232805727: 4 30 3 898 5 730
286485725986: 65 9 4 4 63 4 6 3 7 332 9
68357520: 28 8 55 7 822 6
5328894: 565 316 8 7 6 3 3 85 7 6
271298568: 83 813 8 402 82 9
71406: 92 621 87 3 6 8
3850084094: 5 5 700 795 43 1 7 277
2647261804: 8 7 3 1 93 73 84 3 60 2 4
39655042363: 6 66 550 42 363
6826456: 31 8 19 8 1 8 5 202 2 65
207376577273: 665 3 9 565 2 31 2 5 2 1
84617: 7 2 22 9 16
2508164334: 71 3 8 4 800 164 336
8846609: 5 51 55 67 70 71 7
817389001526: 47 8 19 1 9 900 1 528 1
231543: 6 78 494 23 328
2704854957: 3 20 2 28 3 1 549 55
87792: 1 8 7 792
728526617: 6 2 5 816 8 5 4 9 5 4 3 8
4467157029: 7 9 4 6 3 8 1 335 9 3 7 70
119125482: 5 9 1 9 2 8 7 1 77 3 86
606124: 24 25 9 46 5 26 1 99
36952541624: 5 39 1 379 5 1 3 161 6 7
3065247: 7 3 1 4 30 258 19 2 2 9
971579: 480 869 72 29 9
1891834: 16 525 5 40 45 34
17800903630: 232 6 9 4 9 2 7 74 7 8 9 7
39253993319: 32 10 7 9 2 46 8 6 85 9
4808394700: 56 779 3 883 11 8 702
7543: 5 3 50 4 4 1 9 9 6 8 618 8
6152242: 1 8 6 74 1 9 94 760 38 2
21560435: 904 53 5 18 7 5
6452: 78 5 14 8 917 67
34492964: 54 46 53 262 47 91
6143: 8 1 4 76 69
2650433: 3 503 638 3 64
32608539392: 1 4 2 485 4 8 88 13 694
996914: 8 8 11 6 9 15
7491434: 7 1 490 53 1 10 1 79 2 4
1444787: 268 93 4 531 254
1699218797: 1 3 91 40 9 93 5 2 9 9
125: 45 2 14 2 3
1560635136942: 312 5 635 13 694 5
1364803895: 988 77 2 138 7 65
185892174: 5 108 8 7 51 8 8 1 90 4 9
4909: 9 63 68 6 7
125698: 77 265 535 6 59
539863272: 2 3 9 536 6 7 1 37 51 72
703320: 4 6 8 6 9 4 213 5 3 9 410
1171: 1 9 122 2 71
87545446: 4 1 8 9 71 90 92 2 87
3454542861344: 3 33 26 399 861 3 44 1
198829067: 6 8 64 5 4 2 14 9 152 7
3637527: 69 4 9 61 6 24 67 740
115169108: 59 3 3 39 449 9 8 4 50 8
52105: 1 9 3 2 62 137 5 45
9997: 386 772 91 2 4 5
1698163241434: 288 41 8 7 736 1 9 436
13954511681: 852 8 59 2 3 303 18
62128443: 49 3 7 51 3 9 6 1 187 3
33963194: 2 3 8 5 5 1 8 4 6 388 9 5
166733310603: 9 5 24 4 91 6 9 6 969 4 6
4742402: 7 614 7 582 53 10 3 32
1205991360: 34 6 32 9 4 56 45 734
92363911263: 3 57 9 2 1 9 2 6 521 6 2
404026558: 816 2 63 8 980
4114042047: 9 1 6 6 4 8 7 2 20 7 204 7
187800: 2 8 6 7 2 385 503 3 6 4 5
773000606511: 480 5 623 92 7 5 2 48 9
4575227: 4 2 1 21 6 9 73 5 6 8 3 24
24437686: 5 541 71 37 64 88
7024283808: 707 545 6 603 935
15541937280: 605 657 7 391 75 8
1588304669: 8 1 5 1 8 7 7 4 700 6 66
55582288572: 3 70 94 5 9 4 9 4 8 45 3 9
1258520904: 1 569 4 151 10 7 6 2 4
194000: 210 22 42 708 5
1004532: 2 9 977 9 532
28242055: 31 9 7 387 71 255 40
2497162: 3 80 27 1 39 383 2
2113202995948: 132 512 599 58 83 9
4169396: 45 4 1 6 895 9 211 247
4167651: 783 9 3 5 7 4 5 8 5 757 2
62430315: 6 39 977 1 8
145045721765: 38 3 3 9 8 92 204 863 4
1713620: 85 681 20
8652047: 4 8 61 2 719 204 5 2
83092616: 25 73 5 247 3 5 77 8
298683: 44 22 9 2 492 1 524 3
209527771530: 737 25 465 58 49
5348998: 891 4 8 8 75
102566433: 7 9 5 1 2 8 2 7 31 91 3 2
16159: 6 5 8 839 218
116652639: 12 8 4 97 9 911
1177258162: 21 8 6 5 317 8 18 955
88785: 7 413 68 6 1 3
24757333: 745 4 27 2 5 2 9 68 97
12893032: 11 865 5 271 152 55
4150408646: 8 63 904 532 5
1424648: 7 747 339 179 4 35 8 8
639080: 78 315 10 2 13
64638140111: 140 29 11 2 537 78
23239: 83 74 148
22735784: 5 39 42 97 78 4
1776931595: 6 633 704 5 5 79
420869739792: 601 2 42 2 2 8 4 8 6 7 3 6
300460: 5 9 4 7 4 7 626 5 8
49077: 23 75 41 72 12
5846791: 58 467 7 1 9
128174255: 2 101 71 445 59
9548470573: 2 795 95 46 1 762 6
1200810: 704 568 3 888 306
8268178: 3 984 92 28 5
1251: 3 57 64 8 1 1
36297: 8 551 59 2 6 52 8 81
26920458: 9 89 40 97 55 8 6
63313: 6 6 7 866 93
41711425: 21 36 53 3 725
1266480065088: 578 8 1 2 547 627 4 4 2
1256275: 4 4 6 6 6 8 3 3 9 24 7 25
351751992: 272 9 8 1 5 1 24 6 52 7 4
1141050240: 506 4 87 8 810
2732172398: 4 3 8 16 3 59 65 2 69 98
496204: 82 2 1 7 89 54
2351040677: 4 7 4 505 60 24 67 8
20361680: 9 376 8 6 9 681
19565: 93 795 90 2 5
157: 6 4 8 39 39
55066: 3 923 75 55 10
678078: 9 53 37 539 1 4 7 5
2068976356: 17 8 48 812 254 99
1167049647: 476 7 141 1 4 9 531 69
47340932167: 493 644 4 42 70 991
3176645: 5 59 2 157 8 1 6 9 48 4
363525896226: 6 8 861 930 7 8 225
15948: 214 31 7 940 3 6
2167814926453: 78 887 687 4 5 807 2 4
2218: 2 6 5 469 18 956 687
294158590: 18 12 382 56 9 976
23101312: 181 9 2 635 9
9109854003: 87 587 9 78 375 456
13759: 9 140 70 6 453
410519041983: 3 3 9 9 4 5 4 36 8 8 1 981
38631736: 52 807 10 950 15 9
19389630: 4 884 87 7 390
2353160400888: 4 744 275 496 888
4620: 33 1 84 1 31 3 5 5 5 6
74595: 745 20 78
1007311921628: 4 880 6 440 8 4 4 647
553: 1 9 506 3 4 30
2879: 3 74 7 258 5
9755: 23 10 88 9 8
1283480: 2 2 90 5 8 67 8 1 33 1 5 7
5221132: 15 866 62 6 40 84 8
2077360320: 8 4 8 805 1 6 8 7 1 42 8 4
1215068: 651 266 275 7 982
21160956866: 41 935 12 8 46 66
265482518844: 301 882 5 188 44
4337814321: 43 93 84 493 643 2
363048346: 747 9 6 617 74 8 9 4 50
368017137496: 99 59 541 865 63
118224970527: 3 2 4 8 14 25 5 8 2 5 5 71
95338728: 30 92 9 571 6
719154430: 996 526 6 8 72 82
61229835: 6 425 2 8 5 7 7 3 9 150 8
1308850290566: 156 711 9 29 580 4 8 6
131674: 4 94 935 5 69 3 3
1720: 4 8 22 3 39 3 9 127 4 5
54321382: 97 56 12 70 46 65
578: 55 1 8 133 5
17120: 2 9 6 12 8 685 4 3 8 656
56014516: 55 72 27 2 41 13 403
1031940: 65 2 73 32 7 49 61 84 7
3480490: 2 5 70 3 497
109494584: 8 964 302 621 27 47
6624870401: 6 46 9 61 88 6 870 401
1932590647866: 41 45 3 1 1 9 7 6 8 3 6 74
4251407: 25 1 2 31 1 5 521 5 43
6370650: 57 83 1 835 99 66
1424752683: 212 56 932 7 4 3 3
2047788: 98 5 1 2 6 346 2 40 812
2135380: 9 62 5 5 764
16782872260: 44 873 99 4 935
529647: 1 980 79 29 5
1034281: 72 214 20 52 65
1481605493: 57 65 257 49 4
26823: 244 8 88 797 2 731 2 3
294907969: 8 88 9 5 3 738 956
258989: 71 4 6 91
17858: 6 4 3 248
263467053076: 62 82 7 3 5 509 342 76
13779755961: 7 3 573 7 5 5 6 365 22
370: 35 5 4 1 9
167010: 30 6 59 3 586
500009855775: 1 7 16 3 3 399 5 3 577 8
8470455: 28 52 495 2 6
16770537: 69 4 6 319 305 19 14
10543: 15 4 57 7 85 3
237976754: 5 718 2 5 19 52 4
115325496456: 6 3 31 253 8 9 382 75
8571959: 8 6 8 39 9 79 8 7 673 6
556951135688: 6 10 8 404 743 9 859 9
282684: 3 2 7 58 6 82
249472: 2 48 5 9 70
8295: 82 4 5 2 3
21988671258: 2 443 9 1 671 17 9 1
2737827433: 3 2 6 507 3 457 6 4 8
638692: 189 39 7 73 4
2939773270: 584 5 19 773 270
761567: 92 5 7 8 4 5 4 291 1 4
12640207: 3 1 5 6 3 1 13 5 6 9 692 3
417802012: 2 37 78 1 21 654
10995267: 4 1 9 1 4 2 3 1 84 9 6
902703291: 580 308 7 31 163
371907: 871 6 84 387
55152844588: 90 965 973 4 157 587
2831012450: 57 757 1 81 81

15
day7/input.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_day7_input_txt_start __asm("_binary_day7_input_txt_start");
extern uint32_t _binary_day7_input_txt_end __asm("_binary_day7_input_txt_end");
extern uint32_t _binary_day7_input_txt_size __asm("_binary_day7_input_txt_size");
#ifdef __cplusplus
}
#endif

9
day7/sample1.txt Normal file
View File

@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20

15
day7/sample1.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_day7_sample1_txt_start __asm("_binary_day7_sample1_txt_start");
extern uint32_t _binary_day7_sample1_txt_end __asm("_binary_day7_sample1_txt_end");
extern uint32_t _binary_day7_sample1_txt_size __asm("_binary_day7_sample1_txt_size");
#ifdef __cplusplus
}
#endif

121
day7/solution.c Normal file
View File

@ -0,0 +1,121 @@
#include <stdint.h>
#include <stdbool.h>
#include "parse.h"
#include "printf.h"
#include "unparse.h"
struct equation {
int64_t value;
int operand[32];
int operand_count;
};
const char * parse_equation(const char * input, const char * end, struct equation * equation)
{
input = parse_base10_64(input, &equation->value);
input = parse_skip(input, ':');
input = parse_skip(input, ' ');
equation->operand_count = 0;
while (*input != '\n') {
input = parse_base10(input, &equation->operand[equation->operand_count++]);
input = parse_skip(input, ' ');
}
return parse_skip(input, '\n');
}
static bool evaluate_equation1(struct equation * equation, int64_t acc, int depth)
{
if (depth >= equation->operand_count)
return acc == equation->value;
int64_t operand = equation->operand[depth];
int64_t acc1 = acc * operand;
int64_t acc2 = acc + operand;
return
evaluate_equation1(equation, acc1, depth + 1)
|| evaluate_equation1(equation, acc2, depth + 1);
}
int64_t day7_part1(const char * input, int length)
{
const char * end = input + length;
int64_t sum = 0;
while (input < end) {
struct equation equation;
input = parse_equation(input, end, &equation);
bool possible = evaluate_equation1(&equation, equation.operand[0], 1);
if (possible)
sum += equation.value;
}
return sum;
}
static int64_t pow10[20] = {
1ull,
10ull,
100ull,
1000ull,
10000ull,
100000ull,
1000000ull,
10000000ull,
100000000ull,
1000000000ull,
10000000000ull,
100000000000ull,
1000000000000ull,
10000000000000ull,
100000000000000ull,
1000000000000000ull,
10000000000000000ull,
100000000000000000ull,
1000000000000000000ull,
10000000000000000000ull,
};
int64_t concatenate(int64_t a, int64_t b)
{
int digits = digits_base10_64(b);
a *= pow10[digits];
return a + b;
}
static bool evaluate_equation2(struct equation * equation, int64_t acc, int depth)
{
if (depth >= equation->operand_count)
return acc == equation->value;
int64_t operand = equation->operand[depth];
int64_t acc1 = acc * operand;
int64_t acc2 = acc + operand;
int64_t acc3 = concatenate(acc, operand);
return
evaluate_equation2(equation, acc1, depth + 1)
|| evaluate_equation2(equation, acc2, depth + 1)
|| evaluate_equation2(equation, acc3, depth + 1);
}
int64_t day7_part2(const char * input, int length)
{
const char * end = input + length;
int64_t sum = 0;
while (input < end) {
struct equation equation;
input = parse_equation(input, end, &equation);
bool possible = evaluate_equation2(&equation, equation.operand[0], 1);
if (possible)
sum += equation.value;
}
return sum;
}

27
gen.sh
View File

@ -13,12 +13,14 @@ if [ ! -z "$day" ]; then
mkdir -p day${day}
cat <<EOF > day${day}/solution.c
int day${day}_part1(char * input, int length)
#include <stdint.h>
int day${day}_part1(const char * input, int length)
{
return -1;
}
int day${day}_part2(char * input, int length)
int day${day}_part2(const char * input, int length)
{
return -1;
}
@ -87,18 +89,33 @@ for i in day* ; do
echo " ${i}/sample2.txt.o \\" >> solutions.mk
fi
echo " ${i}/input.txt.o \\" >> solutions.mk
if [ -f ${i}/render.cpp ]; then
echo " ${i}/render.o \\" >> solutions.mk
fi
echo -n " ${i}/solution.o" >> solutions.mk
done
echo >> solutions.mk
truncate -s0 runner.inc
for i in day* ; do
echo "int ${i}_part1(char * input, int length);" >> runner.inc
echo "int ${i}_part2(char * input, int length);" >> runner.inc
echo "int64_t ${i}_part1(const char * input, int length);" >> runner.inc
echo "int64_t ${i}_part2(const char * input, int length);" >> runner.inc
if [ -f ${i}/render.cpp ]; then
echo "void ${i}_render(const struct font * font," >> runner.inc
echo " const struct glyph * glyphs," >> runner.inc
echo " const void * maple_ft0_data);" >> runner.inc
fi
done
echo >> runner.inc
echo "part_func solution[][2] = {" >> runner.inc
echo "struct day_funcs solution[] = {" >> runner.inc
for i in day* ; do
echo " {" >> runner.inc
echo " {${i}_part1, ${i}_part2}," >> runner.inc
if [ -f ${i}/render.cpp ]; then
echo " ${i}_render," >> runner.inc
else
echo " NULL," >> runner.inc
fi
echo " }," >> runner.inc
done
echo "};" >> runner.inc

View File

@ -9,6 +9,10 @@
#include "day4/input.txt.h"
#include "day5/sample1.txt.h"
#include "day5/input.txt.h"
#include "day6/sample1.txt.h"
#include "day6/input.txt.h"
#include "day7/sample1.txt.h"
#include "day7/input.txt.h"
static struct start_size sample[][2] = {
{
@ -41,6 +45,18 @@ static struct start_size sample[][2] = {
{ ( char *)&_binary_day5_sample1_txt_start,
(uint32_t)&_binary_day5_sample1_txt_size },
},
{
{ ( char *)&_binary_day6_sample1_txt_start,
(uint32_t)&_binary_day6_sample1_txt_size },
{ ( char *)&_binary_day6_sample1_txt_start,
(uint32_t)&_binary_day6_sample1_txt_size },
},
{
{ ( char *)&_binary_day7_sample1_txt_start,
(uint32_t)&_binary_day7_sample1_txt_size },
{ ( char *)&_binary_day7_sample1_txt_start,
(uint32_t)&_binary_day7_sample1_txt_size },
},
};
static struct start_size input[] = {
@ -54,4 +70,8 @@ static struct start_size input[] = {
(uint32_t)&_binary_day4_input_txt_size },
{ ( char *)&_binary_day5_input_txt_start,
(uint32_t)&_binary_day5_input_txt_size },
{ ( char *)&_binary_day6_input_txt_start,
(uint32_t)&_binary_day6_input_txt_size },
{ ( char *)&_binary_day7_input_txt_start,
(uint32_t)&_binary_day7_input_txt_size },
};

42
parse.c
View File

@ -45,6 +45,23 @@ const char * parse_base10(const char * s, int * n)
return s;
}
const char * parse_base10_64(const char * s, int64_t * n)
{
*n = 0;
while (true) {
int digit = base10_digit(*s);
if (digit == -1)
break;
*n *= 10;
*n += digit;
s++;
}
return s;
}
const char * parse_match(const char * s, const char * m)
{
while (*m != 0) {
@ -54,3 +71,28 @@ const char * parse_match(const char * s, const char * m)
}
return s;
}
int parse_stride(const char * s, int length)
{
for (int i = 0; i < length; i++) {
if (s[i] == '\n') {
return i + 1;
}
}
return -1;
}
int parse_height(const char * s, int length)
{
int rows = 0;
int i;
for (i = 0; i < length; i++) {
if (s[i] == '\n') {
rows += 1;
}
}
if (s[i-1] != '\n') {
rows += 1;
}
return rows;
}

View File

@ -1,12 +1,17 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
const char * parse_skip(const char * s, char c);
const char * parse_base10(const char * s, int * n);
const char * parse_base10_64(const char * s, int64_t * n);
const char * parse_match(const char * s, const char * m);
int parse_stride(const char * s, int length);
int parse_height(const char * s, int length);
#ifdef __cplusplus
}

View File

@ -8,6 +8,7 @@
enum format_type {
FORMAT_BASE10,
FORMAT_BASE10_64,
FORMAT_BASE16,
FORMAT_STRING,
FORMAT_PERCENT,
@ -38,6 +39,9 @@ static const char * parse_escape(const char * format, struct format * ft)
case 'd':
ft->type = FORMAT_BASE10;
return format + 1;
case 'l':
ft->type = FORMAT_BASE10_64;
return format + 1;
case 'x':
ft->type = FORMAT_BASE16;
return format + 1;
@ -77,6 +81,14 @@ void _printf(const char * format, ...)
global_output_buffer.buf_ix += offset;
}
break;
case FORMAT_BASE10_64:
{
int64_t num = va_arg(args, int64_t);
char * s = &global_output_buffer.buf[global_output_buffer.buf_ix];
int offset = unparse_base10_64(s, num, ft.pad_length, ft.fill_char);
global_output_buffer.buf_ix += offset;
}
break;
case FORMAT_BASE16:
{
uint32_t num = va_arg(args, uint32_t);

View File

@ -1,10 +1,20 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "printf.h"
#include "input.h"
#include "runner.h"
typedef int (* part_func)(char * input, int length);
typedef int64_t (* part_func)(const char * input, int length);
typedef void (* render_func)(const struct font * font,
const struct glyph * glyphs,
const void * maple_ft0_data);
struct day_funcs {
part_func part[2];
render_func render;
};
#include "runner.inc"
const int solution_days = (sizeof (solution)) / (sizeof (solution[0]));
@ -14,18 +24,37 @@ bool runner_tick(struct runner_state * runner_state)
int part = runner_state->tick % 2;
int day = runner_state->tick / 2;
if (day >= solution_days)
if (day >= solution_days) {
runner_state->want_render = false;
return true;
}
runner_state->want_render = solution[day].render != NULL;
char * buf;
int length;
//open_sample(day + 1, part + 1, &buf, &length);
open_input(day + 1, &buf, &length);
int answer = solution[day][part](buf, length);
int64_t answer = solution[day].part[part](buf, length);
printf("day %d part %d: %d\n", day + 1, part + 1, answer);
printf("day %d part %d: %l\n", day + 1, part + 1, answer);
runner_state->tick += 1;
return false;
}
void runner_render(struct runner_state * runner_state,
const struct font * font,
const struct glyph * glyphs,
const void * maple_ft0_data)
{
int day = runner_state->tick / 2;
if (day >= solution_days) {
return;
}
if (solution[day].render != NULL) {
solution[day].render(font, glyphs, maple_ft0_data);
}
}

View File

@ -2,15 +2,22 @@
#include <stdbool.h>
#include "font/font.h"
#ifdef __cplusplus
extern "C" {
#endif
struct runner_state {
int tick;
bool want_render;
};
bool runner_tick(struct runner_state * runner_state);
void runner_render(struct runner_state * runner_state,
const struct font * font,
const struct glyph * glyphs,
const void * maple_ft0_data);
#ifdef __cplusplus
}

View File

@ -1,18 +1,48 @@
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);
int day3_part1(char * input, int length);
int day3_part2(char * input, int length);
int day4_part1(char * input, int length);
int day4_part2(char * input, int length);
int day5_part1(char * input, int length);
int day5_part2(char * input, int length);
int64_t day1_part1(const char * input, int length);
int64_t day1_part2(const char * input, int length);
int64_t day2_part1(const char * input, int length);
int64_t day2_part2(const char * input, int length);
int64_t day3_part1(const char * input, int length);
int64_t day3_part2(const char * input, int length);
int64_t day4_part1(const char * input, int length);
int64_t day4_part2(const char * input, int length);
int64_t day5_part1(const char * input, int length);
int64_t day5_part2(const char * input, int length);
int64_t day6_part1(const char * input, int length);
int64_t day6_part2(const char * input, int length);
void day6_render(const struct font * font,
const struct glyph * glyphs,
const void * maple_ft0_data);
int64_t day7_part1(const char * input, int length);
int64_t day7_part2(const char * input, int length);
part_func solution[][2] = {
struct day_funcs solution[] = {
{
{day1_part1, day1_part2},
NULL,
},
{
{day2_part1, day2_part2},
NULL,
},
{
{day3_part1, day3_part2},
NULL,
},
{
{day4_part1, day4_part2},
NULL,
},
{
{day5_part1, day5_part2},
NULL,
},
{
{day6_part1, day6_part2},
day6_render,
},
{
{day7_part1, day7_part2},
NULL,
},
};

View File

@ -1,4 +1,5 @@
#include <cstdint>
#include <bit>
#include "holly/texture_memory_alloc2.hpp"
#include "holly/isp_tsp.hpp"
@ -23,7 +24,7 @@
#include "systembus.hpp"
#include "systembus_bits.hpp"
#include "font/font.hpp"
#include "font/font.h"
#include "font/dejavusansmono/dejavusansmono.data.h"
#include "palette.hpp"
@ -31,6 +32,61 @@
#include "printf.h"
#include "runner.h"
#include "maple/maple.hpp"
#include "maple/maple_host_command_writer.hpp"
#include "maple/maple_bus_bits.hpp"
#include "maple/maple_bus_commands.hpp"
#include "maple/maple_bus_ft0.hpp"
static ft0::data_transfer::data_format maple_ft0_data[4];
static uint8_t send_buf[1024] __attribute__((aligned(32)));
static uint8_t recv_buf[1024] __attribute__((aligned(32)));
static bool maple_wait = false;
void do_get_condition()
{
if (maple_wait)
maple::dma_wait_complete();
auto writer = maple::host_command_writer(send_buf, recv_buf);
writer.append_command_all_ports<maple::device_request, maple::device_status>(false);
using command_type = maple::get_condition;
using response_type = maple::data_transfer<ft0::data_transfer::data_format>;
auto [host_command, host_response]
= writer.append_command_all_ports<command_type, response_type>();
host_command->bus_data.data_fields.function_type = std::byteswap(function_type::controller);
maple::dma_start(send_buf, writer.send_offset,
recv_buf, writer.recv_offset);
maple_wait = true;
for (uint8_t port = 0; port < 4; port++) {
auto& bus_data = host_response[port].bus_data;
maple_ft0_data[port].digital_button = 0;
if (bus_data.command_code != response_type::command_code) {
return;
}
auto& data_fields = bus_data.data_fields;
if ((std::byteswap(data_fields.function_type) & function_type::controller) == 0) {
return;
}
maple_ft0_data[port].digital_button = data_fields.data.digital_button;
maple_ft0_data[port].analog_coordinate_axis[0] = data_fields.data.analog_coordinate_axis[0];
maple_ft0_data[port].analog_coordinate_axis[1] = data_fields.data.analog_coordinate_axis[1];
maple_ft0_data[port].analog_coordinate_axis[2] = data_fields.data.analog_coordinate_axis[2];
maple_ft0_data[port].analog_coordinate_axis[3] = data_fields.data.analog_coordinate_axis[3];
maple_ft0_data[port].analog_coordinate_axis[4] = data_fields.data.analog_coordinate_axis[4];
maple_ft0_data[port].analog_coordinate_axis[5] = data_fields.data.analog_coordinate_axis[5];
}
}
struct vertex {
float x;
float y;
@ -49,7 +105,7 @@ const struct vertex strip_vertices[4] = {
constexpr uint32_t strip_length = (sizeof (strip_vertices)) / (sizeof (struct vertex));
void transform_start(const uint32_t texture_width, uint32_t texture_height)
void glyph_start(const uint32_t texture_width, uint32_t texture_height)
{
const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume
| para_control::list_type::translucent
@ -129,7 +185,7 @@ int32_t transform_char(const uint32_t texture_width,
void transfer_scene(const struct font * font,
const struct glyph * glyphs)
{
transform_start(font->texture_width, font->texture_height);
glyph_start(font->texture_width, font->texture_height);
int32_t horizontal_advance = font->face_metrics.max_advance / 5; // 26.6 fixed point
int32_t vertical_advance = font->face_metrics.height; // 26.6 fixed point
@ -151,10 +207,6 @@ void transfer_scene(const struct font * font,
vertical_advance);
}
}
*reinterpret_cast<ta_global_parameter::end_of_list *>(store_queue) =
ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
sq_transfer_32byte(ta_fifo_polygon_converter);
}
void copy_font(const uint8_t * src,
@ -197,6 +249,8 @@ constexpr int tile_height = framebuffer_height / 32;
static uint32_t frame = 0;
struct runner_state runner_state = {0};
void render()
{
if (core >= 0) {
@ -236,7 +290,15 @@ void render()
ta_alloc,
tile_width,
tile_height);
///
transfer_scene(font, glyphs);
if (runner_state.want_render) {
runner_render(&runner_state, font, glyphs, maple_ft0_data);
}
*reinterpret_cast<ta_global_parameter::end_of_list *>(store_queue) =
ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
sq_transfer_32byte(ta_fifo_polygon_converter);
}
void vbr600()
@ -254,6 +316,8 @@ void vbr600()
serial::integer<uint32_t>(system.ISTNRM & system.IML6NRM);
*/
do_get_condition();
render();
// reset v_blank_in interrupt
@ -271,6 +335,12 @@ void vbr400()
void vbr100()
{
serial::string("vbr100\n");
serial::string("expevt ");
serial::integer<uint16_t>(sh7091.CCN.EXPEVT);
serial::string("intevt ");
serial::integer<uint16_t>(sh7091.CCN.INTEVT);
serial::string("tra ");
serial::integer<uint16_t>(sh7091.CCN.TRA);
while (1);
}
@ -354,8 +424,6 @@ int main()
copy_font(texture, font->max_z_curve_ix);
palette_data<256>();
struct runner_state runner_state = {0};
video_output::set_mode_vga();
ta = -1;

View File

@ -14,4 +14,11 @@ DAY_OBJ = \
day4/solution.o \
day5/sample1.txt.o \
day5/input.txt.o \
day5/solution.o
day5/solution.o \
day6/sample1.txt.o \
day6/input.txt.o \
day6/render.o \
day6/solution.o \
day7/sample1.txt.o \
day7/input.txt.o \
day7/solution.o

View File

@ -2,18 +2,19 @@
#include <stdbool.h>
#include "minmax.h"
#include "unparse.h"
static int digits_base10(uint32_t n)
int digits_base10(uint32_t n)
{
if (n >= 1000000000) return 10;
if (n >= 100000000) return 9;
if (n >= 10000000) return 8;
if (n >= 1000000) return 7;
if (n >= 100000) return 6;
if (n >= 10000) return 5;
if (n >= 1000) return 4;
if (n >= 100) return 3;
if (n >= 10) return 2;
if (n >= 1000000000ul) return 10;
if (n >= 100000000ul) return 9;
if (n >= 10000000ul) return 8;
if (n >= 1000000ul) return 7;
if (n >= 100000ul) return 6;
if (n >= 10000ul) return 5;
if (n >= 1000ul) return 4;
if (n >= 100ul) return 3;
if (n >= 10ul) return 2;
return 1;
}
@ -50,6 +51,63 @@ int unparse_base10(char * s, int32_t n, int len, char fill)
return ret;
}
int digits_base10_64(uint64_t n)
{
if (n >= 10000000000000000000ull) return 20;
if (n >= 1000000000000000000ull) return 19;
if (n >= 100000000000000000ull) return 18;
if (n >= 10000000000000000ull) return 17;
if (n >= 1000000000000000ull) return 16;
if (n >= 100000000000000ull) return 15;
if (n >= 10000000000000ull) return 14;
if (n >= 1000000000000ull) return 13;
if (n >= 100000000000ull) return 12;
if (n >= 10000000000ull) return 11;
if (n >= 1000000000ull) return 10;
if (n >= 100000000ull) return 9;
if (n >= 10000000ull) return 8;
if (n >= 1000000ull) return 7;
if (n >= 100000ull) return 6;
if (n >= 10000ull) return 5;
if (n >= 1000ull) return 4;
if (n >= 100ull) return 3;
if (n >= 10ull) return 2;
return 1;
}
int unparse_base10_64(char * s, int64_t n, int len, char fill)
{
bool negative = false;
int digits = 0;
if (n < 0) {
digits += 1;
n = -n;
negative = true;
}
digits += digits_base10_64(n);
len = max(digits, len);
int ret = len;
while (len > digits) {
*s++ = fill;
--len;
}
if (negative) {
*s++ = '-';
len--;
}
while (len > 0) {
const uint32_t digit = n % 10;
n = n / 10;
s[--len] = digit + 48;
}
return ret;
}
static int digits_base16(uint32_t n)
{
if (n <= 0xf) return 1;

View File

@ -4,9 +4,13 @@
extern "C" {
#endif
int unparse_base10(char * s, uint32_t n, int len, char fill);
int unparse_base10(char * s, int32_t n, int len, char fill);
int unparse_base10_64(char * s, int64_t n, int len, char fill);
int unparse_base16(char * s, uint32_t n, int len, char fill);
int digits_base_64(uint64_t n);
int digits_base10_64(uint64_t n);
#ifdef __cplusplus
}
#endif