-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.ps1
80 lines (50 loc) · 1.46 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
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
# Advent of Code
# Day 3 : https://adventofcode.com/2024/day/3
# Date : 12/3/2024
# Completion:
$data = get-content .\input_day3.txt
$data = $data -join ""
# Part 1
# Remove all strings that are not mul(x,y)
# Use Boyer-Moore algorithm to find the patterns.
# Switched to using RegEx
$pattern = "(mul\(\d{1,3},\d{1,3}\))";
$matches = [regex]::Matches($data,$pattern)
$sumofproducts = 0
foreach ($m in $matches){
$pattern = "[mul()]"
$newval = [regex]::Replace($m.value,$pattern,"")
[int[]]$set = $newval -split ","
$product = 1
foreach ($s in $set) {
$product *= $s
}
$sumofproducts += $product
}
Write-host "Sum of products: " $sumofproducts
# Part 2
# Remove all strings that are not mul(x,y) or do() or don't()
# Use Boyer-Moore algorithm to find the patterns.
# Switched to using RegEx
$pattern = "(mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\))";
$matches = [regex]::Matches($data,$pattern)
[int]$sumofproducts = 0
[bool]$enabled = $true
foreach ($m in $matches){
if ($m.Value -eq "do()") { $enabled = $true }
if ($m.Value -eq "don't()") { $enabled = $false }
if ($m.value -like "mul*" -and $enabled) {
$pattern = "[mul()]"
$newval = [regex]::Replace($m.value,$pattern,"")
[int[]]$set = $newval -split ","
$product = 1
foreach ($s in $set) {
$product *= $s
}
write-host $product
$sumofproducts += $product
}
}
Write-host "Sum of products: " $sumofproducts
# 140063820 too high
# 76911921