Skip to content

Commit f28743a

Browse files
authored
Merge pull request #24 from Arxcis/day03/c
Solve day03 in c
2 parents f9ed587 + 4bfbb68 commit f28743a

File tree

6 files changed

+149
-14
lines changed

6 files changed

+149
-14
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
4. Test your solution.
99

1010
```sh
11-
../languages/python.sh day-03/input.txt day-03/output.txt day-03/solutions/super-optimized.py
11+
cd days/day03
12+
../../languages/python.sh input.txt output.txt solutions/super-optimized.py
1213
```
1314

14-
5. Add the test to `day03/test.sh`, to let the CI know how to test your solution.
15+
5. Add the test to `day03/test.sh`, to let the CI know how to test your solution.
1516
6. Make a Pull Request to the `main` branch.
1617
7. One of the maintainers will merge when the tests pass!
1718
8. Remember to have fun :tada:

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# adventofcode2020 🕯️ 🎄 ⛄
22

3-
Welcome to this community project, where we collaboratively solve the 2020 edition of https://adventofcode.com/.
3+
Welcome to this community project, where we collaboratively solve the 2020 edition of https://adventofcode.com/.
44

55
### [CONTRIBUTING.md](./CONTRIBUTING.md)
66

7-
Anyone is encouraged to contribute, and as a contributor you may do whatever you want with this code. Treat it as your own :+1:
7+
Anyone is encouraged to contribute, and as a contributor you may do whatever you want with this code. Treat it as your own :+1:
88

99
A shout-out to the developers over at Maritime Optima - https://github.com/orgs/MaritimeOptima/people - volunteering to support this project :pray:
1010

1111
Also a big thanks to the rest of our contributors :tada:
1212
- @Celebrian
13-
- @Avokadoen
13+
- @Avokadoen
14+
- @Stekpotet
1415

15-
Contributors are welcomed to join our private leaderboard :sunglasses: Use the join code **376961-8a514359** at https://adventofcode.com/2020/leaderboard/private
16+
Contributors are welcomed to join our private leaderboard :sunglasses: Use the join code **376961-8a514359** at https://adventofcode.com/2020/leaderboard/private
1617

1718
If you enjoy working on this project, consider sharing it with your friends. The more the merrier 🚀
1819

@@ -52,12 +53,12 @@ If you enjoy working on this project, consider sharing it with your friends. The
5253

5354
| Language | Command | # of days completed |
5455
|----------|------------------------------------------------|---------------------|
55-
| c | gcc main.c -o out.gcc && ./out.gcc | 1 / 25 |
56-
| c++ | g++ main.cpp -o out.g++ && ./out.g++ | 1 / 25 |
57-
| python | python3 main.py | 1 / 25 |
56+
| python | python3 main.py | 3 / 25 |
57+
| sml | polyc main.sml -o out.polyc && ./out.polyc | 3 / 25 |
58+
| c | gcc main.c -o out.gcc && ./out.gcc | 2 / 25 |
59+
| c++ | g++ main.cpp -o out.g++ && ./out.g++ | 2 / 25 |
5860
| ruby | ruby main.rb | 1 / 25 |
5961
| rust | rustc main.rs -o out.rustc && ./out.rustc | 1 / 25 |
60-
| sml | polyc main.sml -o out.polyc && ./out.polyc | 1 / 25 |
6162
| bash | bash main.bash | 0 / 25 |
6263
| golang | go run main.go | 0 / 25 |
6364
| java | javac Main.java && java Main | 0 / 25 |

