-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.nim
48 lines (40 loc) · 940 Bytes
/
day3.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sequtils
import sugar
# Count trees on a slope
proc treeCount(input: string, dx, dy: int) : int =
let treeChar = '#'
var
x = 0
y = 0
result = 0
# Go line by line
for line in lines input:
# Check y and x position
if y == 0:
if line[x] == treeChar:
result += 1
# Increase x poisition
x = (x + dx) mod line.len
# Increase y position
y = (y + 1) mod dy
let inputFile = "resources/day3_input.txt"
echo "--- Part 1 Report ---"
echo "Trees = " & $treeCount(inputFile, 3, 1)
## Part 2
# Define slopes
let slopes = [
(1, 1),
(3, 1),
(5, 1),
(7, 1),
(1, 2)
]
# Calculate the tree count from all slopes
let treesPerSlope = collect(newSeq):
for x, y in slopes.items:
treeCount(inputFile, x, y)
# Calculate product
let product = treesPerSlope.foldl(a * b)
echo "--- Part 2 Report ---"
echo "Trees per slope = " & $treesPerSlope
echo "Tree product = " & $product