-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.ps1
83 lines (48 loc) · 1.3 KB
/
day1.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
# Advent of Code
# Day 1 : https://adventofcode.com/2024/day/1
# Date : 12/1/2024
# Completion: ~30 minutes
# Part 1 & 2
$group1 = new-object "system.collections.arraylist"
$group2 = new-object "system.collections.arraylist"
$list = Get-Content .\input.txt
foreach ($i in $list) {
$row = $i -split "\s+"
$group1.Add([int]$row[0])
$group2.Add([int]$row[1])
}
# Part 1
# Since each group has the same number of elements, sort each group and find the distance.
$group1.sort()
$group2.sort()
[int]$sum=0
for ($i=0; $i -lt $group1.Count; $i++) {
$sum += [math]::abs($group1[$i] - $group2[$i])
}
write-host "Total distance between A and B:" $sum
# Part 2
# Count the number of times each occurs to calculate similarity score
[int]$sum=0
for ($i=0; $i -lt $group1.Count; $i++) {
for ($j=0; $j -lt $group2.Count; $j++) {
[int]$count =0
if ($group2[$j] -eq $group1[$i]) {
$count += 1
}
$sum += ($count*$group1[$i])
}
}
write-host "Similarity score: " $sum
$group1.Add([int]$row[0])
$group2.Add([int]$row[0])
}
$sum += $i
if ($i -eq '') {
[void]$groups.add($sum)
$sum = 0
}
}
# Part 1 solution
$groups | Sort-Object -Descending | Select-Object -First 1
# Part 2 solution
$groups | sort-object -Descending | Select-Object -First 3 | Measure-Object -Sum