-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.ps1
144 lines (96 loc) · 3.44 KB
/
day7.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Advent of Code
# Day 7 : https://adventofcode.com/2024/day/7
# Date : 12/7/2024
$data = @"
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
"@
$data = get-content .\input_day7.txt
$table = $data -split "\r\n"
# Part 1
# Find number of combinations of + and * based on spaces between digits
# The test values are saved in a hash table and the test results are updated to $true.
$hashmap = @{}
for ($r=0; $r -lt $table.Count; $r++){
$test = $table[$r] -split ":"
$hashmap.add($test[0],$false)
$numbers = $test[1].trim() -split " "
$spacecount = ($test[1].trim() -split '\s+' | Measure-Object -Line).Lines - 1
$chars = "+*"
$operators = new-object system.collections.stack
$numberstack = new-object system.collections.stack
# Go through all possible combinations of operators
for ($i = 0; $i -lt [Math]::Pow($chars.Length, $spacecount); $i++) {
$operators.Clear()
$numberstack.Clear()
for ($n = $numbers.Length - 1; $n -ge 0; $n--) { $numberstack.Push($numbers[$n]) }
for ($j = 0; $j -lt $spacecount; $j++) {
$index = [Math]::Floor(($i / [Math]::Pow($chars.length, $j)) % $chars.Length)
#write-host $index
$operators.Push($chars[$index])
}
# Reset the list of numbers in the stack
for ($k=0; $k -lt $numbers.Length ; $k++) { $operands.Push($numbers[$k])}
# Take two number, apply the operator, and push back onto the stack. Repeat.
while ($operators.count -gt 0) {
$symbol = $operators.pop()
$n1 = [int64]$numberstack.pop()
$n2 = [int64]$numberstack.pop()
switch ($symbol) {
'+' { $numberStack.Push($n1 + $n2) }
'*' { $numberStack.Push($n1 * $n2) }
}
}
$result = $numberstack.pop()
if ($result -eq $test[0]) {Write-host "Match $test $operators"; $hashmap.($test[0]) = $true} else {}
}
}
$hashmap
$trueKeys = $hashmap.Keys | Where-Object { $hashmap[$_] -eq $true }
$truekeys | measure-object -sum
# Part 2:
$hashmap = @{}
for ($r=0; $r -lt $table.Count; $r++){
$test = $table[$r] -split ":"
$hashmap.add($test[0],$false)
$numbers = $test[1].trim() -split " "
$spacecount = ($test[1].trim() -split '\s+' | Measure-Object -Line).Lines - 1
$chars = "+*"
$operators = new-object system.collections.stack
$numberstack = new-object system.collections.stack
# Go through all possible combinations of operators
for ($i = 0; $i -lt [Math]::Pow($chars.Length, $spacecount); $i++) {
$operators.Clear()
$numberstack.Clear()
for ($n = $numbers.Length - 1; $n -ge 0; $n--) { $numberstack.Push($numbers[$n]) }
for ($j = 0; $j -lt $spacecount; $j++) {
$index = [Math]::Floor(($i / [Math]::Pow($chars.length, $j)) % $chars.Length)
#write-host $index
$operators.Push($chars[$index])
}
# Reset the list of numbers in the stack
for ($k=0; $k -lt $numbers.Length ; $k++) { $operands.Push($numbers[$k])}
# Take two number, apply the operator, and push back onto the stack. Repeat.
while ($operators.count -gt 0) {
$symbol = $operators.pop()
$n1 = [int64]$numberstack.pop()
$n2 = [int64]$numberstack.pop()
switch ($symbol) {
'+' { $numberStack.Push($n1 + $n2) }
'*' { $numberStack.Push($n1 * $n2) }
}
}
$result = $numberstack.pop()
if ($result -eq $test[0]) {Write-host "Match $test $operators"; $hashmap.($test[0]) = $true} else {}
}
}
$hashmap
$trueKeys = $hashmap.Keys | Where-Object { $hashmap[$_] -eq $true }
$truekeys | measure-object -sum