days/day-03/README.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
1-
# days/day-03
2-
## --- Part 1 ---
3-
## --- Part 2 ---
1+
# --- Day 3: Toboggan Trajectory ---
2+
3+
With the toboggan login problems resolved, you set off toward the airport. While travel by toboggan might be easy, it's certainly not safe: there's very minimal steering and the area is covered in trees. You'll need to see which angles will take you near the fewest trees.
4+
5+
Due to the local geology, trees in this area only grow on exact integer coordinates in a grid. You make a map (your puzzle input) of the open squares (.) and trees (#) you can see. For example:
6+
7+
```
8+
..##.......
9+
#...#...#..
10+
.#....#..#.
11+
..#.#...#.#
12+
.#...##..#.
13+
..#.##.....
14+
.#.#.#....#
15+
.#........#
16+
#.##...#...
17+
#...##....#
18+
.#..#...#.#
19+
```
20+
21+
These aren't the only trees, though; due to something you read about once involving arboreal genetics and biome stability, the same pattern repeats to the right many times:
22+
23+
```
24+
..##.........##.........##.........##.........##.........##....... --->
25+
#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
26+
.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
27+
..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
28+
.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
29+
..#.##.......#.##.......#.##.......#.##.......#.##.......#.##..... --->
30+
.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
31+
.#........#.#........#.#........#.#........#.#........#.#........#
32+
#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...
33+
#...##....##...##....##...##....##...##....##...##....##...##....#
34+
.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.# --->
35+
```
36+
37+
You start on the open square (.) in the top-left corner and need to reach the bottom (below the bottom-most row on your map).
38+
39+
The toboggan can only follow a few specific slopes (you opted for a cheaper model that prefers rational numbers); start by counting all the trees you would encounter for the slope right 3, down 1:
40+
41+
From your starting position at the top-left, check the position that is right 3 and down 1. Then, check the position that is right 3 and down 1 from there, and so on until you go past the bottom of the map.
42+
43+
The locations you'd check in the above example are marked here with O where there was an open square and X where there was a tree:
44+
45+
```
46+
..##.........##.........##.........##.........##.........##....... --->
47+
#..O#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
48+
.#....X..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
49+
..#.#...#O#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
50+
.#...##..#..X...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
51+
..#.##.......#.X#.......#.##.......#.##.......#.##.......#.##..... --->
52+
.#.#.#....#.#.#.#.O..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
53+
.#........#.#........X.#........#.#........#.#........#.#........#
54+
#.##...#...#.##...#...#.X#...#...#.##...#...#.##...#...#.##...#...
55+
#...##....##...##....##...#X....##...##....##...##....##...##....#
56+
.#..#...#.#.#..#...#.#.#..#...X.#.#..#...#.#.#..#...#.#.#..#...#.# --->
57+
```
58+
59+
In this example, traversing the map using this slope would cause you to encounter 7 trees.
60+
61+
Starting at the top-left corner of your map and following a slope of right 3 and down 1, how many trees would you encounter?
62+
63+
## --- Part Two ---
64+
65+
Time to check the rest of the slopes - you need to minimize the probability of a sudden arboreal stop, after all.
66+
67+
Determine the number of trees you would encounter if, for each of the following slopes, you start at the top-left corner and traverse the map all the way to the bottom:
68+
69+
```
70+
Right 1, down 1.
71+
Right 3, down 1. (This is the slope you already checked.)
72+
Right 5, down 1.
73+
Right 7, down 1.
74+
Right 1, down 2.
75+
```
76+
77+
In the above example, these slopes would find 2, 7, 3, 4, and 2 tree(s) respectively; multiplied together, these produce the answer 336.
78+
79+
What do you get if you multiply together the number of trees encountered on each of the listed slopes?

days/day-03/solutions/.keep

Whitespace-only changes.

days/day-03/solutions/day03.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
const int MAX_LINE = 100;
5+
const char TREE = '#';
6+
7+
struct Toboggan {
8+
int x;
9+
int trees_hit;
10+
const int right;
11+
const int down;
12+
};
13+
14+
// Declare a bunch of toboggans (sleighs)
15+
const int TOBOGGANS_SIZE = 5;
16+
struct Toboggan toboggans[5] = {
17+
{ .x = 0, .right = 3, .down = 1, .trees_hit = 0 },
18+
{ .x = 0, .right = 1, .down = 1, .trees_hit = 0 },
19+
{ .x = 0, .right = 5, .down = 1, .trees_hit = 0 },
20+
{ .x = 0, .right = 7, .down = 1, .trees_hit = 0 },
21+
{ .x = 0, .right = 1, .down = 2, .trees_hit = 0 },
22+
};
23+
24+
int main() {
25+
// Ignore start-line
26+
char line[MAX_LINE];
27+
fgets(line, MAX_LINE, stdin);
28+
const int WRAP_WIDTH = strlen(line) - 1;
29+
30+
// Read line by line starting from line 1
31+
int y = 1;
32+
for (char line[MAX_LINE]; fgets(line, MAX_LINE, stdin); ++y) {
33+
34+
for (int i = 0; i < TOBOGGANS_SIZE; ++i) {
35+
struct Toboggan* toboggan = &toboggans[i];
36+
37+
// Skip current line, for this specific toboggan, if...
38+
if (y % toboggan->down != 0) {
39+
continue;
40+
}
41+
42+
toboggan->x = (toboggan->x + toboggan->right) % WRAP_WIDTH;
43+
toboggan->trees_hit = toboggan->trees_hit + (line[toboggan->x] == TREE);
44+
}
45+
}
46+
47+
long int tree_product = 1;
48+
for (int i = 0; i < TOBOGGANS_SIZE; ++i) {
49+
tree_product = tree_product * toboggans[i].trees_hit;
50+
}
51+
52+
printf("%d\n", toboggans[0].trees_hit);
53+
printf("%ld\n", tree_product);
54+
55+
return 0;
56+
}

days/day-03/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ D=$(dirname $(realpath $0))
66
echo ""
77
echo "--- Day 3: Counting trees ---"
88
$D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/day03.py
9-
$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day03.sml
9+
$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day03.sml
1010
$D/../../languages/python.sh $D/input.txt $D/output.txt $D/solutions/python_klyve.py
11+
$D/../../languages/c.sh $D/input.txt $D/output.txt $D/solutions/day03.c
1112
echo ""

0 commit comments

Comments
 (0)