Skip to content

Commit 39e3c6a

Browse files
committed
2024, day2
1 parent d41e61d commit 39e3c6a

File tree

8 files changed

+1198
-0
lines changed

8 files changed

+1198
-0
lines changed

2024/2/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
main1:
2+
go build -o main1 main1.go common.go
3+
4+
main2:
5+
go build -o main2 main2.go common.go
6+
7+
.PHONY: run1 run2 clean
8+
9+
run1: main1
10+
./main1 <input
11+
12+
run2: main2
13+
./main2 <input
14+
15+
clean:
16+
rm -f main1 main2
17+

2024/2/assignment

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
--- Day 2: Red-Nosed Reports ---
2+
3+
Fortunately, the first location The Historians want to search isn't a
4+
long walk from the Chief Historian's office.
5+
6+
While the [1]Red-Nosed Reindeer nuclear fusion/fission plant appears to
7+
contain no sign of the Chief Historian, the engineers there run up to
8+
you as soon as they see you. Apparently, they still talk about the time
9+
Rudolph was saved through molecular synthesis from a single electron.
10+
11+
They're quick to add that - since you're already here - they'd really
12+
appreciate your help analyzing some unusual data from the Red-Nosed
13+
reactor. You turn to check if The Historians are waiting for you, but
14+
they seem to have already divided into groups that are currently
15+
searching every corner of the facility. You offer to help with the
16+
unusual data.
17+
18+
The unusual data (your puzzle input) consists of many reports , one
19+
report per line. Each report is a list of numbers called levels that
20+
are separated by spaces. For example:
21+
22+
7 6 4 2 1
23+
1 2 7 8 9
24+
9 7 6 2 1
25+
1 3 2 4 5
26+
8 6 4 4 1
27+
1 3 6 7 9
28+
29+
30+
This example data contains six reports each containing five levels.
31+
32+
The engineers are trying to figure out which reports are safe . The
33+
Red-Nosed reactor safety systems can only tolerate levels that are
34+
either gradually increasing or gradually decreasing. So, a report only
35+
counts as safe if both of the following are true:
36+
* The levels are either all increasing or all decreasing .
37+
* Any two adjacent levels differ by at least one and at most three .
38+
39+
In the example above, the reports can be found safe or unsafe by
40+
checking those rules:
41+
* 7 6 4 2 1 : Safe because the levels are all decreasing by 1 or 2.
42+
* 1 2 7 8 9 : Unsafe because 2 7 is an increase of 5.
43+
* 9 7 6 2 1 : Unsafe because 6 2 is a decrease of 4.
44+
* 1 3 2 4 5 : Unsafe because 1 3 is increasing but 3 2 is decreasing.
45+
* 8 6 4 4 1 : Unsafe because 4 4 is neither an increase or a
46+
decrease.
47+
* 1 3 6 7 9 : Safe because the levels are all increasing by 1, 2, or
48+
3.
49+
50+
So, in this example, 2 reports are safe .
51+
52+
Analyze the unusual data from the engineers. How many reports are safe?
53+
54+
--- Part Two ---
55+
56+
The engineers are surprised by the low number of safe reports until
57+
they realize they forgot to tell you about the Problem Dampener .
58+
59+
The Problem Dampener is a reactor-mounted module that lets the reactor
60+
safety systems tolerate a single bad level in what would otherwise be a
61+
safe report. It's like the bad level never happened!
62+
63+
Now, the same rules apply as before, except if removing a single level
64+
from an unsafe report would make it safe, the report instead counts as
65+
safe.
66+
67+
More of the above example's reports are now safe:
68+
* 7 6 4 2 1 : Safe without removing any level.
69+
* 1 2 7 8 9 : Unsafe regardless of which level is removed.
70+
* 9 7 6 2 1 : Unsafe regardless of which level is removed.
71+
* 1 3 2 4 5 : Safe by removing the second level, 3 .
72+
* 8 6 4 4 1 : Safe by removing the third level, 4 .
73+
* 1 3 6 7 9 : Safe without removing any level.
74+
75+
Thanks to the Problem Dampener, 4 reports are actually safe !
76+
77+
Update your analysis by handling situations where the Problem Dampener
78+
can remove a single level from unsafe reports. How many reports are now
79+
safe?
80+
81+
References
82+
83+
1. file:///2015/day/19

2024/2/common.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import "strconv"
4+
5+
func toInt(xs []string) ([]int, error) {
6+
l := make([]int, len(xs))
7+
for i, x := range xs {
8+
v, err := strconv.Atoi(x)
9+
if err != nil {
10+
return nil, err
11+
}
12+
l[i] = v
13+
}
14+
return l, nil
15+
}
16+
17+
func safe(xs []int) bool {
18+
asc := xs[1]-xs[0] > 0
19+
20+
for i := 1; i < len(xs); i++ {
21+
incr := xs[i] - xs[i-1]
22+
23+
if incr > 0 != asc {
24+
return false
25+
}
26+
27+
if incr < 0 {
28+
incr = -incr
29+
}
30+
31+
if incr < 1 || incr > 3 {
32+
return false
33+
}
34+
}
35+
return true
36+
}

0 commit comments

Comments
 (0)