-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.ps1
52 lines (38 loc) · 1.1 KB
/
day3.ps1
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
49
50
51
52
# Advent of Code 2020, Day 3
# (c) blu3r4y
$INPUT_PATH = "./data/day3.txt"
function Get-PartOne {
param ( [char[][]]$Matrix )
$Result = Get-NumberOfTrees $Matrix 3 1
Write-Output $Result
}
function Get-PartTwo {
param ( [char[][]]$Matrix )
$Result = Get-NumberOfTrees $Matrix 1 1
$Result *= Get-NumberOfTrees $Matrix 3 1
$Result *= Get-NumberOfTrees $Matrix 5 1
$Result *= Get-NumberOfTrees $Matrix 7 1
$Result *= Get-NumberOfTrees $Matrix 1 2
Write-Output $Result
}
function Get-NumberOfTrees {
param ( [char[][]]$Matrix, [int]$SlopeX, [int]$SlopeY )
$LimitX, $LimitY = $Matrix[0].Length, $Matrix.Length
$NumTrees = 0
$X, $Y = 0, 0
# traverse until we reach the bottom
while ($Y -lt $LimitY) {
# retrieve element, but wrap around all axis
$Element = $Matrix[$Y % $LimitY][$X % $LimitX]
if ($Element -eq "#") {
$NumTrees++
}
$X = $X + $SlopeX
$Y = $Y + $SlopeY
}
Write-Output $NumTrees
}
# index with [y, x]
$Matrix = Get-Content $INPUT_PATH
Get-PartOne $Matrix
Get-PartTwo $Matrix