Skip to content

Commit 32167b6

Browse files
authored
Initial commit
0 parents commit 32167b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+12835
-0
lines changed

.editorconfig

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[*]
2+
cpp_indent_braces=false
3+
cpp_indent_multi_line_relative_to=innermost_parenthesis
4+
cpp_indent_within_parentheses=indent
5+
cpp_indent_preserve_within_parentheses=false
6+
cpp_indent_case_labels=true
7+
cpp_indent_case_contents=true
8+
cpp_indent_case_contents_when_block=false
9+
cpp_indent_lambda_braces_when_parameter=true
10+
cpp_indent_goto_labels=one_left
11+
cpp_indent_preprocessor=leftmost_column
12+
cpp_indent_access_specifiers=false
13+
cpp_indent_namespace_contents=true
14+
cpp_indent_preserve_comments=false
15+
cpp_new_line_before_open_brace_namespace=ignore
16+
cpp_new_line_before_open_brace_type=ignore
17+
cpp_new_line_before_open_brace_function=ignore
18+
cpp_new_line_before_open_brace_block=ignore
19+
cpp_new_line_before_open_brace_lambda=ignore
20+
cpp_new_line_scope_braces_on_separate_lines=false
21+
cpp_new_line_close_brace_same_line_empty_type=false
22+
cpp_new_line_close_brace_same_line_empty_function=true
23+
cpp_new_line_before_catch=true
24+
cpp_new_line_before_else=true
25+
cpp_new_line_before_while_in_do_while=true
26+
cpp_space_before_function_open_parenthesis=remove
27+
cpp_space_within_parameter_list_parentheses=false
28+
cpp_space_between_empty_parameter_list_parentheses=false
29+
cpp_space_after_keywords_in_control_flow_statements=true
30+
cpp_space_within_control_flow_statement_parentheses=false
31+
cpp_space_before_lambda_open_parenthesis=false
32+
cpp_space_within_cast_parentheses=false
33+
cpp_space_after_cast_close_parenthesis=false
34+
cpp_space_within_expression_parentheses=false
35+
cpp_space_before_block_open_brace=true
36+
cpp_space_between_empty_braces=false
37+
cpp_space_before_initializer_list_open_brace=true
38+
cpp_space_within_initializer_list_braces=true
39+
cpp_space_preserve_in_initializer_list=true
40+
cpp_space_before_open_square_bracket=false
41+
cpp_space_within_square_brackets=false
42+
cpp_space_before_empty_square_brackets=false
43+
cpp_space_between_empty_square_brackets=false
44+
cpp_space_group_square_brackets=true
45+
cpp_space_within_lambda_brackets=false
46+
cpp_space_between_empty_lambda_brackets=false
47+
cpp_space_before_comma=false
48+
cpp_space_after_comma=true
49+
cpp_space_remove_around_member_operators=true
50+
cpp_space_before_inheritance_colon=true
51+
cpp_space_before_constructor_colon=true
52+
cpp_space_remove_before_semicolon=true
53+
cpp_space_after_semicolon=false
54+
cpp_space_remove_around_unary_operator=true
55+
cpp_space_around_binary_operator=insert
56+
cpp_space_around_assignment_operator=insert
57+
cpp_space_pointer_reference_alignment=left
58+
cpp_space_around_ternary_operator=insert
59+
cpp_wrap_preserve_blocks=one_liners

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt text eol=lf

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a.out
2+
*.exe
3+
*.o
4+
.vscode/
5+
.antlr/

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Ishan Pranav
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
CC = gcc
2+
CFLAGS = -O3 -pedantic -std=c99 -Wall -Wextra
3+
LIBM = -lm
4+
TWOS_COMPLEMENT = -fno-strict-overflow -fwrapv
5+
6+
all: \
7+
day01a day01b \
8+
day02a day02b \
9+
day03a day03b \
10+
day04a day04b \
11+
day05a day05b \
12+
day06a day06b \
13+
day07a day07b \
14+
day08a day08b \
15+
day09a day09b \
16+
day10a \
17+
day11a day11b \
18+
day12a day12b \
19+
day13a day13b \
20+
day14a day14b \
21+
day15a day15b \
22+
day16a day16b \
23+
day17a day17b \
24+
day18a day18b \
25+
day19a day19b \
26+
day20a day20b \
27+
day21a \
28+
day22a day22b \
29+
day24a day24b \
30+
day25z
31+
32+
day01a: src/day01a.c
33+
$(CC) $(CFLAGS) $< -o $@.o
34+
35+
day01b: src/day01b.c
36+
$(CC) $(CFLAGS) $< -o $@.o
37+
38+
day02a: src/day02a.c
39+
$(CC) $(CFLAGS) $< -o $@.o
40+
41+
day02b: src/day02b.c
42+
$(CC) $(CFLAGS) $< -o $@.o
43+
44+
day03a: src/day03a.c
45+
$(CC) $(CFLAGS) $< -o $@.o
46+
47+
day03b: src/day03b.c
48+
$(CC) $(CFLAGS) $< -o $@.o
49+
50+
day04a: src/day04a.c
51+
$(CC) $(CFLAGS) $< -o $@.o
52+
53+
day04b: src/day04b.c
54+
$(CC) $(CFLAGS) $< -o $@.o
55+
56+
day05a: src/day05a.c
57+
$(CC) $(CFLAGS) $< -o $@.o
58+
59+
day05b: src/day05b.c
60+
$(CC) $(CFLAGS) $(TWOS_COMPLEMENT) $< -o $@.o
61+
62+
day06a: src/day06a.c
63+
$(CC) $(CFLAGS) $< -o $@.o $(LIBM)
64+
65+
day06b: src/day06b.c
66+
$(CC) $(CFLAGS) $< -o $@.o $(LIBM)
67+
68+
day07a: src/day07a.c
69+
$(CC) $(CFLAGS) $< -o $@.o
70+
71+
day07b: src/day07b.c
72+
$(CC) $(CFLAGS) $< -o $@.o
73+
74+
day08a: src/day08a.c
75+
$(CC) $(CFLAGS) $< -o $@.o
76+
77+
day08b: src/day08b.c
78+
$(CC) $(CFLAGS) $< -o $@.o
79+
80+
day09a: src/day09a.c
81+
$(CC) $(CFLAGS) $< -o $@.o $(LIBM)
82+
83+
day09b: src/day09b.c
84+
$(CC) $(CFLAGS) $< -o $@.o $(LIBM)
85+
86+
day10a: src/day10a.c
87+
$(CC) $(CFLAGS) $< -o $@.o
88+
89+
day11a: src/day11a.c
90+
$(CC) $(CFLAGS) $< -o $@.o
91+
92+
day11b: src/day11b.c
93+
$(CC) $(CFLAGS) $< -o $@.o
94+
95+
day12a: src/day12a.c
96+
$(CC) $(CFLAGS) $< -o $@.o
97+
98+
day12b: src/day12b.c
99+
$(CC) $(CFLAGS) $< -o $@.o
100+
101+
day13a: src/day13a.c
102+
$(CC) $(CFLAGS) $< -o $@.o
103+
104+
day13b: src/day13b.c
105+
$(CC) $(CFLAGS) $< -o $@.o
106+
107+
day14a: src/day14a.c
108+
$(CC) $(CFLAGS) $< -o $@.o
109+
110+
day14b: src/day14b.c
111+
$(CC) $(CFLAGS) $< -o $@.o
112+
113+
day15a: src/day15a.c
114+
$(CC) $(CFLAGS) $< -o $@.o
115+
116+
day15b: src/day15b.c
117+
$(CC) $(CFLAGS) $< -o $@.o
118+
119+
day16a: src/day16a.c
120+
$(CC) $(CFLAGS) $< -o $@.o
121+
122+
day16b: src/day16b.c
123+
$(CC) $(CFLAGS) $< -o $@.o
124+
125+
day17a: src/day17a.c
126+
$(CC) $(CFLAGS) $< -o $@.o
127+
128+
day17b: src/day17b.c
129+
$(CC) $(CFLAGS) $< -o $@.o
130+
131+
day18a: src/day18a.c
132+
$(CC) $(CFLAGS) $< -o $@.o
133+
134+
day18b: src/day18b.c
135+
$(CC) $(CFLAGS) $< -o $@.o
136+
137+
day19a: src/day19a.c
138+
$(CC) $(CFLAGS) $< -o $@.o
139+
140+
day19b: src/day19b.c
141+
$(CC) $(CFLAGS) $< -o $@.o
142+
143+
day20a: src/day20a.c
144+
$(CC) $(CFLAGS) $< -o $@.o
145+
146+
day20b: src/day20b.c
147+
$(CC) $(CFLAGS) $< -o $@.o
148+
149+
day21a: src/day21a.c
150+
$(CC) $(CFLAGS) $< -o $@.o
151+
152+
day22a: src/day22a.c
153+
$(CC) $(CFLAGS) $< -o $@.o
154+
155+
day22b: src/day22b.c
156+
$(CC) $(CFLAGS) $< -o $@.o
157+
158+
day24a: src/day24a.c
159+
$(CC) $(CFLAGS) $< -o $@.o
160+
161+
day24b: src/day24b.c
162+
$(CC) $(CFLAGS) $< -o $@.o
163+
164+
day25z: src/day25z.c
165+
$(CC) $(CFLAGS) $< -o $@.o
166+
167+
clean:
168+
rm -rf *.o

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Advent of Code 2023
2+
3+
This is a collection of solutions to the
4+
[Advent of Code 2023](https://adventofcode.com/2023) programming problems
5+
implemented in the C programming language. I have done my best to minimize time
6+
complexity and running time.
7+
8+
Some of the more complex problems were implemented in a
9+
[higher-level programming language](ref/) before being converted to C. The
10+
equivalent code is included here as a reference.
11+
12+
## Usage
13+
14+
These programs are cross-platform, having been tested on Windows and Linux
15+
operating systems.
16+
17+
**Important:** Do not use Windows-style line endings (`CR LF`, i.e. `\r\n`).
18+
Only use Unix-style line endings (`LF`, i.e. `\n`).
19+
20+
**Important:** I have tested each program on the example test cases and two
21+
puzzle input files - not a large sample size! I have avoided assumptions about
22+
the input, sometimes even at the cost of performance. However, to avoid memory
23+
allocation, all buffers have fixed sizes. Ensure that all buffers (see `#define`
24+
statements) have sufficient capacity before running. Not doing so could result
25+
in a stack smashing, segmentation fault, or worse.
26+
27+
## Summary
28+
29+
**Note:** The times below are expressed as orders of magnitude, not precise
30+
figures. For example, a time of 0.00010 seconds indicates an average running
31+
time between 0.00005 seconds and 0.00050 seconds.
32+
33+
| Day | Problem | Domain | Result | Time | Implementation |
34+
| :-: | :---------------------------------------------: | :-------------------------------------------------------------: | :-------------------: | :----: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
35+
| 1 | [Trebuchet?!](src/day01b.c) | Strings, Tries | Sum | 0.0001 | Primitive [trie](https://en.wikipedia.org/wiki/Trie) |
36+
| 2 | [Cube Conundrum](src/day02b.c) | Strings | Sum | 0.0001 | |
37+
| 3 | [Gear Ratios](src/day03b.c) | Dynamic programming | Sum | 0.0001 | Sliding window technique |
38+
| 4 | [Scratchcards](src/day04b.c) | Sets | Sum | 0.0001 | |
39+
| 5 | [If You Give A Seed A Fertilizer](src/day05b.c) | Functions, Sorting algorithms | Minimum | 0.0001 | Function composition, System of linear equations |
40+
| 6 | [Wait For It](src/day06b.c) | Physics, Algebra | Product | 0.0001 | [Quadratic formula](https://en.wikipedia.org/wiki/Quadratic_formula) |
41+
| 7 | [Camel Cards](src/day07b.c) | Statistics, Dictionaries, Sorting algorithms | Sum | 0.0001 | Frequency map with mode-tracking |
42+
| 8 | [Haunted Wasteland](src/day08b.c) | Graph theory, Number theory, Dictionaries | Least Common Multiple | 0.0001 | [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm), map-based graph, base-36 encoding |
43+
| 9 | [Mirage Maintenance](src/day09b.c) | Numerical analysis | Sum | 0.0001 | [Lagrange polynomial](https://en.wikipedia.org/wiki/Lagrange_polynomial) |
44+
| 10 | [Pipe Maze](src/day10a.c) | Geometry, Graph theory, Pathfinding algorithms | Area | 0.0001 | [Flood-fill](https://en.wikipedia.org/wiki/Flood_fill), matrix-based graph |
45+
| 11 | [Cosmic Expansion](src/day11b.c) | Geometry | Sum | 0.0001 | [Taxicab geometry](https://en.wikipedia.org/wiki/Taxicab_geometry) |
46+
| 12 | [Hot Springs](src/day12b.c) | Automata theory, Regular expressions, Dictionaries | Sum | 0.01 | [Non-deterministic finite automaton](https://en.m.wikipedia.org/wiki/Nondeterministic_finite_automaton), iterable dictionary |
47+
| 13 | [Point of Incidence](src/day13b.c) | Binary arithmetic | Sum | 0.0001 | Bit array, bit matrix |
48+
| 14 | [Parabolic Reflector Dish](src/day14b.c) | Strings, Dictionaries | Sum | 0.0001 | Character matrix, cycle detection |
49+
| 15 | [Lens Library](src/day15b.c) | Cryptography, Hash functions, Dictionaries, Strings | Sum | 0.0001 | Iterable ordered dictionary, Length-prefixed string |
50+
| 16 | [The Floor Will Be Lava](src/day16b.c) | Stacks, Sets, Dictionaries | Maximum | 0.01 | Array stack, array set, array map, character matrix |
51+
| 17 | [Clumsy Crucible](src/day17b.c) | Graph theory, Pathfinding algorithms, Priority queues | Minimum | 0.01 | [Dijkstra\'s algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm), [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer), state matrix |
52+
| 18 | [Lavaduct Lagoon](src/day18b.c) | Geometry | Area | 0.0001 | [Shoelace formula](https://en.wikipedia.org/wiki/Shoelace_formula), [Pick\'s theorem](https://en.wikipedia.org/wiki/Pick%27s_theorem) |
53+
| 19 | [Aplenty](src/day19b.c) | Parsing algorithms, Recursion, Stacks, Functions, Dictionaries | Count | 0.0001 | [Recursive descent parser](https://en.wikipedia.org/wiki/Recursive_descent_parser), dictionary |
54+
| 20 | [Pulse Propagation](src/day20b.c) | Graph theory, Number theory, Dictionaries, Sets, Circuit design | Product | 0.01 | [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm), map-based graph, hash set, length-prefixed string |
55+
| 21 | [Step Counter](src/day21a.c) | Graph theory, Sets, Priority queues | Count | 0.001 | [Breadth-first search](https://en.wikipedia.org/wiki/Breadth-first_search), hash set, [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer), matrix-based graph |
56+
| 22 | [Sand Slabs](src/day22b.c) | Geometry, Graph theory, Sorting algorithms, Stacks, Sets | Count | 0.01 | Object-based graph, hash set |
57+
| 24 | [Never Tell Me The Odds](src/day24b.c) | Physics, Linear algebra | Sum | 0.01 | [Line–line intersection](https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection), [Gaussian elimination](https://en.wikipedia.org/wiki/Gaussian_elimination) |
58+
| 25 | [Snowverload](src/day25z.c) | Graph theory | Product | 0.001 | [Max-flow min-cut theorem](https://en.wikipedia.org/wiki/Max-flow_min-cut_theorem), [Ford–Fulkerson algorithm](https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm), adjacency list, iterable graph |
59+
60+
## Constraints
61+
62+
I am working within the following constraints to ensure high code quality.
63+
64+
- Adhere to the [project style guide](cstyle.md).
65+
- Final solutions must be implemented in the C programming language following
66+
the C99 standard.
67+
- Assume signed integer overflow is defined based on two\'s complement.
68+
- All solutions must be standalone files with no user-defined headers and no
69+
external dependencies beyond the C standard library (`libc`) and the C
70+
mathematics library (`libm`). The first and second problems for a given day
71+
must be implemented separately.
72+
- Bounds checking is not required for data structures whose capacity is defined
73+
by a macro.
74+
- The return values of all C standard library functions must be checked, except
75+
for those returned from the following:
76+
- `fprintf`
77+
- `memcpy`
78+
- `printf`
79+
80+
## Omissions
81+
82+
- The solution must produce the correct result for the [examples](data/), not
83+
just the personalized puzzle input. This means that the efficient solutions to
84+
Day 10(b) and Day 21(b) are unfeasible. I have omitted both.
85+
- By design, there is no efficient solution to Day 23(a) and (b). I have
86+
omitted both.

0 commit comments

Comments
 (0)