-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.ps1
81 lines (63 loc) · 1.74 KB
/
day15.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function DoHash {
param ($Word)
$hash = 0
foreach ($char in $Word.ToCharArray()) {
$val = [byte][char]$char
$hash += $val
$hash *= 17
$hash %= 256
}
return $hash
}
function Part1 {
param ($FileName)
$content = Get-Content $FileName
$codes = $content.Split(",")
$total = 0
foreach ($code in $codes) {
$total += DoHash $code
}
return $total
}
function GetLetterEndIndex {
param ($Code)
for ($i = 0; $i -lt $Code.Length; $i++) {
if ($Code[$i] -eq '=' -or $Code[$i] -eq '-') {
return $i
}
}
return -1
}
function Part2 {
param ($FileName)
$content = Get-Content $FileName
$codes = $content.Split(",")
$hashmaps = [System.Collections.Specialized.OrderedDictionary[]]::new(256)
foreach ($code in $codes) {
$endIndex = GetLetterEndIndex $code
$codeword = $code.Substring(0, $endIndex)
$hash = DoHash $codeword
if ($code[$endIndex] -eq '=') {
if ($null -eq $hashmaps[$hash]) {
$hashmaps[$hash] = [ordered]@{}
}
$hashmaps[$hash][$codeword] = [System.Int32]::Parse($code[$endIndex + 1])
}
elseif ($null -ne $hashmaps[$hash]) {
# '-'
$hashmaps[$hash].Remove($codeword)
}
}
$totalFocalPower = 0
for ($i = 0; $i -lt $hashmaps.Length; $i++) {
$hm = $hashmaps[$i]
if ($hm.Count -gt 0) {
for ($j = 0; $j -lt $hm.Keys.Count; $j++) {
$totalFocalPower += ($i + 1) * ($j + 1) * $hm[$hm.Keys[$j]]
}
}
}
return $totalFocalPower
}
Write-Output "Part 1: $(Part1 day15.txt)"
Write-Output "Part 2: $(Part2 day15.txt)"