2020 Advent of Code–Day 3

This series looks at the Advent of Code challenges.

As one of my goals, I’m working through challenges. This post looks at day 3.

Part 1

Day 3 was tough. The explanation isn’t great, at least, I didn’t get it at first. Essentially you have a map, and then you have some slope. The first part has a slope of right 3, down 1. If you move, assuming the upper left is (1,1), the next spot is 2, 4. That’s if you count going down as positive.

Here’s the map:

..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#

If I count each space, then there is a period (.) in this (2,4) space. If it’s a tree, then there is a hash (#) there. We are trying to count trees before we hit bottom.

The trick I missed is that the map we’ve given repeats. It repeats to the right as often as needed to get to the bottom.

This is really a coordinate problem, counting each down as we move, and repeating the map. The trick often in a short width here is to do the math to wrap around from the right to left if you run out of room.

In SQL, I used a loop. I didn’t spend a ton of time, but couldn’t see a good way to avoid this as I need this to be readable, and I need this to keep working through the map. Here’s the code:

WHILE @currentrow <= @rows
  BEGIN
    SELECT @currentcol += @right
    IF @currentcol > @width
      SELECT @currentcol = @currentcol - @width
    SELECT @currentrow += @down;

   SELECT @trees = @trees + CASE
       WHEN SUBSTRING(dataval, @currentcol, 1) = '#' THEN 1
       ELSE 0
    end
     FROM day3
     WHERE rowkey = @currentrow
  END
SELECT @trees AS TreeCount

I move, count the value if there’s as tree, and then continue moving through the next rows. The WHERE clause orients the rows.

It worked. I go the right answer here.

Part 2

In part 2, this changes to checking a number of sloops and then multiplying the results together. In terms of my code, this is really a repetitive way of running the code again. I could have used different variables and checked multiple slopes at once, but I was busy.

In python, I did something similar. I essentially calculated the next X and Y position, and then looped through the file. Each time the Y matched the current row, I checked for the matching #. If it matched, increment.

Once I matched the correct Y, I incremented X and Y.

Posted in Blog | Tagged , , , , , | Comments Off on 2020 Advent of Code–Day 3

Daily Coping 24 Feb 2021

I started to add a daily coping tip to the SQLServerCentral newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag. 

Today’s tip is to contribute positively to a good cause for your community.

Winter is usually not too bad in my area, but we do get storms once in awhile. This used to be more of a hassle when I had kids in school, or I had to go to the airport, but now it’s not too bad. Worst case, I can’t go to the gym that day and need to figure out meals from what’s in the house.

However, when we get more than a few inches, it can make driving hard. Recently we had a decent storm, which meant that part of the trails to the hay feeders gets hard to traverse. I sometimes run the tractor and blade down those trails.

This time, when I did, I also took 20-30 minutes to go down my driveway, which helps me, but also down the 1/4 mile road I live on, clearing a space for others. A few people have tractors, but we somewhat randomly take turns doing this.

I decided to take my turn.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 24 Feb 2021

Shared Security

When I worked in a large Operations team, we had various passwords for some systems that we stored in a password vault. This was on a network share that only the administrators had access to read. We used this for keeping a number of passwords for various services and systems that required sysadmins to access.

However, we tried not to use shared accounts whenever possible. We wanted to ensure an audit trail, for both compliance and understanding of what happened in our environments. All sysadmins had two accounts, which allowed them to access most services. We only had a user/password stored for certain systems that needed some sort of separate account, and we did change those passwords regularly.

Our customers didn’t always do this. In fact, I regularly found different groups using a single account, always logged in, for which everyone knew the user name and credentials. They never seemed to consider this a problem, despite regular security warnings from administrators.

I was reminded of this story about a water treatment facility in Florida that was hacked. They had a shared account, used by many people, the same password for everyone, and a lack of a firewall. An intruder caused a release of chemicals, but no one was injured.

Firewalls prevent a lot of issues, if they are configured to limit access. In addition, ensuring that each user has different passwords is important. This allows you to turn off access for certain people, which includes former employees. That’s basic security, and I’m surprised how often people forget about this, often because they worry about some system breaking.

The tools we have to enforce stronger security have improved over the years, but we need to take advantage of them, and we need to ensure that good, if not best, security practices are followed. It’s easy to become lax over time, but remember that there are lots of threats out there, from hackers to malware, to former employees.

Far too many people are attacked, more than are in the news, so don’t get complacent. We are all at risk.

Steve Jones

Listen to the podcast at Libsyn, Stitcher, Spotify, or iTunes.

Posted in Editorial | Tagged | Comments Off on Shared Security

Daily Coping 23 Feb 2021

I started to add a daily coping tip to the SQLServerCentral newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag. 

Today’s tip is to get outside and notice 5 beautiful things.

It’s winter, and it’s cold. Still, you can find beauty in little things. I decided to grab a few shots recently while I was outside. Here are my beautiful things:

  • Azure – she think’s she’s co-trainer with my wife. She’s ready to start, waiting for the horse to be saddled.
  • Vega – She was running around, and stopped to wait for me. Quite patiently.
  • The view in the snow from the front yard. Always makes me smile.
  • Sun in Denver; a big reason we moved here.
  • A new tool, Ranger 570. The old one is old, and this gives us a spare, which will make repairs easier. However, this is also a really nicely engineered machine.

20210221_171606

20210220_132207


20210218_091609

20210222_122347

20210220_122535

Posted in Blog | Tagged , , | Comments Off on Daily Coping 23 Feb 2021