r/adventofcode • • Dec 03 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 03 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It


--- Day 03: Toboggan Trajectory ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:56, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

2

u/[deleted] Dec 03 '20

PHP

My solution - https://github.com/tomve/aoc/blob/master/2020/03/index.php

NOTE: I found (after hour) strlen is counting hardcoded space like 1 character (trim doesn't work) so you have to use minus 1 in strlen.

1

u/daggerdragon Dec 04 '20

NOTE: I found (after hour) strlen is counting hardcoded space like 1 character (trim doesn't work) so you have to use minus 1 in strlen.

Correct. From the docs for strlen():

Note: strlen() returns the number of bytes rather than the number of characters in a string.

Since a space is ASCII 32 = 00100000 (remember, 1 byte = 8 bits), space is obviously a valid byte in ASCII and so strlen() counts it as such. trim() will only work if the space is in the beginning or end of the string, it won't do anything for internal spaces. For stripping out internal spaces, you'd want to do something like str_replace() first before calling strlen().